Link to home
Start Free TrialLog in
Avatar of atap
atap

asked on

Problem with decrypting string in Vb.net throws System.cryptography exception " Bad Data"

Hey guys here is my code for the encryption and decryption class, when i encrypt it works fine but in decryption it throws a "CryptographyException bad data" it happens when I try to read the stream and return the decypted string the code is as below. thank in advance

Imports System
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class SymmetricCrypto
  Dim myCryptoService As New RC2CryptoServiceProvider
  Private Function GetLegalKey(ByVal key As String) As Byte()
    Dim tempString As String
    If myCryptoService.LegalKeySizes.Length > 0 Then
      Dim lessSize, moreSize As Integer
      lessSize = 0
      moreSize = myCryptoService.LegalKeySizes(0).MinSize
      While key.Length * 8 > moreSize
        lessSize = moreSize
        moreSize += myCryptoService.LegalKeySizes(0).SkipSize
      End While
      tempString = key.PadRight(moreSize / 8, " ")
    Else
      tempString = key
    End If
    Return ASCIIEncoding.ASCII.GetBytes(tempString)
  End Function
  Public Function Encrypt(ByVal SourceString As String, ByVal key As String) As String
    Dim byteIn() As Byte
    Dim byteKey() As Byte
    Dim byteout() As Byte
    Dim mStream As New System.IO.MemoryStream

    byteIn = System.Text.Encoding.ASCII.GetBytes(SourceString)
    byteKey = GetLegalKey(key)

    myCryptoService.Key = byteKey
    myCryptoService.IV = byteKey

    Dim encrypto As ICryptoTransform
    encrypto = myCryptoService.CreateEncryptor

    Dim CStream As New CryptoStream(mStream, encrypto, CryptoStreamMode.Write)
    CStream.Write(byteIn, 0, byteIn.Length)
    CStream.FlushFinalBlock()

    byteout = mStream.GetBuffer()
    Dim i As Integer
    For i = 0 To byteout.Length
      If byteout(i) = 0 Then
        Exit For
      End If
    Next
    Return System.Convert.ToBase64String(byteout)
  End Function

  Public Function Decrypt(ByVal EncryptedString As String, ByVal key As String) as string
    Dim byteIn() As Byte
    Dim byteKey() As Byte

    byteIn = System.Convert.FromBase64String(EncryptedString)
    Dim mStream As New System.IO.MemoryStream(byteIn, 0, byteIn.Length)
      byteKey = GetLegalKey(key)

      myCryptoService.Key = byteKey
      myCryptoService.IV = byteKey

      Dim encrypto As ICryptoTransform = myCryptoService.CreateDecryptor
      Dim cStream As New CryptoStream(mStream, encrypto, CryptoStreamMode.Read)
      Dim encoding As System.Text.Encoding
      Dim Sreader As New System.IO.StreamReader(cStream, encoding.ASCII)
      Dim decryptedstring As String
      decryptedstring = Sreader.ReadToEnd
    Return decryptedstring

  End Function
End Class
Avatar of DonRameshSachin
DonRameshSachin
Flag of United States of America image

hi,
why dont u try to send the encrypted string as a memory stream and try to convert that as a byte array and check whether ur string is decrypted or not

Don
Avatar of atap
atap

ASKER

Thaks maite i used unicode instead of utf8 and it worked
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial