none
Decryption RRS feed

  • Question

  •  
    Hi All,

    I am trying to take a string that has been encrypted and decrypt it.
    Here is the string:   MMP61ubfQt4=

    The following code runs, but the result is an empty string at the
    end.  Can anyone see what I am doing wrong?

    Thanks,
    Jeff

     

    Public Shared Function decryptUser(ByVal userName As String) As String

        Dim inputInBytes() As Byte = Convert.FromBase64String(userName)
        Dim key() As Byte = Convert.FromBase64String("ABCDEFG/LR3t49HgyCUXns9nfCpaAr0q")
        Dim IV() As Byte = Convert.FromBase64String("ABCDEFGlS/U=")
        
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding
        
    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(key, IV)
        
    Dim decryptedStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write) 
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        decryptedStream.Position = 0
        
    Dim result(decryptedStream.Length - 1) As Byte
        decryptedStream.Read(result, 0, decryptedStream.Length) 
        cryptStream.Close()
        
    Dim myutf As UTF8Encoding = New UTF8Encoding
        
    Return myutf.GetString(result)
    End Function

    Monday, August 25, 2008 10:01 PM

All replies

  • Follow this link: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=107 it looks like your encoding may be an issue.
    Sunday, November 16, 2008 1:26 AM
  • Here's a more streamlined version of your function, that also makes sure you aren't killing the buffer before you can retrieve the contents:

    Public Shared Function decryptUser(ByVal userName As StringAs String 
     
        Dim inputInBytes() As Byte = Convert.FromBase64String(userName)  
        Dim key() As Byte = Convert.FromBase64String("ABCDEFG/LR3t49HgyCUXns9nfCpaAr0q")  
        Dim IV() As Byte = Convert.FromBase64String("ABCDEFGlS/U=")  
     
        Using tdesProvider As New TripleDESCryptoServiceProvider  
            Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(key, IV)  
            Using decryptedStream As New MemoryStream  
                Using cryptStream As New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)  
                    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)  
                    cryptStream.FlushFinalBlock()  
                    Return Encoding.UTF8.GetString(decryptedStream.ToArray())  
                End Using  
            End Using  
        End Using  
    End Function 

    After testing things though, it looks like your input is resulting in nothing being assigned to the buffer. So it looks like you have a problem in the function that does the encryption. It should look like something like this:

    Public Shared Function encryptUser(ByVal userName As StringAs String 
        Dim inputInBytes() As Byte = Encoding.UTF8.GetBytes(userName)  
        Dim key() As Byte = Convert.FromBase64String("ABCDEFG/LR3t49HgyCUXns9nfCpaAr0q")  
        Dim IV() As Byte = Convert.FromBase64String("ABCDEFGlS/U=")  
     
        Using tdesProvider As New TripleDESCryptoServiceProvider  
            Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(key, IV)  
            Using decryptedStream As New MemoryStream  
                Using cryptStream As New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)  
                    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)  
                    cryptStream.FlushFinalBlock()  
                    Return Convert.ToBase64String(decryptedStream.ToArray())  
                End Using  
            End Using  
        End Using  
    End Function 

    By the way, it's not a good idea to store passwords in code, or to display them on the web :-) Hopefully, this was just test data.
    -Rob Teixeira
    Monday, November 17, 2008 9:32 PM