Solved

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

Posted on 2004-03-28
4
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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)…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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