提问人:Roderick Forest 提问时间:11/3/2023 最后编辑:IInspectableRoderick Forest 更新时间:11/3/2023 访问量:112
在 BCryptEncrypt winapi 中提供太短的消息时出现错误C000000D
Error C000000D when giving a too short message in the BCryptEncrypt winapi
问:
我正在尝试使用一些 Winapi 使用公共 PEM RSA 密钥加密简单消息,但我从我的程序中得到了一个奇怪的行为。
当我向两个 API 调用提供 128+ 字节长的消息时,一切都按预期工作。但是,当我提供比这更短的消息时,我收到一个 C000000D () 错误代码。BCryptEncrypt
STATUS_INVALID_PARAMETER
这是我使用的代码(没有所有的调试行)
//1024 bits public key from https://travistidwell.com/jsencrypt/demo/
CHAR key[216] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAriYPFSlSVPprEN0jcg+K+AHMNAanHsfYELEIIrMRirFkzNQ3Zk+YSf1d3qZqGSqmUk6iaawMIh55Usfavnv9DCJtRnE9uHe1F1cgMnm+dug3dzOAiiSb5aIO5JqTK6LCtR+ccubCtw4geUGEk+nN0e0nk6BxRzjRIRGd5bJWDQIDAQAB";
//Get the length of the future base64 decoded public key
DWORD dwBufferLen = 0;
CryptStringToBinaryA(key, 0, CRYPT_STRING_BASE64, NULL, &dwBufferLen, NULL, NULL);
//Allocate a new buffer and redirect the output base64 decoded key to it
LPBYTE pbBuffer = NULL;
pbBuffer = (LPBYTE)LocalAlloc(0, dwBufferLen);
CryptStringToBinaryA(key, 0, CRYPT_STRING_BASE64, pbBuffer, &dwBufferLen, NULL, NULL);
//Get the size of the public key BLOB
DWORD cbKeyBlob = 0;
CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, X509_PUBLIC_KEY_INFO, pbBuffer, dwBufferLen, 0, NULL, NULL, &cbKeyBlob);
//Allocate a new buffer and redirect the output key BLOB to it
LPBYTE pbKeyBlob = NULL;
pbKeyBlob = (LPBYTE)LocalAlloc(0, cbKeyBlob);
CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, X509_PUBLIC_KEY_INFO, pbBuffer, dwBufferLen, 0, NULL, pbKeyBlob, &cbKeyBlob);
//Import that key BLOB to a BCRYPT_KEY_HANDLE
BCRYPT_KEY_HANDLE hKey = NULL;
CryptImportPublicKeyInfoEx2(X509_ASN_ENCODING, pbKeyBlob, 0, 0, &hKey);
//Get the size of the future encrypted message
char* secretMsg = "Hello this message will be sent to Alice";
ULONG secretMsgSize = strlen(secretMsg);
ULONG dwOutputSize = 0;
BCryptEncrypt(hKey, secretMsg, secretMsg Size, NULL, NULL, 0, NULL, NULL, &dwOutputSize , 0);
//Create a new buffer and allocate the dwOutputSize to the Heap
ULONG encryptedBufferSize = 0
PUCHAR encryptedBuffer;
encryptedBuffer = (PUCHAR)HeapAlloc(GetProcessHeap(), 0, dwOutputSize);
BCryptEncrypt(hKey, secretMsg, secretMsgSize, NULL, NULL, 0, encryptedBuffer, dwOutputSize, &encryptedBufferSize, 0);
当我向我的第二个 BCryptEncrypt winapi 提供 128+ 字节长的消息时,一切按预期工作
但是,当我提供比这更短的消息时,我会得到一个C000000D
() 错误代码。STATUS_INVALID_PARAMETER
注意:我在第二个函数上出现错误BCryptEncrypt
注意 2 : 在这些行代码之后,当然有一个清理功能
答:
1赞
Roderick Forest
11/3/2023
#1
在一位好成员的帮助下,我设法通过将 1 个参数更改为 2 个 BCryptEncrypt API 来解决我的问题:
BCryptEncrypt(hKey, secretMsg, secretMsg Size, NULL, NULL, 0, NULL, NULL, &dwOutputSize , BCRYPT_PAD_PKCS1);
BCryptEncrypt(hKey, secretMsg, secretMsgSize, NULL, NULL, 0, encryptedBuffer, dwOutputSize, &encryptedBufferSize, BCRYPT_PAD_PKCS1);
评论
0赞
YangXiaoPo-MSFT
12/1/2023
你能标记一下这个吗?
评论
LPBYTE pbBuffer = NULL; LocalFree(pbBuffer);
LocalFree
LocalFree(NULL)
pbBuffer
LocalFree
LocalFree
LocalFree(pbKeyBlob)
BCRYPT_PAD_PKCS1
您需要将 last 参数用作 ,并且 src 代码代码也要进行优化BCryptEncrypt