Edit: To bear clear, the encrypted buffer was encrypted with the private key. I am trying to decrypt, with the public key, data that was encrypted with the private key.
I’m trying to perform RSA decryption with a public key using the BCrypt API, but BCryptDecrypt always returns STATUS_INVALID_PARAMETER. Does BCrypt not allow decrypt with a public key?
Some error checking and resource cleanup removed for brevity. BCryptDecrypt is where I receive a failure status.
BCRYPT_ALG_HANDLE bcrypt_alg = nullptr;
BCRYPT_KEY_HANDLE bcrypt_pub_key = nullptr;
NTSTATUS status = 0;
status = BCryptOpenAlgorithmProvider(
&bcrypt_alg,
BCRYPT_RSA_ALGORITHM,
nullptr,
0);
status = BCryptImportKeyPair(
bcrypt_alg,
nullptr,
BCRYPT_PUBLIC_KEY_BLOB,
&bcrypt_pub_key,
reinterpret_cast<unsigned char *>(&rsa_pub_key),
sizeof(rsa_pub_key),
0);
BCRYPT_PKCS1_PADDING_INFO padding_info = { 0 };
ULONG output_size = 0;
status = BCryptDecrypt(
bcrypt_pub_key,
(PUCHAR)encrypted,
(ULONG)encrypted_size,
&padding_info,
nullptr,
0,
nullptr,
0,
&output_size,
BCRYPT_PAD_PKCS1);
if (!BCRYPT_SUCCESS(status)) {
printf("Failed to decrypt, error 0x%xn", status);
return false;
}
unsigned __int8 * decrypted = new unsigned __int8[output_size];
status = BCryptDecrypt(
bcrypt_pub_key,
(PUCHAR)encrypted,
(ULONG)encrypted_size,
&padding_info,
nullptr,
0,
decrypted,
output_size,
&output_size,
BCRYPT_PAD_PKCS1);
if (!BCRYPT_SUCCESS(status)) {
printf("Failed to decrypt, error 0x%xn", status);
return false;
}
...
22