我卡住了。似乎由PHP完成的AES加密無法在Windows中解密。PHP加密和Windows解密
PHP代碼:
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,"12345678", "test", MCRYPT_MODE_CBC));
Windows代碼: 「S」 具有由上述響應從BASE64轉換回後創建的字符串。
bool Decrypt(char* s,char* key,char* dest)
{
// Create the crypto provider context.
HCRYPTPROV hProvider = NULL;
if (!CryptAcquireContext(&hProvider,
NULL, // pszContainer = no named container
MS_ENH_RSA_AES_PROV, // pszProvider = default provider
PROV_RSA_AES,
0))
return false;
// Construct the blob necessary for the key generation.
aes128keyBlob aes_blob128;
aes_blob128.header.bType = PLAINTEXTKEYBLOB;
aes_blob128.header.bVersion = CUR_BLOB_VERSION;
aes_blob128.header.reserved = 0;
aes_blob128.header.aiKeyAlg = CALG_AES_128;
aes_blob128.keySize = 16;
memcpy(aes_blob128.bytes, key, 16);
HCRYPTKEY hKey = NULL;
if (!CryptImportKey(hProvider,
(BYTE*)(&aes_blob128),
sizeof(aes_blob128),
NULL, //
0, //
&hKey)) {
...
}
// Set Mode
DWORD dwMode = CRYPT_MODE_CBC;
CryptSetKeyParam(hKey, KP_MODE, (BYTE*)&dwMode, 0);
DWORD length = 16;
BOOL X = CryptDecrypt(hKey,
NULL, // hHash = no hash
TRUE, // Final
0,
(BYTE*)s,
&length);
//int le = GetLastError();
memcpy(dest,s,16);
CryptDestroyKey(hKey);
CryptReleaseContext(hProvider, 0);
}
什麼可能是錯的?
你怎麼傳遞加密的字符串?即餅乾,數據庫等.. – solidau
傳遞給Windows程序?通過打印並將其作爲瀏覽器的輸出。 – Michael
你在哪裏處理初始化向量(IV)?如果你沒有設置IV,PHP會使用其所有字節設置爲''\ 0''的字符;但它看起來並不像你提供給你的解密程序。儘管如此,你真的應該使用IV,否則可能會溝通CBC並使用ECB(當然是以犧牲安全爲代價)。 – NullUserException