Sono bloccato. Sembra che la crittografia AES fatta da PHP non possa essere decifrata in Windows.PHP Encrypt and Windows Decrypt
codice PHP:
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,"12345678", "test", MCRYPT_MODE_CBC));
codice di Windows: "s" ha la stringa che viene creato dalla risposta di cui sopra dopo la conversione di ritorno da 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);
}
Cosa potrebbe essere che non va?
come stai passando la stringa crittografata? cioè cookie, db ecc. – solidau
Passandolo nuovamente al programma Windows? Via stampa e lo prende come output del browser. – Michael
Dove stai gestendo il vettore di inizializzazione (IV)? Se non si imposta un IV, PHP ne usa uno con tutti i suoi byte impostati su ''\ 0''; ma non sembra che tu lo stia fornendo alla tua routine di decrittazione. Dovresti davvero usare una flebo, altrimenti potresti anche cullare la CBC e usare la BCE (a scapito della sicurezza, ovviamente). – NullUserException