通过VB.NET进行RSA加密和解密
我正在制作一个程序,该程序将默认数据“ 2”放入SHA256算法中,然后将其加密到RSA,然后解密它,然后将其转换为字符串。我的SHA256没有问题,但是当它进入RSaencrypt和RsadeCrypt时,两个都有“不良长度”的错误。不长的时间是什么意思?为什么它是在“ 2”的哈希数据上特别发生的?另外,我还有私钥11,公共密钥5,模量14的RSA参数。有人对此有任何建议吗?感谢那些会给我帮助的人。
Imports System.Security.Cryptography
Imports System.Text
Module Program
Sub Main()
Dim m_privkey_tx As String = "11"
Dim m_pubkey_tx As String = "5"
Dim m_mod_tx As String = "14"
Dim m_message_tx As String = "2"
Dim m_hash_tx As Byte()
Dim oSHA256 As SHA256 = SHA256.Create
m_hash_tx = oSHA256.ComputeHash(Encoding.UTF8.GetBytes(m_message_tx))
Console.WriteLine("SHA256: " & Convert.ToBase64String(m_hash_tx))
Dim params As RSAParameters = New RSAParameters()
params.Exponent = Encoding.UTF8.GetBytes(m_pubkey_tx)
params.D = Encoding.UTF8.GetBytes(m_privkey_tx)
params.Modulus = Encoding.UTF8.GetBytes(m_mod_tx)
Console.WriteLine(Convert.ToString(RSAEncrypt(m_hash_tx, params, False)))
Console.WriteLine(Convert.ToString(RSADecrypt(m_hash_tx, params, False)))
Console.Read()
End Sub
Public Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal m_oeap_yn As Boolean) As Byte()
Try
Dim m_dec_bit() As Byte
Using RSA As New RSACryptoServiceProvider(1024)
RSA.ImportParameters(RSAKeyInfo)
m_dec_bit = RSA.Decrypt(DataToDecrypt, m_oeap_yn)
End Using
Return m_dec_bit
Catch ex As CryptographicException
Console.WriteLine(ex.Message())
Return Nothing
End Try
End Function
Public Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal m_oeap_yn As Boolean) As Byte()
Try
Dim m_enc_bit() As Byte
Using RSA As New RSACryptoServiceProvider(1024)
RSA.ImportParameters(RSAKeyInfo)
m_enc_bit = RSA.Encrypt(DataToEncrypt, m_oeap_yn)
End Using
Return m_enc_bit
Catch ex As CryptographicException
Console.WriteLine(ex.Message())
Return Nothing
End Try
End Function
End Module
I am making a program that hashes the default data "2" into SHA256 algorithm then encrypting it to RSA then decrypting it then re convert it to string. My SHA256 has no problem but when it goes to the RSAEncrypt and RSADecrypt, both had same error of "Bad Length". What does it mean by bad length and why does it happen specially on a hashed data of "2"? Also I have RSA parameters of Private Key 11, Public Key 5, Modulus 14. Anyone, got any advice for this? Appreciate those who'll give me assistance.
Imports System.Security.Cryptography
Imports System.Text
Module Program
Sub Main()
Dim m_privkey_tx As String = "11"
Dim m_pubkey_tx As String = "5"
Dim m_mod_tx As String = "14"
Dim m_message_tx As String = "2"
Dim m_hash_tx As Byte()
Dim oSHA256 As SHA256 = SHA256.Create
m_hash_tx = oSHA256.ComputeHash(Encoding.UTF8.GetBytes(m_message_tx))
Console.WriteLine("SHA256: " & Convert.ToBase64String(m_hash_tx))
Dim params As RSAParameters = New RSAParameters()
params.Exponent = Encoding.UTF8.GetBytes(m_pubkey_tx)
params.D = Encoding.UTF8.GetBytes(m_privkey_tx)
params.Modulus = Encoding.UTF8.GetBytes(m_mod_tx)
Console.WriteLine(Convert.ToString(RSAEncrypt(m_hash_tx, params, False)))
Console.WriteLine(Convert.ToString(RSADecrypt(m_hash_tx, params, False)))
Console.Read()
End Sub
Public Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal m_oeap_yn As Boolean) As Byte()
Try
Dim m_dec_bit() As Byte
Using RSA As New RSACryptoServiceProvider(1024)
RSA.ImportParameters(RSAKeyInfo)
m_dec_bit = RSA.Decrypt(DataToDecrypt, m_oeap_yn)
End Using
Return m_dec_bit
Catch ex As CryptographicException
Console.WriteLine(ex.Message())
Return Nothing
End Try
End Function
Public Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal m_oeap_yn As Boolean) As Byte()
Try
Dim m_enc_bit() As Byte
Using RSA As New RSACryptoServiceProvider(1024)
RSA.ImportParameters(RSAKeyInfo)
m_enc_bit = RSA.Encrypt(DataToEncrypt, m_oeap_yn)
End Using
Return m_enc_bit
Catch ex As CryptographicException
Console.WriteLine(ex.Message())
Return Nothing
End Try
End Function
End Module
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论