求教,如何用VB实现BASE64编解码
2个回答
2015-09-04
展开全部
Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。它将需要编码的数据拆分成字节数组。以3个字节为一组。按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。这样就把一个3字节为一组的数据重新编码成了4个字节。当所要编码的数据的字节数不是3的整倍数,也就是说在分组时最后一组不够3个字节。这时在最后一组填充1到2个0字节。并在最后编码完成后在结尾添加1到2个 “=”。
例:将对ABC进行BASE64编码:
1、首先取ABC对应的ASCII码值。A(65)B(66)C(67);
2、再取二进制值A(01000001)B(01000010)C(01000011);
3、然后把这三个字节的二进制码接起来(010000010100001001000011);
4、 再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00010000)(00010100)(00001001)(00000011),其中蓝色部分为真实数据;
5、再把这四个字节数据转化成10进制数得(16)(20)(9)(3);
6、最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符(Q)(U)(J)(D),这里的值实际就是数据在字符表中的索引。
即: ABC编码为QUJD
同理,我们可以实现编码案例: AB编码为QUI= A编码为QQ==
注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
例:将对ABC进行BASE64编码:
1、首先取ABC对应的ASCII码值。A(65)B(66)C(67);
2、再取二进制值A(01000001)B(01000010)C(01000011);
3、然后把这三个字节的二进制码接起来(010000010100001001000011);
4、 再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00010000)(00010100)(00001001)(00000011),其中蓝色部分为真实数据;
5、再把这四个字节数据转化成10进制数得(16)(20)(9)(3);
6、最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符(Q)(U)(J)(D),这里的值实际就是数据在字符表中的索引。
即: ABC编码为QUJD
同理,我们可以实现编码案例: AB编码为QUI= A编码为QQ==
注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
展开全部
Option Explicit
Private Const cstBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Private arrBase64() As String
Public Function Base64Encode(strSource As String) As String
On Error Resume Next
If UBound(arrBase64) = -1 Then
arrBase64 = Split(StrConv(cstBase64, vbUnicode), vbNullChar)
End If
Dim arrB() As Byte, bTmp(2) As Byte, bT As Byte
Dim I As Long, J As Long
arrB = StrConv(strSource, vbFromUnicode)
J = UBound(arrB)
For I = 0 To J Step 3
Erase bTmp
bTmp(0) = arrB(I + 0)
bTmp(1) = arrB(I + 1)
bTmp(2) = arrB(I + 2)
bT = (bTmp(0) And 252) / 4
Base64Encode = Base64Encode & arrBase64(bT)
bT = (bTmp(0) And 3) * 16
bT = bT + bTmp(1) \ 16
Base64Encode = Base64Encode & arrBase64(bT)
bT = (bTmp(1) And 15) * 4
bT = bT + bTmp(2) \ 64
If I + 1 <= J Then
Base64Encode = Base64Encode & arrBase64(bT)
Else
Base64Encode = Base64Encode & "="
End If
bT = bTmp(2) And 63
If I + 2 <= J Then
Base64Encode = Base64Encode & arrBase64(bT)
Else
Base64Encode = Base64Encode & "="
End If
Next
End Function
Public Function Base64Decode(strEncoded As String, m) As String
On Error Resume Next
Dim arrB() As Byte, bTmp(3) As Byte, bT, bRet() As Byte
Dim I As Long, J As Long
arrB = StrConv(strEncoded, vbFromUnicode)
J = InStr(strEncoded & "=", "=") - 2
ReDim bRet(J - J \ 4 - 1)
For I = 0 To J Step 4
Erase bTmp
bTmp(0) = (InStr(cstBase64, Chr(arrB(I))) - 1) And 63
bTmp(1) = (InStr(cstBase64, Chr(arrB(I + 1))) - 1) And 63
bTmp(2) = (InStr(cstBase64, Chr(arrB(I + 2))) - 1) And 63
bTmp(3) = (InStr(cstBase64, Chr(arrB(I + 3))) - 1) And 63
bT = bTmp(0) * 2 ^ 18 + bTmp(1) * 2 ^ 12 + bTmp(2) * 2 ^ 6 + bTmp(3)
bRet((I \ 4) * 3) = bT \ 65536
bRet((I \ 4) * 3 + 1) = (bT And 65280) \ 256
bRet((I \ 4) * 3 + 2) = bT And 255
Next
Base64Decode = StrConv(bRet, vbUnicode)
Base64Decod = Base64Decod Xor m
End Function
Base64Encode 编、、码
Base64Uncode 解、、密
Private Const cstBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Private arrBase64() As String
Public Function Base64Encode(strSource As String) As String
On Error Resume Next
If UBound(arrBase64) = -1 Then
arrBase64 = Split(StrConv(cstBase64, vbUnicode), vbNullChar)
End If
Dim arrB() As Byte, bTmp(2) As Byte, bT As Byte
Dim I As Long, J As Long
arrB = StrConv(strSource, vbFromUnicode)
J = UBound(arrB)
For I = 0 To J Step 3
Erase bTmp
bTmp(0) = arrB(I + 0)
bTmp(1) = arrB(I + 1)
bTmp(2) = arrB(I + 2)
bT = (bTmp(0) And 252) / 4
Base64Encode = Base64Encode & arrBase64(bT)
bT = (bTmp(0) And 3) * 16
bT = bT + bTmp(1) \ 16
Base64Encode = Base64Encode & arrBase64(bT)
bT = (bTmp(1) And 15) * 4
bT = bT + bTmp(2) \ 64
If I + 1 <= J Then
Base64Encode = Base64Encode & arrBase64(bT)
Else
Base64Encode = Base64Encode & "="
End If
bT = bTmp(2) And 63
If I + 2 <= J Then
Base64Encode = Base64Encode & arrBase64(bT)
Else
Base64Encode = Base64Encode & "="
End If
Next
End Function
Public Function Base64Decode(strEncoded As String, m) As String
On Error Resume Next
Dim arrB() As Byte, bTmp(3) As Byte, bT, bRet() As Byte
Dim I As Long, J As Long
arrB = StrConv(strEncoded, vbFromUnicode)
J = InStr(strEncoded & "=", "=") - 2
ReDim bRet(J - J \ 4 - 1)
For I = 0 To J Step 4
Erase bTmp
bTmp(0) = (InStr(cstBase64, Chr(arrB(I))) - 1) And 63
bTmp(1) = (InStr(cstBase64, Chr(arrB(I + 1))) - 1) And 63
bTmp(2) = (InStr(cstBase64, Chr(arrB(I + 2))) - 1) And 63
bTmp(3) = (InStr(cstBase64, Chr(arrB(I + 3))) - 1) And 63
bT = bTmp(0) * 2 ^ 18 + bTmp(1) * 2 ^ 12 + bTmp(2) * 2 ^ 6 + bTmp(3)
bRet((I \ 4) * 3) = bT \ 65536
bRet((I \ 4) * 3 + 1) = (bT And 65280) \ 256
bRet((I \ 4) * 3 + 2) = bT And 255
Next
Base64Decode = StrConv(bRet, vbUnicode)
Base64Decod = Base64Decod Xor m
End Function
Base64Encode 编、、码
Base64Uncode 解、、密
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询