程序挂起在对Wincrypt函数的函数调用中
Program hangs in a function call to Wincrypt functions
你能帮我处理这个代码吗?调用函数AesDecrypt
时挂起。
#include <iostream>
#include <Windows.h>
#include <wincrypt.h>
struct AesKey {
BLOBHEADER Header;
DWORD dwKeyLength;
BYTE cbKey[16];
AesKey() {
ZeroMemory(this, sizeof(*this));
Header.bType = PLAINTEXTKEYBLOB;
Header.bVersion = CUR_BLOB_VERSION;
Header.reserved = 0;
Header.aiKeyAlg = CALG_AES_128;
dwKeyLength = 16;
}
};
void AesDecrypt(unsigned char *output, unsigned char *input, int inLen, unsigned char *key, unsigned char *iv, int &plainSize) {
HCRYPTPROV provider;
AesKey rawKey;
HCRYPTKEY cKey;
BOOL hr = CryptAcquireContext(&provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0);
if (hr == FALSE)
throw "Unable to acquire AES Context";
for (int i = 0; i < 16; i++)
rawKey.cbKey[i] = key[i];
hr = CryptImportKey(provider, (BYTE *) &rawKey, sizeof(AesKey), NULL, 0, &cKey);
if (hr == FALSE)
throw "Unable to import given key";
hr = CryptSetKeyParam(cKey, KP_IV, (BYTE *) iv, 0);
if (hr == FALSE)
throw "Unable to set IV";
DWORD dwMode = CRYPT_MODE_CBC;
hr = CryptSetKeyParam(cKey, KP_MODE, (BYTE*) &dwMode, 0);
if (hr == FALSE)
throw "Unable to set mode";
memcpy(output, input, inLen);
DWORD d = (DWORD) inLen;
hr = CryptDecrypt(cKey, NULL, TRUE, 0, output, &d);
if (hr == FALSE)
{
int err = GetLastError();
throw "Error during Decryption";
}
plainSize = d;
}
// codigo generado por mi
int main(int argc, char** argv) {
unsigned char input[] = "SE83loTjmMeaG9+xoIyjng==";
unsigned char *pinput = input;
unsigned char output[] = "SE83loTjmMeaG9+xoIyjng==";
unsigned char *poutput = output;
unsigned char iv[] = "1234567890ABCDEF";
unsigned char *piv = iv;
unsigned char key[] = "1234567890ABCDEF";
unsigned char *pkey = key;
int plaInt;
AesDecrypt(poutput, pinput, 24, pkey, piv, plaInt);
std::cout << output << std::endl;
return 0;
}
调用函数时代码崩溃,我认为我传递指针是做错了什么。
抛出"char const*"实例后调用terminate
此应用程序已请求运行时在不同寻常的方式。请联系应用程序的支持团队以了解更多信息信息
--------------------------------进程已退出,返回值为255按任意键继续。
我怀疑这一行是罪魁祸首。
hr = CryptImportKey(provider, (BYTE *) &rawKey, sizeof(AesKey), NULL, 0, &cKey);
你可能打算使用
hr = CryptImportKey(provider, rawKey.cbKey, sizeof(AesKey), NULL, 0, &cKey);
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 从 Android JNI/NDK 代码中的C++函数调用 C 函数
- 如何从前面定义的另一个函数调用函数模板
- 为什么我们不能从具有默认参数的函数调用函数?
- 从其他函数调用函数或将函数作为数组的参数是一种好做法还是坏做法
- 使用c++中定义的头函数调用函数
- 从FORTRAN代码调用的c++函数调用c++函数
- 从函数调用函数时出错"identifier not found"
- 函数A调用函数B,函数B调用函数A,你怎么称呼它
- 函数A调用函数B,反之亦然(为什么不起作用)
- 当C++函数参数列表中有String类型时,我可以使用C程序函数调用C++函数吗
- 从静态函数调用函数指针
- 从 Objective-C 函数调用C++函数不起作用
- 从c++ /CLI函数调用c#函数