C# 使用充气城堡库时遇到问题

发布于 2024-08-22 14:45:08 字数 1836 浏览 4 评论 0原文

我正在尝试加密某些内容然后解密它。下面显示的代码是 VB,但我将其与 C# 库一起使用。我在 C# 中尝试了相同的代码,并抛出了完全相同的异常。

我使用 RSA 引擎和 StreamReader 中的私钥文件对文本进行加密。将其写入控制台工作正常,但是当我尝试解密它时会抛出无效的块长度异常:

Module Module1

    Sub Main()
        Dim input As String
        Dim output As String

        input = "Feb Twenty-Fourth Two Thousand Ten"
        output = EncryptKey(input)
        Console.WriteLine(DecryptKey(output) <---Error!

    End Sub

    Private Function EncryptKey(ByVal sPlain As String) As String
        Dim enc As New System.Text.UTF8Encoding
        Dim result As Byte()
        result = BouncyCastleCrypto(True, enc.GetBytes(sPlain))
        Return Convert.ToBase64String(result)
    End Function

    Private Function BouncyCastleCrypto(ByVal bForEncryption As Boolean, ByVal input As Byte()) As Byte()
        Dim bValue64 As Byte() = {0}
        Try
            Dim keypair As Greenway.PrimeResearch.Encryption.Crypto.AsymmetricCipherKeyPair
            Dim sr As New StreamReader("C:\Documents and Settings\xxxx\Desktop\test.KEY")
            keypair = New Greenway.PrimeResearch.Encryption.OpenSsl.PemReader(sr).ReadObject
            Dim cryptEngine As New Greenway.PrimeResearch.Encryption.Crypto.Encodings.Pkcs1Encoding(New Greenway.PrimeResearch.Encryption.Crypto.Engines.RsaEngine())
            cryptEngine.Init(bForEncryption, keypair.Private)
            bValue64 = cryptEngine.ProcessBlock(input, 0, input.Length)
        Catch ex As Exception
            Throw ex
        End Try

        Return bValue64
    End Function

    Private Function DecryptKey(ByVal sCipher As String) As String
        Dim enc As New System.Text.ASCIIEncoding
        Dim result As Byte()
        result = BouncyCastleCrypto(False, Convert.FromBase64String(sCipher))
        Return enc.GetString(result)
    End Function
End Module

I'm attempting to encrypt something then decrypt it. The code shown below is VB but I'm using it with the C# libraries. I've tried the same code in C# as well with the exact same exception thrown.

I'm encrypting the text using the RSA engine and the private key file from a StreamReader. Writing that to the console works fine, but the invalid block length exception is thrown when I try to decrypt it:

Module Module1

    Sub Main()
        Dim input As String
        Dim output As String

        input = "Feb Twenty-Fourth Two Thousand Ten"
        output = EncryptKey(input)
        Console.WriteLine(DecryptKey(output) <---Error!

    End Sub

    Private Function EncryptKey(ByVal sPlain As String) As String
        Dim enc As New System.Text.UTF8Encoding
        Dim result As Byte()
        result = BouncyCastleCrypto(True, enc.GetBytes(sPlain))
        Return Convert.ToBase64String(result)
    End Function

    Private Function BouncyCastleCrypto(ByVal bForEncryption As Boolean, ByVal input As Byte()) As Byte()
        Dim bValue64 As Byte() = {0}
        Try
            Dim keypair As Greenway.PrimeResearch.Encryption.Crypto.AsymmetricCipherKeyPair
            Dim sr As New StreamReader("C:\Documents and Settings\xxxx\Desktop\test.KEY")
            keypair = New Greenway.PrimeResearch.Encryption.OpenSsl.PemReader(sr).ReadObject
            Dim cryptEngine As New Greenway.PrimeResearch.Encryption.Crypto.Encodings.Pkcs1Encoding(New Greenway.PrimeResearch.Encryption.Crypto.Engines.RsaEngine())
            cryptEngine.Init(bForEncryption, keypair.Private)
            bValue64 = cryptEngine.ProcessBlock(input, 0, input.Length)
        Catch ex As Exception
            Throw ex
        End Try

        Return bValue64
    End Function

    Private Function DecryptKey(ByVal sCipher As String) As String
        Dim enc As New System.Text.ASCIIEncoding
        Dim result As Byte()
        result = BouncyCastleCrypto(False, Convert.FromBase64String(sCipher))
        Return enc.GetString(result)
    End Function
End Module

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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