VB.net base 64 字符数组或字符串的无效长度
VB.net invalid length for a base 64 char array or string
我在将 base-64 字符串转换为正常的可读文本时遇到问题。我做了一些研究,发现 base 64 字符串的长度必须是 4 的倍数。所以我使用 padRight 给它一个有效长度,但我总是遇到同样的错误。例如,我输入 "hi",它编码为“⚫aGk====”,这对我来说似乎是 8 个字符(显然是 4 的倍数)。当我尝试读取它时,它读入的长度为 1。
我还使用了一个自定义文件扩展名,我刚刚称之为“.bgs”。我不确定这是否有任何作用。将此文件作为 base64 字符串和 reading/decoding 写入,这是我唯一想做的事情。
这是我的代码:
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using bs As New BinaryWriter(File.Open("saveFile.bgs", FileMode.Create))
Dim originText As String = TextBox1.Text
Dim cipherText As String
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(originText)
cipherText = Convert.ToBase64String(byteArray)
Dim realLength As Integer = cipherText.Length() + 1
Dim len As Integer = (realLength Mod 4)
If (len > 0) Then bs.Write(cipherText.PadRight(realLength + (3 - len), "="))
bs.Close()
End Using
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Using bs As New BinaryReader(File.Open("saveFile.bgs", FileMode.Open))
Dim cipherText2 As String = bs.Read()
Dim originText2 As String = "Failed"
TextBox2.Text = cipherText2.Length() 'reports length of 1
Try
Dim byteArray2 As Byte() = Convert.FromBase64String(cipherText2)
originText2 = System.Text.Encoding.UTF8.GetString(byteArray2)
Catch ex As Exception
End Try
'TextBox2.Text = originText2
End Using
End Sub
非常感谢任何帮助!
更新:当我用记事本打开.bgs文件时,它看起来像第一个字符(上例中的点)控制了cipherText2的内容,这只是一个数字,解释了为什么长度是这么低
Base64 仅使用可打印的 ASCII 字符进行编码。
您看到这个点是因为您在写入文件时使用 prefixes strings with their length 二进制写入器。
然后你使用 Read
而不是 ReadString
所以你读取字符串长度作为数字(然后隐式转换为字符串因为你没有像你一样使用 Option Strict On
应该)。
您可以使用 ReadString
而不是 Read
来修复它,但是如果您使用文本编写器会更容易。
您也不应该尝试填充 ToBase64String
的结果。它已经为您提供了正确的字符串。
我会将您的代码重写为:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim originText As String = TextBox1.Text
Dim cipherText As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(originText))
System.IO.File.WriteAllText("saveFile.bgs", cipherText, System.Text.Encoding.ASCII)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim cipherText2 As String = System.IO.File.ReadAllText("saveFile.bgs", System.Text.Encoding.ASCII)
Dim originText2 As String = "Failed"
originText2 = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cipherText2))
'TextBox2.Text = originText2
End Sub
我在将 base-64 字符串转换为正常的可读文本时遇到问题。我做了一些研究,发现 base 64 字符串的长度必须是 4 的倍数。所以我使用 padRight 给它一个有效长度,但我总是遇到同样的错误。例如,我输入 "hi",它编码为“⚫aGk====”,这对我来说似乎是 8 个字符(显然是 4 的倍数)。当我尝试读取它时,它读入的长度为 1。 我还使用了一个自定义文件扩展名,我刚刚称之为“.bgs”。我不确定这是否有任何作用。将此文件作为 base64 字符串和 reading/decoding 写入,这是我唯一想做的事情。
这是我的代码:
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using bs As New BinaryWriter(File.Open("saveFile.bgs", FileMode.Create))
Dim originText As String = TextBox1.Text
Dim cipherText As String
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(originText)
cipherText = Convert.ToBase64String(byteArray)
Dim realLength As Integer = cipherText.Length() + 1
Dim len As Integer = (realLength Mod 4)
If (len > 0) Then bs.Write(cipherText.PadRight(realLength + (3 - len), "="))
bs.Close()
End Using
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Using bs As New BinaryReader(File.Open("saveFile.bgs", FileMode.Open))
Dim cipherText2 As String = bs.Read()
Dim originText2 As String = "Failed"
TextBox2.Text = cipherText2.Length() 'reports length of 1
Try
Dim byteArray2 As Byte() = Convert.FromBase64String(cipherText2)
originText2 = System.Text.Encoding.UTF8.GetString(byteArray2)
Catch ex As Exception
End Try
'TextBox2.Text = originText2
End Using
End Sub
非常感谢任何帮助!
更新:当我用记事本打开.bgs文件时,它看起来像第一个字符(上例中的点)控制了cipherText2的内容,这只是一个数字,解释了为什么长度是这么低
Base64 仅使用可打印的 ASCII 字符进行编码。
您看到这个点是因为您在写入文件时使用 prefixes strings with their length 二进制写入器。
然后你使用 Read
而不是 ReadString
所以你读取字符串长度作为数字(然后隐式转换为字符串因为你没有像你一样使用 Option Strict On
应该)。
您可以使用 ReadString
而不是 Read
来修复它,但是如果您使用文本编写器会更容易。
您也不应该尝试填充 ToBase64String
的结果。它已经为您提供了正确的字符串。
我会将您的代码重写为:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim originText As String = TextBox1.Text
Dim cipherText As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(originText))
System.IO.File.WriteAllText("saveFile.bgs", cipherText, System.Text.Encoding.ASCII)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim cipherText2 As String = System.IO.File.ReadAllText("saveFile.bgs", System.Text.Encoding.ASCII)
Dim originText2 As String = "Failed"
originText2 = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cipherText2))
'TextBox2.Text = originText2
End Sub