Solved

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

Posted on 2004-03-28
4
290 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
Comment Utility
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
Comment Utility
Thaks maite i used unicode instead of utf8 and it worked
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now