C# の AES 復号を C++で実装しなおす その2
BYTE array[32] = salt; Rfc2898DeriveBytes aesDeriveBytes = new Rfc2898DeriveBytes(password, array, 1000); aes.KeySize = 256; aes.BlockSize = 128; aes.Key = aesDeriveBytes.GetBytes(aesKeySize / 8); aes.IV = aesDeriveBytes.GetBytes(aes.BlockSize / 8); aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CFB; CryptoStream cryptoStream = new CryptoStream(fileStream, aesManaged.CreateDecryptor(), CryptoStreamMode.Read); |
C#赤字部分の実装。
#include <openssl> #include <openssl> #pragma comment (lib, "libeay32.lib") : DWORD len; |
変換 C++コード 今回も openssl の力を借りる。
復号してみたらうまくいった! ・ω・
ちなみに、以下の様なコード書いてwincryptでやろうとしたらダメだった。
key_blob.hdr.bType = PLAINTEXTKEYBLOB; key_blob.hdr.bVersion = CUR_BLOB_VERSION; key_blob.hdr.reserved = 0; key_blob.hdr.aiKeyAlg = CALG_AES_256; key_blob.len = 32; int result; DWORD dwPaddingMode = PKCS5_PADDING; memcpy(key_blob.key, rijndaelManaged, 32); if( !CryptAcquireContext(&hCrypt,NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) ) CryptAcquireContext(&hCrypt, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT); CryptImportKey(hCrypt, (BYTE *)&key_blob, sizeof(key_blob), NULL, 0, &hKey); CryptSetKeyParam(hKey, KP_MODE, (BYTE *)&dwMode, 0); CryptSetKeyParam(hKey, KP_PADDING, (CONST PBYTE)&dwPaddingMode, 0); CryptSetKeyParam(hKey, KP_IV, IV, 0); pdwDataLen = fLen; CryptDecrypt(hKey, NULL, TRUE, 0, buf, &pdwDataLen); |
Comments