VBBase64编解码
来源:优易学  2011-12-10 20:23:56   【优易学:中国教育考试门户网】   资料下载   IT书店

  模块代码( m_Base64.bas):
  Option Explicit
  ’’除以2的一次方是右移一位
  ’’乘以2的一次方是左移一位
  ’’(btyInText(i) And &HFC) (2 ^ 2)
  ’’第一个字节的内容And运算0xFC(11111100)(取左边6位),再除以2的二次方(右移2位)
  ’’(btyInText(i) And &H3) * (2 ^ 4) Or (btyInText(i + 1) And &HF0) (2 ^ 4)
  ’’第一个字节的内容And运算0x03(00000011)(取右边2位),再乘以2的四次方(左移4位)
  ’’第二个字节的内容And运算0xF0(11110000)(取左边4位),再除以2的四次方(右移4位)
  ’’两个结果再Or运算
  ’’(btyInText(i + 1) And &HF) * (2 ^ 2) + (btyInText(i + 2) And &HC0) (2 ^ 6)
  ’’第二个字节的内容And运算0x0F(00001111)(取右边4位),再乘以2的二次方(左移2位)
  ’’第三个字节的内容And运算0xC0(11000000)(取左边2位),再除以2的六次方(右移6位)
  ’’两个结果再Or运算
  ’’btyInText(i + 2) And &H3F
  ’’第三个字节的内容And运算0x3F(00111111)(取右边6位)
  ’’Base64编码函数
  Public Function Base64_Encode(btyInText() As Byte) As Byte()
  Dim Base64EncodeTable() As Byte
  Dim lngInTextLen As Long, lngMod As Long, i As Long
  Dim btyEncode() As Byte, lngEncodeLen As Long
  Base64_Encode = Chr(0) ’’初始化函数返回值
  Base64EncodeTable() = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ’’初始化Base64编码表
  Base64EncodeTable() = StrConv(Base64EncodeTable(), vbFromUnicode) ’’转换为ANSI编码
  If LBound(btyInText) <> 0 Then Exit Function ’’btyInText数组下标不从零开始则出错返回
  lngInTextLen = UBound(btyInText) - LBound(btyInText) + 1 ’’计算btyInText数组长度
  lngMod = lngInTextLen Mod 3 ’’取模3后的余数(结果只有0、1、2三种情况)
  If lngMod = 0 Then
  lngEncodeLen = lngInTextLen / 3 * 4 ’’求编码后的长度
  lngInTextLen = lngInTextLen / 3 * 3 ’’取3的整数倍
  ElseIf lngMod = 1 Then
  lngEncodeLen = (lngInTextLen + 2) / 3 * 4 ’’求编码后的长度
  lngInTextLen = ((lngInTextLen + 2) / 3 - 1) * 3 ’’取3的整数倍
  ElseIf lngMod = 2 Then
  lngEncodeLen = (lngInTextLen + 1) / 3 * 4 ’’求编码后的长度
  lngInTextLen = ((lngInTextLen + 1) / 3 - 1) * 3 ’’取3的整数倍
  End If
  ’’MsgBox "编码后的长度为" & lngEncodeLen & "字节!"
  ’’MsgBox "3的整数倍为" & lngInTextLen
  ReDim btyEncode(0 To lngEncodeLen - 1) ’’重新定义编码缓冲区
  lngEncodeLen = 0 ’’初始化编码长度计数
  For i = 0 To lngInTextLen - 1 Step 3
  btyEncode(lngEncodeLen) = Base64EncodeTable((btyInText(i) And &HFC) (2 ^ 2))
  btyEncode(lngEncodeLen + 1) = Base64EncodeTable((btyInText(i) And &H3) * (2 ^ 4) Or (btyInText(i + 1) And &HF0) (2 ^ 4))
  btyEncode(lngEncodeLen + 2) = Base64EncodeTable((btyInText(i + 1) And &HF) * (2 ^ 2) Or (btyInText(i + 2) And &HC0) (2 ^ 6))
  btyEncode(lngEncodeLen + 3) = Base64EncodeTable(btyInText(i + 2) And &H3F)
  lngEncodeLen = lngEncodeLen + 4
  Next
  i = lngInTextLen - 1 + 1
  If lngMod = 1 Then ’’对剩余字节进行填充
  btyEncode(lngEncodeLen) = Base64EncodeTable((btyInText(i) And &HFC) (2 ^ 2))
  btyEncode(lngEncodeLen + 1) = Base64EncodeTable((btyInText(i) And &H3) * (2 ^ 4))
  btyEncode(lngEncodeLen + 2) = Base64EncodeTable(64) ’’填充=
  btyEncode(lngEncodeLen + 3) = Base64EncodeTable(64) ’’填充=
  lngEncodeLen = lngEncodeLen + 4
  ElseIf lngMod = 2 Then
  btyEncode(lngEncodeLen) = Base64EncodeTable((btyInText(i) And &HFC) (2 ^ 2))
  btyEncode(lngEncodeLen + 1) = Base64EncodeTable((btyInText(i) And &H3) * (2 ^ 4) Or (btyInText(i + 1) And &HF0) (2 ^ 4))
  btyEncode(lngEncodeLen + 2) = Base64EncodeTable((btyInText(i + 1) And &HF) * (2 ^ 2))
  btyEncode(lngEncodeLen + 3) = Base64EncodeTable(64) ’’填充=
  lngEncodeLen = lngEncodeLen + 4
  End If
  Base64_Encode = btyEncode()
  End Function
  ’’Base64解码函数
  Public Function Base64_Decode(btyInText() As Byte) As Byte()
  Dim Base64DecodeTable(1 To 122) As Byte
  Dim lngInTextLen As Long, i As Long
  Dim btyDecode() As Byte, lngDecodeLen As Long
  Base64_Decode = Chr(0) ’’初始化函数返回值

  If LBound(btyInText) <> 0 Then Exit Function ’’btyInText数组下标不从零开始则出错返回
  lngInTextLen = UBound(btyInText) - LBound(btyInText) + 1 ’’计算btyInText数组长度
  If lngInTextLen Mod 4 <> 0 Then Exit Function ’’输入编码不是4的倍数则出错返回
  For i = 1 To 122 ’’初始化Base64解码表
  Select Case i
  Case 43 ’’+
  Base64DecodeTable(i) = 62
  Case 47 ’’/
  Base64DecodeTable(i) = 63
  Case 48 To 57 ’’0 - 9
  Base64DecodeTable(i) = 52 + (i - 48)
  Case 65 To 90 ’’A - Z
  Base64DecodeTable(i) = 0 + (i - 65)
  Case 97 To 122 ’’a - z
  Base64DecodeTable(i) = 26 + (i - 97)
  Case Else
  Base64DecodeTable(i) = 255
  End Select
  Next
  lngDecodeLen = lngInTextLen / 4 * 3 ’’求解码后的最大长度
  ReDim btyDecode(0 To lngDecodeLen - 1) ’’重新定义解码缓冲区
  ’’MsgBox "解码后的最大长度为:" & lngDecodeLen
  lngDecodeLen = 0 ’’初始化解码长度
  For i = 0 To lngInTextLen - 1 Step 4
  btyDecode(lngDecodeLen) = (Base64DecodeTable(btyInText(i)) * (2 ^ 2)) Or ((Base64DecodeTable(btyInText(i + 1)) And &H30) (2 ^ 4))
  btyDecode(lngDecodeLen + 1) = ((Base64DecodeTable(btyInText(i + 1)) And &HF) * (2 ^ 4)) Or ((Base64DecodeTable(btyInText(i + 2)) And &H3C) (2 ^ 2))
  btyDecode(lngDecodeLen + 2) = ((Base64DecodeTable(btyInText(i + 2)) And &H3) * (2 ^ 6)) Or Base64DecodeTable(btyInText(i + 3))
  lngDecodeLen = lngDecodeLen + 3
  Next
  If btyInText(lngInTextLen - 1) = &H3D Then ’’判断最后两个字节的情况,求解码后的实际长度
  If btyInText(lngInTextLen - 2) = &H3D Then
  lngDecodeLen = lngDecodeLen - 2 ’’最后两个字节为"="
  Else
  lngDecodeLen = lngDecodeLen - 1 ’’最后一个字节为"="
  End If
  btyDecode(lngDecodeLen) = 0 ’’在实际长度的后一个字节放个结束符
  End If
  ’’MsgBox "解码后的实际长度为:" & lngDecodeLen
  Base64_Decode = btyDecode()
  End Function
  调用例子(frmMain.frm):
  Option Explicit
  Private Sub cmdDecode_Click() ’’解码按钮
  Dim btyInText() As Byte
  Dim btyOutText() As Byte
  btyInText() = Text1.Text ’’输入文本框
  btyInText() = StrConv(btyInText(), vbFromUnicode)
  btyOutText() = Base64_Decode(btyInText())
  Open "C:Base64解码后的字符.txt" For Binary As #1
  Put #1, , btyOutText()
  Close #1
  MsgBox "’’" & StrConv(btyInText(), vbUnicode) & "’’的Base64解码是:" & vbCrLf & vbCrLf & StrConv(btyOutText(), vbUnicode)
  Text2.Text = StrConv(btyOutText(), vbUnicode) ’’输出文本框
  End Sub
  Private Sub cmdEncode_Click() ’’编码按钮
  Dim btyInText() As Byte
  Dim btyOutText() As Byte
  btyInText() = Text1.Text ’’输入文本框
  btyInText() = StrConv(btyInText(), vbFromUnicode)
  btyOutText() = Base64_Encode(btyInText())
  Open "C:Base64编码后的字符.txt" For Binary As #1
  Put #1, , btyOutText()
  Close #1
  MsgBox "’’" & StrConv(btyInText(), vbUnicode) & "’’的Base64编码是:" & vbCrLf & vbCrLf & StrConv(btyOutText(), vbUnicode)
  Text2.Text = StrConv(btyOutText(), vbUnicode) ’’输出文本框
  End Sub

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训