我在使用带有 OaepSHA256(C#,.NET Framework 4.6.1)的 RSACng(512)时遇到问题 - 获取 System.Security.Cryptography.CryptographicException:“参数不正确。” 在编码(任何)字节数组时:
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
如果我使用 1024 密钥大小,它可以工作:
using (RSACng rsaCng = new RSACng(1024))
{
var result = rsaCng.Encrypt(new byte[1], RSAEncryptionPadding.OaepSHA256); // works ok
}
如果使用 512 密钥大小,它也可以工作,但使用 OaepSHA1 而不是 OaepSHA256:
using (RSACng rsaCng = new RSACng(512))
{
var result = rsaCng.Encrypt(new byte[1], RSAEncryptionPadding.OaepSHA1); // works ok
}
知道为什么这不起作用以及如何使其适用于 512 + OaepSHA256?