Reputation: 1116
I'm trying to use BCrypt crypto operation (BCryptEncrypt, BCryptDecrypt). Before running encryption, decryption, I wanted to export and import the key.
I'm getting an error when trying to use 'BCryptExportKey'.
Found status code was 3221225659
(Hex - C00000BB
).
This NTSTATUS code means "The request is not supported." (0xC00000BB - STATUS_NOT_SUPPORTED)
Ref : NTSTATUS
But I can't figure out why this problem occurred. I haven't found any resources/documentation related to this problem. Could anyone help me with this?
Code:
using System.Runtime.InteropServices;
using System.Text;
namespace TestConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
ExportImportKey();
}
internal static void ExportImportKey()
{
string keyString = "12345678abcdefgh"; // 16 byte
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] exportedKey = ExportKey(key);
}
internal static byte[] ExportKey(byte[] key)
{
IntPtr phAlgorithm = IntPtr.Zero;
IntPtr hKey = IntPtr.Zero;
byte[] exportedKey = null;
uint statusCode = BCryptOpenAlgorithmProvider(out phAlgorithm, "AES", "Microsoft Primitive Provider", 0);
statusCode = BCryptGenerateSymmetricKey(phAlgorithm, out hKey, IntPtr.Zero, 0, key, key.Length, 0);
int keySize = 0;
statusCode = BCryptExportKey(hKey, IntPtr.Zero, "ECCPUBLICBLOB", null, 0, out keySize, 0);
exportedKey = new byte[keySize];
statusCode = BCryptExportKey(hKey, IntPtr.Zero, "KeyDataBlob", exportedKey, exportedKey.Length, out keySize, 0);
return exportedKey;
}
[DllImport("bcrypt.dll")]
public static extern uint BCryptOpenAlgorithmProvider(out IntPtr phAlgorithm, [MarshalAs(UnmanagedType.LPWStr)] string pszAlgId, [MarshalAs(UnmanagedType.LPWStr)] string pszImplementation, uint dwFlags);
[DllImport("bcrypt.dll")]
public static extern int BCryptCloseAlgorithmProvider(IntPtr phAlgorithm, int dwFlags);
[DllImport("bcrypt.dll")]
public static extern uint BCryptExportKey(IntPtr hKey, IntPtr hExportKey, string pszBlobType, byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags);
[DllImport("bcrypt.dll")]
public static extern uint BCryptGenerateSymmetricKey(IntPtr phAlgorithm, out IntPtr phKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbSecret, int cbSecret, int dwFlags);
}
}
The provided status code was found after calling the first 'BCryptExportKey' in the given code.
Upvotes: 1
Views: 55