问题
We have an C++ application where it encrypts password using AES algorithm through openssl API's (EVP_EncryptInit_ex, EVP_EncryptUpdate, EVP_EncryptFinal_ex).
Now I try to develop UWP application through which I want to do same authentication as the above legacy app does.
But when I'm using API's from "Windows.Security.Cryptography" my authentication fails as the encrypted data output is different from the legacy app, so decryption fails and there by authentication.
Legacy app uses one Key and initialization vector and same are used in my UWP app also. Legacy app uses cipher mode CBC, I had used "AesCbcPKCS7" in my UWP but the encrypted output is different.
I also tried just "AesCbc" and padded data as per PKCS7 padding manually still the encrypted output is different and my authentication is failing.
Kindly help with the above issue.
Some of Legacy App Code API Flow Sample:
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_EncryptUpdate(&ctx, output, &nOutputSize, input, strlen((const char *)input)+1);
EVP_EncryptFinal_ex(&ctx, output + nOutputSize, &nTmplen);
EVP_CIPHER_CTX_cleanup(&ctx);
UWP Code sample below:
IBuffer iBuf = CryptographicBuffer.ConvertStringToBinary(strPwd, BinaryStringEncoding.Utf8);
IBuffer iPubKey = CryptographicBuffer.CreateFromByteArray(PUB_KEY);
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
CryptographicKey cryptKey = objAlg.CreateSymmetricKey(iPubKey);
IBuffer iv = CryptographicBuffer.CreateFromByteArray(INIT_VECTOR);
IBuffer encryptPwd = CryptographicEngine.Encrypt(cryptKey, iBuf, iv);
string strEncPwd = CryptographicBuffer.EncodeToBase64String(encryptPwd);
来源:https://stackoverflow.com/questions/54005487/aes-encryption-not-working-properly-in-uwp