我正在尝试使用 AES128 将字符串加密为 8 字节(64 位)的大小wincrypt.h
,请注意该字符串小于 128 位的 AES 块大小。
HCRYPTKEY hKey;
if (!CryptDeriveKey(hProv, CALG_AES_128, hHash, 0, &hKey)) {
dwStatus = GetLastError();
printf("CryptDeriveKey failed: %x\n", dwStatus);
CryptReleaseContext(hProv, 0);
system("pause");
return dwStatus;
}
printf("[+] CryptDeriveKey Success\n");
const size_t string_size = 8;
BYTE string[8] = { "Foooooo" }; // \0 counts as 1 byte
DWORD out_len = 8;
if (!CryptEncrypt(hKey, NULL, TRUE, 0, string, &out_len, string_size)) {
printf("[-] CryptEncrypt failed\n");
}
printf("%s", string);
printf("\n");
if (!CryptDecrypt(hKey, NULL, TRUE, 0, string, &out_len)) {
printf("[-] CryptDecrypt failed\n");
}
printf("%s", string);
printf("\n");
但它看起来不能很好地加密/解密,因为我得到了这个输出:
[+] CryptDeriveKey Success
Fooooooo
[-] CryptEncrypt failed
ÉÆ╠1╔P█ídhù;$§┴
[-] CryptDecrypt failed
我做错了什么?&out_len
或者string_size
应该是 128 作为 AES 块大小?