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.Cryptograp hy
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.LegalKeySi zes.Length > 0 Then
Dim lessSize, moreSize As Integer
lessSize = 0
moreSize = myCryptoService.LegalKeySi zes(0).Min Size
While key.Length * 8 > moreSize
lessSize = moreSize
moreSize += myCryptoService.LegalKeySi zes(0).Ski pSize
End While
tempString = key.PadRight(moreSize / 8, " ")
Else
tempString = key
End If
Return ASCIIEncoding.ASCII.GetByt es(tempStr ing)
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( SourceStri ng)
byteKey = GetLegalKey(key)
myCryptoService.Key = byteKey
myCryptoService.IV = byteKey
Dim encrypto As ICryptoTransform
encrypto = myCryptoService.CreateEncr yptor
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.ToBase64Str ing(byteou t)
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.FromBase64S tring(Encr yptedStrin g)
Dim mStream As New System.IO.MemoryStream(byt eIn, 0, byteIn.Length)
byteKey = GetLegalKey(key)
myCryptoService.Key = byteKey
myCryptoService.IV = byteKey
Dim encrypto As ICryptoTransform = myCryptoService.CreateDecr yptor
Dim cStream As New CryptoStream(mStream, encrypto, CryptoStreamMode.Read)
Dim encoding As System.Text.Encoding
Dim Sreader As New System.IO.StreamReader(cSt ream, encoding.ASCII)
Dim decryptedstring As String
decryptedstring = Sreader.ReadToEnd
Return decryptedstring
End Function
End Class
Imports System
Imports System.Security.Cryptograp
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.LegalKeySi
Dim lessSize, moreSize As Integer
lessSize = 0
moreSize = myCryptoService.LegalKeySi
While key.Length * 8 > moreSize
lessSize = moreSize
moreSize += myCryptoService.LegalKeySi
End While
tempString = key.PadRight(moreSize / 8, " ")
Else
tempString = key
End If
Return ASCIIEncoding.ASCII.GetByt
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
byteKey = GetLegalKey(key)
myCryptoService.Key = byteKey
myCryptoService.IV = byteKey
Dim encrypto As ICryptoTransform
encrypto = myCryptoService.CreateEncr
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.ToBase64Str
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.FromBase64S
Dim mStream As New System.IO.MemoryStream(byt
byteKey = GetLegalKey(key)
myCryptoService.Key = byteKey
myCryptoService.IV = byteKey
Dim encrypto As ICryptoTransform = myCryptoService.CreateDecr
Dim cStream As New CryptoStream(mStream, encrypto, CryptoStreamMode.Read)
Dim encoding As System.Text.Encoding
Dim Sreader As New System.IO.StreamReader(cSt
Dim decryptedstring As String
decryptedstring = Sreader.ReadToEnd
Return decryptedstring
End Function
End Class
ASKER
Thaks maite i used unicode instead of utf8 and it worked
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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