Solved

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

Posted on 2004-03-28
4
299 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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