Trouble decoding text using RSA and bouncy castle libraries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • westwolf2010
    New Member
    • Feb 2010
    • 1

    Trouble decoding text using RSA and bouncy castle libraries

    The code segment below is in VB.net and referencing C# Bouncy Castle libraries. I get the same results when the VB code is in C#. Exact same Console output of EncryptKey and exact same "incorrect block type" exception with DecryptKey, so I don't think the language here should matter too much. The test.KEY file is a private key file we're using.

    Code:
    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
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Line 7 is going to need another right parenthesis.

    Aren't BouncyCastles those inflatables you rent for a 10 year old's birthday party?

    Did you try contacting whomever created "BouncyCast le"? I would expect they would be more help for their product than someone NOT familiar with the product.

    Try breaking down your consolidated statements so you can see each value before use:

    Code:
    result = BouncyCastleCrypto(False, Convert.FromBase64String(sCipher))
    First to the Convert portion and set it to a variable, then use that variable. This way you can see what is being sent as a parameter, when you walk through the code.

    Code:
    string Converted = Convert.FromBase64String(sCipher);
    result = BouncyCastleCrypto(False, Converted)
    Put a breakpoint at line 35 and follow it through line-by-line, watching the Locals and Autos pallets. Maybe something will come up from there.

    Comment

    Working...