用戶登錄  |  用戶注冊
首 頁源碼下載網絡學院最新源碼源碼排行屏蔽廣告
當前位置:新興網絡 > 網絡學院 > 編程開發 > Visual Basic

VB Base64編碼與解碼函數

減小字體 增大字體 作者:佚名  來源:本站整理  發布時間:2010-07-12 00:12:40
VBScript code復制代碼
' Module1.bas

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) As String'解碼
    On Error Resume Next
    Dim arrB() As Byte, bTmp(3) As Byte, bT As Long, 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)
End Function

Tags:VB Base64 編碼 解碼

作者:佚名
  • 好的評價 如果您覺得此文章好,就請您
      100%(3)
  • 差的評價 如果您覺得此文章差,就請您
      0%(0)

網絡學院評論評論內容只代表網友觀點,與本站立場無關!

   評論摘要(共 0 條,得分 0 分,平均 0 分) 查看完整評論
皮皮麻将官网下载 (★^O^★)MG巨人财富游戏 黑龙江36选7开奖结果走势图 (*^▽^*)MG秘密行动如何爆大奖 码报13期 南粤体彩36选7走势图 浙江快乐彩汇总走势图 (^ω^)MG老虎月亮爆分打法 (*^▽^*)MG春假时光如何爆大奖 (★^O^★)MG日日进财游戏规则 今晚福建22选5开奖 mg游戏幸运双星如何调 (*^▽^*)MG德科钻石登陆 网上兼职帮人代打彩票 南粤风采26选5中奖注数 新疆18选7走势图2元网 甘肃快三开奖结果今天推荐