與RSA加密解密我用下面的命令使用的OpenSSL在Linux機器的CMD線創建了一個公鑰和私鑰能夠解密已經用公鑰加密的VB.NET中的數據。在VB.NET
我正在使用the MSDN documentation中的方法進行解密。我試圖設置RSAParameters傳遞給RSADecrypt
方法,但我不確定每個字段的內容。
每當我嘗試運行代碼我得到以下錯誤:
System.Security.Cryptography.CryptographicException: Bad Data. at System.Security.Cryptography.CryptographicException.ThrowCryptographicExce ption(Int32 hr) at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 k eyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey) at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSA Parameters parameters) at Decryptor.Module1.RSADecrypt(Byte[] DataToDecrypt, RSAParameters RSAKeyInf o, Boolean DoOAEPPadding) in C:\Users\yhorowitz\AppData\Local\Temporary Projects \Decryptor\Module1.vb:line 30
的誤差在RSADecrypt
方法
這被扔在這條線上RSA.ImportParameters(RSAKeyInfo)
是代碼我到目前爲止:
Imports System.Security.Cryptography
Imports System.Text
Module Module1
Dim encoder As New UTF8Encoding
Sub Main()
Dim MyPrivateKey As String = "" 'Removed
Dim MyPublicKey As String = "" 'Removed
Dim MyModulus as String = "" 'Removed
Dim DataToDecrypt() As Byte = Convert.FromBase64String("CourNHBC55DgGtBZU6Ahtm0emywGi5hWo5/h9zD6A/NASKMpZ/A5GCU8G5TNTMgJQxVFsabbdeuNhf4VQzBuFqewuD8eD7MwpJvjmuPfrs7xcEzOrwbF549v0PHv/nfN+03winW6s3ecnv1dm0TctQgqsauEuvXu2PMVEFivqPo=")
Dim params As RSAParameters = New RSAParameters()
params.Exponent = Convert.FromBase64String(MyPublicKey)
params.D = Convert.FromBase64String(MyPrivateKey)
params.Modulus = Convert.FromBase64String(MyModulus)
Console.WriteLine(Convert.ToString(RSADecrypt(DataToDecrypt, params, False)))
Console.Read()
End Sub
Public Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
Try
Dim decryptedData() As Byte
'Create a new instance of RSACryptoServiceProvider.
Using RSA As New RSACryptoServiceProvider(1024)
RSA.ImportParameters(RSAKeyInfo)
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
End Using
Return decryptedData
Catch e As CryptographicException
Console.WriteLine(e.ToString())
Return Nothing
End Try
End Function
End Module