问题描述
我在这里有以下代码:
let rsa = RSA.Create(2048)
let msg : byte[] = someBytes // 'someBytes' are just some bytes,not relevant for this example
let publicKey1 : byte[] = someBytes
let privateKey1 : byte[] = someBytes
let publicKey2 : byte[] = someBytes
let privateKey2 : byte[] = someBytes
rsa.ImportRSAPublicKey(ReadOnlySpan<byte>(publicKey1),ref 2048)
rsa.ImportRSAPrivateKey(ReadOnlySpan<byte>(privateKey2),ref 2048)
let encryptedMsg = rsa.Encrypt(msg,RSAEncryptionPadding.OaepSHA256)
rsa.ImportRSAPrivateKey(ReadOnlySpan<byte>(privateKey1),ref 2048)
let decryptedMsg = rsa.Decrypt(encryptedMsg,RSAEncryptionPadding.OaepSHA256)
运行它时,出现以下错误:
Unhandled exception. Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The parameter is incorrect.
at System.Security.Cryptography.RSAImplementation.RSACng.EncryptOrDecrypt(SafeNCryptKeyHandle key,ReadOnlySpan`1 input,AsymmetricPaddingMode paddingMode,Void* paddingInfo,Boolean encrypt)
at System.Security.Cryptography.RSAImplementation.RSACng.EncryptOrDecrypt(Byte[] data,RSAEncryptionPadding padding,Boolean encrypt)
at System.Security.Cryptography.RSAImplementation.RSACng.Decrypt(Byte[] data,RSAEncryptionPadding padding)
at Program(Byte[] msg,Byte[] key,Byte[] iv) in ...
at Program.main(String[] _arg1) in ...
当我删除rsa.ImportRSAPrivateKey(ReadOnlySpan<byte>(privateKey2),ref 2048)
行时,代码运行正常。
我的问题是,为什么在第一种情况下,加密还某种程度上取决于私钥,而当我解密时,会出现此错误?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)