Solved

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

Posted on 2004-03-28
4
301 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:atap
4 Comments
 
LVL 2

Expert Comment

by:DonRameshSachin
ID: 10701445
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
0
 

Author Comment

by:atap
ID: 10701577
Thaks maite i used unicode instead of utf8 and it worked
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 11102735
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question