base64 上的意外字符 encode/decode
unexpected character on base64 encode/decode
我正在使用以下编码器和解码器来保存密码:
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
问题是在解码后,我在字符串的开头有一个随机字符,通常是位于 ASCII table 开头的字符(如 STX、EOT...)。例如我得到 ChrW(7) & "MyPassword"
而不是 "MyPassword"
.
我也试过确保编码后的密码是 4 的倍数,但我遇到了同样的问题。
我会按照 MS 中的示例调用这样的函数:
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 的示例:
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
感谢@Jimi link,我找到了一个解决方案:直接写入加密流而不使用 BinaryWriter
,可能 FlushFinalBlock()
也有帮助。
这里是修改后的工作代码:
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
我正在使用以下编码器和解码器来保存密码:
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
问题是在解码后,我在字符串的开头有一个随机字符,通常是位于 ASCII table 开头的字符(如 STX、EOT...)。例如我得到 ChrW(7) & "MyPassword"
而不是 "MyPassword"
.
我也试过确保编码后的密码是 4 的倍数,但我遇到了同样的问题。
我会按照 MS 中的示例调用这样的函数:
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 的示例:
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
感谢@Jimi link,我找到了一个解决方案:直接写入加密流而不使用 BinaryWriter
,可能 FlushFinalBlock()
也有帮助。
这里是修改后的工作代码:
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