Chiave AES-256 con codice duro con Wincrypt & CryptimportKey
-
20-08-2019 - |
Domanda
Devo avere un'applicazione Win32 caricare un tasto AES-256 con codice duro, idealmente usando i metodi wincrypt.h. Ho la mia chiave in un char non firmato [32] ma non riesco a trovare il formato corretto di una chiara da passare a CryptImportKey. Tutto sembra darmi errori di parametri non validi. C'è un modo per fare questo?
(Anche importante è come impostare IV in Wincrypt. Non riesco a vedere come farlo)
Soluzione
Risolto. Stavo usando il BType sbagliato e usando 256 per KeySize anziché 32.
BYTE myPrivateKey[] =
{1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32};
BYTE myIV[] =
{1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16};
struct aes256keyBlob
{
BLOBHEADER hdr;
DWORD keySize;
BYTE bytes[32];
} blob;
blob.hdr.bType = PLAINTEXTKEYBLOB;
blob.hdr.bVersion = CUR_BLOB_VERSION;
blob.hdr.reserved = 0;
blob.hdr.aiKeyAlg = CALG_AES_256;
blob.keySize = 32;
memcpy(blob.bytes, myPrivateKey, 32);
HCRYPTKEY hKey;
if (CryptImportKey(hCryptProv, (BYTE*)&blob, sizeof(aes256keyBlob), NULL, 0, &hKey))
{
if(CryptSetKeyParam(hKey, KP_IV, myIV, 0))
{
//do decryption here
}
else{/*error*/}
CryptDestroyKey(hKey);
}
else{/*error*/}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow