基于输入字节的 RSA 加密(使用 RSAEncryptionPadding.OaepSHA256)出现可解释的错误
In explicable error with RSA encrypt (with RSAEncryptionPadding.OaepSHA256) based on the input bytes
我在 windows 10 上使用 C# 和 .net core 3.1。
以下代码中的选项 1 成功加密了平面字节,但选项 2 在 Encrypt 方法上抛出错误。唯一的区别是输入字节。
private byte[] TestCode()
{
var cert = new X509Certificate2("<PEM-FILE-WITH-CERTIFICATE>");
var publicKey = cert.GetRSAPublicKey();
var plainConnectionInfo1 = new PlainConnectionInfo()
{
ConnectionStringWithoutPath = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
Path = "222222222222"
};
byte[] encryptedBytes = null;
byte[] plainBytes = null;
var option = 1;
if (option == 1)
{
//Option 1
plainBytes = UTF8Encoding.UTF8.GetBytes(plainConnectionInfo1.ConnectionStringWithoutPath);
encryptedBytes = publicKey.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);
}
else
{
//Option 2
var plainConnectionInfo1Json = JsonConvert.SerializeObject(plainConnectionInfo1);
plainBytes = UTF8Encoding.UTF8.GetBytes(plainConnectionInfo1Json);
encryptedBytes = publicKey.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);
}
return encryptedBytes;
}
选项 1 和选项 2 之间的唯一区别是选项 2 中的文字多了一点(与选项 1 相比)。选项 2 是一个 json 字符串,因为它只是选项 1 中的一个字符串。两个字符串在加密之前都被转换为 UTF8 编码字节。
选项 2 向我抛出以下错误:
Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException
HResult=0x80090027
Message=The parameter is incorrect.
Source=System.Security.Cryptography.Cng
StackTrace:
at System.Security.Cryptography.RSACng.EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan`1 input, AsymmetricPaddingMode paddingMode, Void* paddingInfo, Boolean encrypt)
at System.Security.Cryptography.RSACng.EncryptOrDecrypt(Byte[] data, RSAEncryptionPadding padding, Boolean encrypt)
at System.Security.Cryptography.RSACng.Encrypt(Byte[] data, RSAEncryptionPadding padding)
根据错误,我无法破译我做错了什么。有人可以解释一下吗?
我使用的是密钥大小为 2048 的 RSA public 密钥。使用此密钥大小,任何时候都只能加密 190 个字节。此处对此进行了全面解释:https://crypto.stackexchange.com/questions/42097/what-is-the-maximum-size-of-the-plaintext-message-for-rsa-oaep
我在 windows 10 上使用 C# 和 .net core 3.1。
以下代码中的选项 1 成功加密了平面字节,但选项 2 在 Encrypt 方法上抛出错误。唯一的区别是输入字节。
private byte[] TestCode()
{
var cert = new X509Certificate2("<PEM-FILE-WITH-CERTIFICATE>");
var publicKey = cert.GetRSAPublicKey();
var plainConnectionInfo1 = new PlainConnectionInfo()
{
ConnectionStringWithoutPath = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
Path = "222222222222"
};
byte[] encryptedBytes = null;
byte[] plainBytes = null;
var option = 1;
if (option == 1)
{
//Option 1
plainBytes = UTF8Encoding.UTF8.GetBytes(plainConnectionInfo1.ConnectionStringWithoutPath);
encryptedBytes = publicKey.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);
}
else
{
//Option 2
var plainConnectionInfo1Json = JsonConvert.SerializeObject(plainConnectionInfo1);
plainBytes = UTF8Encoding.UTF8.GetBytes(plainConnectionInfo1Json);
encryptedBytes = publicKey.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);
}
return encryptedBytes;
}
选项 1 和选项 2 之间的唯一区别是选项 2 中的文字多了一点(与选项 1 相比)。选项 2 是一个 json 字符串,因为它只是选项 1 中的一个字符串。两个字符串在加密之前都被转换为 UTF8 编码字节。
选项 2 向我抛出以下错误:
Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException
HResult=0x80090027
Message=The parameter is incorrect.
Source=System.Security.Cryptography.Cng
StackTrace:
at System.Security.Cryptography.RSACng.EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan`1 input, AsymmetricPaddingMode paddingMode, Void* paddingInfo, Boolean encrypt)
at System.Security.Cryptography.RSACng.EncryptOrDecrypt(Byte[] data, RSAEncryptionPadding padding, Boolean encrypt)
at System.Security.Cryptography.RSACng.Encrypt(Byte[] data, RSAEncryptionPadding padding)
根据错误,我无法破译我做错了什么。有人可以解释一下吗?
我使用的是密钥大小为 2048 的 RSA public 密钥。使用此密钥大小,任何时候都只能加密 190 个字节。此处对此进行了全面解释:https://crypto.stackexchange.com/questions/42097/what-is-the-maximum-size-of-the-plaintext-message-for-rsa-oaep