| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 841 人关注过本帖, 1 人收藏
标题:发一个Base64编码解码的模块
只看楼主 加入收藏
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:5 
发一个Base64编码解码的模块
程序代码:
'//! Module Name:mduBase64.bas
'//! Intro:Base64 Encode/Decode

Option Explicit

Private Const BASE64STR As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
Private Const ERROR_USER As Integer = 531

Public Function EncodeBase64(ByVal strSrc As String) As String
On Error GoTo errHandler

Dim bytSrc() As Byte
Dim bytTmp() As Byte
Dim strDes   As String
Dim i        As Integer
Dim j        As Integer
Dim n        As Integer
Dim l        As Integer

bytSrc = StrConv(strSrc, vbFromUnicode)
n = UBound(bytSrc) + 1
l = n Mod 3
n = n - l - 1

ReDim bytTmp(3) As Byte

For i = 0 To n Step 3
    bytTmp(0) = (bytSrc(i) And &HFC) \ &H4
    bytTmp(1) = (bytSrc(i) And &H3) * &H10 + (bytSrc(i + 1) And &HF0) \ &H10
    bytTmp(2) = (bytSrc(i + 1) And &HF) * &H4 + (bytSrc(i + 2) And &HC0) \ &H40
    bytTmp(3) = bytSrc(i + 2) And &H3F
    
    For j = 0 To 3
        strDes = strDes & Mid$(BASE64STR, bytTmp(j) + 1, 1)
    Next
Next

Select Case l
    Case 1
        bytTmp(0) = (bytSrc(n + 1) And &HFC) \ &H4
        bytTmp(1) = (bytSrc(n + 1) And &H3) * &H10
        bytTmp(2) = 64
        bytTmp(3) = 64
        For i = 0 To 1
            strDes = strDes & Mid$(BASE64STR, bytTmp(i) + 1, 1)
        Next
        strDes = strDes & "=="
    Case 2
        bytTmp(0) = (bytSrc(n + 1) And &HFC) \ &H4
        bytTmp(1) = (bytSrc(n + 1) And &H3) * &H10 + (bytSrc(n + 2) And &HF0) \ &H10
        bytTmp(2) = (bytSrc(n + 2) And &HF) * &H4
        bytTmp(3) = 64
        For i = 0 To 2
            strDes = strDes & Mid$(BASE64STR, bytTmp(i) + 1, 1)
        Next
        strDes = strDes & "="
    Case Else
End Select

EncodeBase64 = strDes
Exit Function
errHandler:
    Debug.Print Err.Description
    #If DEBUG_MODE Then
        Stop: Resume
    #End If
    EncodeBase64 = ""
End Function

Public Function DecodeBase64(ByVal strSrc As String) As String
On Error GoTo errHandler

Dim bytTmp() As Byte
Dim bytDes() As Byte
Dim strDes   As String
Dim i        As Integer
Dim j        As Integer
Dim n        As Integer
Dim iPos     As Integer
Dim iLen     As Integer
Dim iFrom    As Integer
Dim iTo      As Integer

n = Len(strSrc)
iFrom = 0
iTo = 0
ReDim typdes(iTo) As Byte

ReDim bytTmp(3) As Byte
For i = 1 To n Step 4
    iLen = 4
    For j = 0 To 3
        iPos = InStr(1, BASE64STR, Mid$(strSrc, i + j, 1))
        Select Case iPos
            Case 1 To 64
                bytTmp(j) = iPos - 1
            Case 65
                iLen = j
                Exit For
            Case Else
                Err.Raise ERROR_USER, , "包含非法字符"
        End Select
    Next
    
    bytTmp(0) = bytTmp(0) * &H4 + (bytTmp(1) And &H30) \ &H10
    bytTmp(1) = (bytTmp(1) And &HF) * &H10 + (bytTmp(2) And &H3C) \ &H4
    bytTmp(2) = (bytTmp(2) And &H3) * &H40 + bytTmp(3)
    
    iFrom = iTo
    iTo = iTo + (iLen - 1) - 1
    ReDim Preserve bytDes(iTo) As Byte
    For j = iFrom To iTo
        bytDes(j) = bytTmp(j - iFrom)
    Next
    
    iTo = iTo + 1
Next

strDes = StrConv(bytDes(), vbUnicode)
DecodeBase64 = strDes
Exit Function
errHandler:
    Debug.Print Err.Description
    #If DEBUG_MODE Then
        Stop: Resume
    #End If
    DecodeBase64 = ""
End Function
mduBase64.rar (1.38 KB)


[ 本帖最后由 jiashie 于 2010-5-20 11:42 编辑 ]
收到的鲜花
  • Artless2010-05-19 23:57 送鲜花  5朵   附言:好文章
搜索更多相关主题的帖子: 模块 解码 编码 
2010-05-18 14:57
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
收藏
得分:10 
支持
文件呢?

无知
2010-05-19 23:56
jiashie
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:237
专家分:999
注 册:2009-4-30
收藏
得分:0 
以下是引用Artless在2010-5-19 23:56:57的发言:

支持
文件呢?


在此基础上可以自己再扩展几个不同参数的函数,如Public Function EncodeBase64(ByRef bytSrc As Byte()) As Byte()
编码时,其实也是对byte()进行的
bytSrc = StrConv(strSrc, vbFromUnicode)
对于文件,可以用文件读写Get 到一个byte(),代码稍加修改即可。

[ 本帖最后由 jiashie 于 2010-5-20 08:41 编辑 ]
2010-05-20 08:35
ftwl516
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2009-10-5
收藏
得分:10 
这个实用..顶了.
2010-05-21 03:27
bolang_88
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-12-2
收藏
得分:0 
好牛逼啊
2021-12-02 15:29
yzwxx
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-6-12
收藏
得分:0 
有没有问题
2022-06-12 23:40
快速回复:发一个Base64编码解码的模块
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032311 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved