通过VB.NET进行RSA加密和解密

发布于 2025-01-17 08:42:05 字数 2114 浏览 3 评论 0原文

我正在制作一个程序,该程序将默认数据“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文