Encryption padding error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Ross
    New Member
    • Jan 2007
    • 119

    #1

    Encryption padding error

    I am trying to ecrypt passwords to store them in a DB but I want to store them in String(nvarchar ) format. So I have written this class

    Code:
        Public Class PasswordEncryption
            Private IV As Byte() = Convert.FromBase64String("pl21q5lX3KvPdKEyss7Za0i2nEdfzfbHvbsxlsKCWbU=")
            Private pKey As Byte() = Convert.FromBase64String("1taYM5kPvxIdvNwBcuQefdiKMlzgqpdgiP/BjG4anVw=")
            Dim Cipher As RijndaelManaged
            Public Sub New()
                Cipher = New RijndaelManaged
                Cipher.KeySize = 256
                Cipher.BlockSize = 256
                Cipher.Mode = CipherMode.CBC
                Cipher.Key = pKey
                Cipher.IV = IV
            End Sub
            Public Function EncryptMessage(ByVal plainText As String) As String
                Dim BR As Byte() = Encoding.Unicode.GetBytes(plainText)
                Dim transform As ICryptoTransform = Cipher.CreateEncryptor()
                Dim cipherText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)
                Return Convert.ToBase64String(Cipher.Key)
            End Function
            Public Function DecryptMessage(ByVal cipherText As String) As String
                Dim BR As Byte() = Convert.FromBase64String(cipherText)
                Dim transform As ICryptoTransform = Cipher.CreateDecryptor()
                Dim plainText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)
                Return Convert.ToBase64String(plainText)
            End Function
        End Class
    But every time I try


    Code:
                Dim Enc As New PasswordEncryption
                Dim PassEnc As String = Enc.EncryptMessage("hello")
                Dim PassUnEnc As String = Enc.DecryptMessage(PassEnc)
    I get "Padding is invalid and cannot be converted" Anyone know how I can fix this?
    Tried different padding modes but same error everytime.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I would try to debug until the exact spot where it fails to have the padding, then examine the strings and see where it is getting lost?
    What you have seems sound enough, assuming what you're using is a 2-way ciper

    Comment

    • Bob Ross
      New Member
      • Jan 2007
      • 119

      #3
      I can't examine the strings because I am unable to read encrypted text.

      It is getting stuck when it trys to decrypt the string this line
      Code:
      Dim plainText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        EDIT:
        Actually, your encrypt function is only returning the key
        Code:
        Return Convert.ToBase64String(Cipher.Key)
        Which you can't decrypt.
        You should be returning, I *believe* they key AND the encrypted message or at lease the encrypted message.

        Comment

        Working...