硬编码AES-256密钥与WinCrypt& CryptImportKey [英] Hard coded AES-256 key with WinCrypt & CryptImportKey
问题描述
我需要一个Win32应用程序加载一个硬编码的AES-256密钥,理想情况下使用WinCrypt.h方法。我有一个unsigned char [32]中的键,但我找不到一个密钥blob的正确格式传递给CryptImportKey。一切似乎给我无效的参数错误。有没有办法做到这一点?
I need to have a Win32 application load a hard coded AES-256 key, ideally using the WinCrypt.h methods. I've got my key in an unsigned char[32] but I can't find the correct format of a key blob to pass to CryptImportKey. Everything seems to give me invalid parameter errors. Is there any way to do this?
(同样重要的是如何在WinCrypt中设置IV,我看不到如何做到这一点)
(Also important is how to set IV in WinCrypt. I can't see how to do that at all)
推荐答案
解决了。我使用的是错误的bType,使用256作为keySize而不是32。
Solved it. I was using the wrong bType and using 256 for keySize instead of 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*/}
这篇关于硬编码AES-256密钥与WinCrypt& CryptImportKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!