VB.NET中的Encode64/Decode64
很抱歉写了这么长的文章,但是在.NET中是否已经实现了与此VB6代码简单等效的代码?多谢各位VB.NET中的Encode64/Decode64,vb.net,vb6,Vb.net,Vb6,很抱歉写了这么长的文章,但是在.NET中是否已经实现了与此VB6代码简单等效的代码?多谢各位 Public Function Encode64(sString As String) As String Dim bTrans(63) As Byte, lPowers8(255) As Long, lPowers16(255) As Long, bout() As Byte, bIn() As Byte Dim lChar As Long, lTrip As Long, iPad
Public Function Encode64(sString As String) As String
Dim bTrans(63) As Byte, lPowers8(255) As Long, lPowers16(255) As Long, bout() As Byte, bIn() As Byte
Dim lChar As Long, lTrip As Long, iPad As Integer, lLen As Long, lTemp As Long, lPos As Long, lOutSize As Long
For lTemp = 0 To 63 'Fill the translation table.
Select Case lTemp
Case 0 To 25
bTrans(lTemp) = 65 + lTemp 'A - Z
Case 26 To 51
bTrans(lTemp) = 71 + lTemp 'a - z
Case 52 To 61
bTrans(lTemp) = lTemp - 4 '1 - 0
Case 62
bTrans(lTemp) = 43 'Chr(43) = "+"
Case 63
bTrans(lTemp) = 47 'Chr(47) = "/"
End Select
Next lTemp
For lTemp = 0 To 255 'Fill the 2^8 and 2^16 lookup tables.
lPowers8(lTemp) = lTemp * cl2Exp8
lPowers16(lTemp) = lTemp * cl2Exp16
Next lTemp
iPad = StrLen(sString) Mod 3 'See if the length is divisible by 3
If iPad Then 'If not, figure out the end pad and resize the input.
iPad = 3 - iPad
sString = sString & String(iPad, Chr(0))
End If
bIn = StrConv(sString, vbFromUnicode) 'Load the input string.
lLen = ((UBound(bIn) + 1) \ 3) * 4 'Length of resulting string.
lTemp = lLen \ 72 'Added space for vbCrLfs.
lOutSize = ((lTemp * 2) + lLen) - 1 'Calculate the size of the output buffer.
ReDim bout(lOutSize) 'Make the output buffer.
lLen = 0 'Reusing this one, so reset it.
For lChar = LBound(bIn) To UBound(bIn) Step 3
lTrip = lPowers16(bIn(lChar)) + lPowers8(bIn(lChar + 1)) + bIn(lChar + 2) 'Combine the 3 bytes
lTemp = lTrip And clOneMask 'Mask for the first 6 bits
bout(lPos) = bTrans(lTemp \ cl2Exp18) 'Shift it down to the low 6 bits and get the value
lTemp = lTrip And clTwoMask 'Mask for the second set.
bout(lPos + 1) = bTrans(lTemp \ cl2Exp12) 'Shift it down and translate.
lTemp = lTrip And clThreeMask 'Mask for the third set.
bout(lPos + 2) = bTrans(lTemp \ cl2Exp6) 'Shift it down and translate.
bout(lPos + 3) = bTrans(lTrip And clFourMask) 'Mask for the low set.
If lLen = 68 Then 'Ready for a newline
bout(lPos + 4) = 13 'Chr(13) = vbCr
bout(lPos + 5) = 10 'Chr(10) = vbLf
lLen = 0 'Reset the counter
lPos = lPos + 6
Else
lLen = lLen + 4
lPos = lPos + 4
End If
Next lChar
If bout(lOutSize) = 10 Then lOutSize = lOutSize - 2 'Shift the padding chars down if it ends with CrLf.
If iPad = 1 Then 'Add the padding chars if any.
bout(lOutSize) = 61 'Chr(61) = "="
ElseIf iPad = 2 Then
bout(lOutSize) = 61
bout(lOutSize - 1) = 61
End If
Encode64 = StrConv(bout, vbUnicode) 'Convert back to a string and return it.
End Function
Public Function Decode64(sString As String) As String
Dim bout() As Byte, bIn() As Byte, bTrans(255) As Byte, lPowers6(63) As Long, lPowers12(63) As Long
Dim lPowers18(63) As Long, lQuad As Long, iPad As Integer, lChar As Long, lPos As Long, sOut As String
Dim lTemp As Long
sString = Replace(sString, vbCr, vbNullString) 'Get rid of the vbCrLfs. These could be in...
sString = Replace(sString, vbLf, vbNullString) 'either order.
lTemp = StrLen(sString) Mod 4 'Test for valid input.
If lTemp Then
Call Err.Raise(vbObjectError, "MyDecode", "Input string is not valid Base64.")
End If
If InStrRev(sString, "==") Then 'InStrRev is faster when you know it's at the end.
iPad = 2 'Note: These translate to 0, so you can leave them...
ElseIf InStrRev(sString, "=") Then 'in the string and just resize the output.
iPad = 1
End If
For lTemp = 0 To 255 'Fill the translation table.
Select Case lTemp
Case 65 To 90
bTrans(lTemp) = lTemp - 65 'A - Z
Case 97 To 122
bTrans(lTemp) = lTemp - 71 'a - z
Case 48 To 57
bTrans(lTemp) = lTemp + 4 '1 - 0
Case 43
bTrans(lTemp) = 62 'Chr(43) = "+"
Case 47
bTrans(lTemp) = 63 'Chr(47) = "/"
End Select
Next lTemp
For lTemp = 0 To 63 'Fill the 2^6, 2^12, and 2^18 lookup tables.
lPowers6(lTemp) = lTemp * cl2Exp6
lPowers12(lTemp) = lTemp * cl2Exp12
lPowers18(lTemp) = lTemp * cl2Exp18
Next lTemp
bIn = StrConv(sString, vbFromUnicode) 'Load the input byte array.
ReDim bout((((UBound(bIn) + 1) \ 4) * 3) - 1) 'Prepare the output buffer.
For lChar = 0 To UBound(bIn) Step 4
lQuad = lPowers18(bTrans(bIn(lChar))) + lPowers12(bTrans(bIn(lChar + 1))) + _
lPowers6(bTrans(bIn(lChar + 2))) + bTrans(bIn(lChar + 3)) 'Rebuild the bits.
lTemp = lQuad And clHighMask 'Mask for the first byte
bout(lPos) = lTemp \ cl2Exp16 'Shift it down
lTemp = lQuad And clMidMask 'Mask for the second byte
bout(lPos + 1) = lTemp \ cl2Exp8 'Shift it down
bout(lPos + 2) = lQuad And clLowMask 'Mask for the third byte
lPos = lPos + 3
Next lChar
sOut = StrConv(bout, vbUnicode) 'Convert back to a string.
If iPad Then sOut = Left$(sOut, StrLen(sOut) - iPad) 'Chop off any extra bytes.
Decode64 = sOut
End Function
有一些本机.NET函数可以处理此问题:
System.Convert.ToBase64String(编码)和System.Convert.FromBase64String(解码)这是如何将.net中的本机函数与字符串一起使用的:
Public Shared Function EncodeString64(instr as string) as String
Return Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(instr))
End function
Public Shared Function DecodeString64(encstr as string) as String
Return ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(encstr))
End function
对于unicode:
Public Shared Function EncodeString64(instr as string) as String
Return Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(instr))
End function
Public Shared Function DecodeString64(encstr as string) as String
Return System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(encstr))
End function
非常感谢。但它只接受字节,我想给它传递一个字符串。只需将
ascienceoding.ASCII
替换为System.Text.Encoding.Unicode
。我将更新我的答案。VB6代码使用ANSI编码。如果希望VB.Net的行为与VB6完全相同,例如需要读取由VB6编写的文件,则这一点可能很重要。与VB.Net等价的是Encoding.DefaultANSI可能意味着很多事情。它的历史用途是在终端中,ASCII(7位)用于前128个字符,上面的128个字符基本上可以是任何字符。除非您知道上面部分的代码页,否则您只能通过尝试解码字符串来找到它。Base64对此视而不见,但只要它们保持在字节宽度内(1字节代表ascii/ansi,2字节代表某些unicode等),我认为这不会是一个问题。您不能再将二进制数据存储在字符串中。必须在vb.net中使用Byte()。如果忽略这一点,那么当.NET规范化字符串时,您会发现数据已损坏。