Home > Enterprise >  unexpected character on base64 encode/decode
unexpected character on base64 encode/decode

Time:02-02

I'm using following encoder and decoder to save a password:

Public Function Encrypt(msg As String) As String
        Try
            If String.IsNullOrEmpty(msg) = False Then
                Using cryptic As DESCryptoServiceProvider = New DESCryptoServiceProvider()
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes("Something")
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes("Something")

                    Using ms As New MemoryStream()
                        Using cs As New CryptoStream(ms, cryptic.CreateEncryptor(), CryptoStreamMode.Write)
                            Using sw As New BinaryWriter(cs)
                                sw.Write(msg)
                            End Using

                            Return Convert.ToBase64String(ms.ToArray())
                        End Using
                    End Using
                End Using
            End If
        Catch ex As Exception
            ExportLog(ex)
        End Try

        Return ""
    End Function

    Public Function Decrypt(msg As String) As String
        Try
            If String.IsNullOrEmpty(msg) = False Then
                Using cryptic As New DESCryptoServiceProvider()
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes("Something")
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes("Something")

                    Using ms As New MemoryStream()
                        Using cs As New CryptoStream(ms, cryptic.CreateDecryptor(), CryptoStreamMode.Write)
                            Using sw As New BinaryWriter(cs)
                                sw.Write(Convert.FromBase64String(msg))
                            End Using

                            Return Encoding.UTF8.GetString(ms.ToArray())
                        End Using
                    End Using
                End Using
            End If
        Catch ex As Exception
            ExportLog(ex)
        End Try

        Return ""
    End Function

The problem is that after decoding, I have one random character at the beginning of the string, often a character located at the beginning of the ASCII table (like STX, EOT...). For example I get ChrW(7) & "MyPassword" instead of "MyPassword".

I've also tried to ensure that the encoded password is a multiple of 4 but I have the same issue.

CodePudding user response:

I would follow the example from MS calling the functions like this:

    Try
        Dim rnd As New Random
        Dim Key(31) As Byte
        Dim IV(15) As Byte
        rnd.NextBytes(Key) ' your key
        rnd.NextBytes(IV) ' your IV

        Dim original As String = "Here is some data to encrypt!"
        Using myRijndael = Rijndael.Create()
            Dim encrypted As Byte() = EncryptStringToBytes(original, Key, IV)
            Console.WriteLine(Encoding.ASCII.GetString(encrypted))
            Dim roundtrip As String = DecryptStringFromBytes(encrypted, Key, IV)
            Console.WriteLine("Original:   {0}", original)
            Console.WriteLine("Round Trip: {0}", roundtrip)
        End Using
    Catch e As Exception
        Console.WriteLine("Error: {0}", e.Message)
    End Try
    Console.ReadLine()

MS's example:

Shared Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
    ' Check arguments.
    If plainText Is Nothing OrElse plainText.Length <= 0 Then
        Throw New ArgumentNullException("plainText")
    End If
    If Key Is Nothing OrElse Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing OrElse IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If
    Dim encrypted() As Byte
    ' Create an Rijndael object
    ' with the specified key and IV.
    Using rijAlg = Rijndael.Create()
        rijAlg.Key = Key
        rijAlg.IV = IV
        ' Create an encryptor to perform the stream transform.
        Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
        ' Create the streams used for encryption.
        Using msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)

                    'Write all data to the stream.
                    swEncrypt.Write(plainText)
                End Using
                encrypted = msEncrypt.ToArray()
            End Using
        End Using
    End Using
    ' Return the encrypted bytes from the memory stream.
    Return encrypted
End Function
Shared Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
    ' Check arguments.
    If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
        Throw New ArgumentNullException("cipherText")
    End If
    If Key Is Nothing OrElse Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing OrElse IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If
    ' Declare the string used to hold
    ' the decrypted text.
    Dim plaintext As String = Nothing

    ' Create an Rijndael object
    ' with the specified key and IV.
    Using rijAlg = Rijndael.Create()
        rijAlg.Key = Key
        rijAlg.IV = IV

        ' Create a decryptor to perform the stream transform.
        Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)

        ' Create the streams used for decryption.
        Using msDecrypt As New MemoryStream(cipherText)

            Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                Using srDecrypt As New StreamReader(csDecrypt)


                    ' Read the decrypted bytes from the decrypting stream
                    ' and place them in a string.
                    plaintext = srDecrypt.ReadToEnd()
                End Using
            End Using
        End Using
    End Using

    Return plaintext

End Function 'DecryptStringFromBytes 

CodePudding user response:

Thanks to @Jimi link, I've found a solution: writing directly into the cryptostream without using BinaryWriter, and probably the FlushFinalBlock() helps too.

Here is the modified working code:

Public Function Encrypt(msg As String) As String
        Try
            If String.IsNullOrEmpty(msg) = False Then
                Using cryptic As DESCryptoServiceProvider = New DESCryptoServiceProvider()
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes("Something")
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes("Something")

                    Using ms As New MemoryStream()
                        Using cs As New CryptoStream(ms, cryptic.CreateEncryptor(), CryptoStreamMode.Write)
                            Dim Data As Byte() = Encoding.UTF8.GetBytes(msg)
                            cs.Write(Data, 0, Data.Length)
                            cs.FlushFinalBlock()

                            Return Convert.ToBase64String(ms.ToArray())
                        End Using
                    End Using
                End Using
            End If
        Catch ex As Exception
            ExportLog(ex)
        End Try

        Return ""
    End Function

    Public Function Decrypt(msg As String) As String
        Try
            If String.IsNullOrEmpty(msg) = False Then
                Using cryptic As New DESCryptoServiceProvider()
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes("Something")
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes("Something")

                    Using ms As New MemoryStream()
                        Using cs As New CryptoStream(ms, cryptic.CreateDecryptor(), CryptoStreamMode.Write)
                            Dim Data As Byte() = Convert.FromBase64String(msg)
                            cs.Write(Data, 0, Data.Length)
                            cs.FlushFinalBlock()

                            Return Encoding.UTF8.GetString(ms.ToArray())
                        End Using
                    End Using
                End Using
            End If
        Catch ex As Exception
            ExportLog(ex)
        End Try

        Return ""
    End Function
  •  Tags:  
  • Related