snejk85
snejk85

Reputation: 21

"The parameter is incorrect" exception when encrypting array using RSACng 512 + OaepSHA256

I'm having trouble using RSACng (512) with OaepSHA256 (C#, .NET Framework 4.6.1) - getting System.Security.Cryptography.CryptographicException: 'The parameter is incorrect.' while encoding (any) byte array:

using System.Security.Cryptography;
...

using (RSACng rsaCng = new RSACng(512))
{
    var result = rsaCng.Encrypt(new byte[1], RSAEncryptionPadding.OaepSHA256); // throws ex
}
System.Security.Cryptography.CryptographicException: 'The parameter is incorrect.'
This exception was originally thrown at this call stack:
    System.Security.Cryptography.NCryptNative.EncryptData<T>(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle, byte[], ref T, System.Security.Cryptography.AsymmetricPaddingMode, System.Security.Cryptography.NCryptNative.NCryptEncryptor<T>)
    System.Security.Cryptography.NCryptNative.EncryptDataOaep(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle, byte[], string)
    System.Security.Cryptography.RSACng.Encrypt(byte[], System.Security.Cryptography.RSAEncryptionPadding)
    MiniJavaCertTest.Program.Test() in Program.cs
    MiniJavaCertTest.Program.Main() in Program.cs

If I use 1024 key size, it works:

using (RSACng rsaCng = new RSACng(1024))
{
    var result = rsaCng.Encrypt(new byte[1], RSAEncryptionPadding.OaepSHA256); // works ok
}

It also works if 512 key size is used, but with OaepSHA1 instead of OaepSHA256:

using (RSACng rsaCng = new RSACng(512))
{
    var result = rsaCng.Encrypt(new byte[1], RSAEncryptionPadding.OaepSHA1); // works ok
}

Any idea why doesn't this work and how to get it to work for 512 + OaepSHA256?

Upvotes: 2

Views: 2608

Answers (0)

Related Questions