威山滴屋

非专注研究ASP、.NET、C\C++、PHP、Sql Server、Mysql、Hack、Div+CSS、AJAX、Visual Baisc以及快速致富

上一篇: VB调用API来创建、启动、删除系统服务 下一篇:VB下的注册表操作类模块

'威山使用的BASE64编码类模块

 

Option Explicit
Dim Base64Chr         As String
Dim NullChr As String
Dim psBase64Chr(0 To 63)    As String

Public Function InitBase(ByVal Base64Chr As String)
    '如果没有提供字符串则按照标准编码
    If Base64Chr = "" Then Base64Chr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
    Dim iPtr    As Integer
    '初始化 BASE64数组
    For iPtr = 0 To 63
        psBase64Chr(iPtr) = Mid$(Base64Chr, iPtr + 1, 1)
    Next
   
    NullChr = Right(Base64Chr, 1)
End Function

'从一个经过Base64的字符串中解码到源字符串
Public Function DecodeBase64String(str2Decode As String) As String
    DecodeBase64String = StrConv(DecodeBase64Byte(str2Decode), vbUnicode)
End Function
 
'从一个经过Base64的字符串中解码到源字节数组
Public Function DecodeBase64Byte(str2Decode As String) As Byte()
 
    Dim lPtr            As Long
    Dim iValue          As Integer
    Dim iLen            As Integer
    Dim iCtr            As Integer
    Dim Bits(1 To 4)    As Byte
    Dim strDecode       As String
    Dim str             As String
    Dim Output()        As Byte
   
    Dim iIndex          As Long

    Dim lFrom As Long
    Dim lTo As Long
         
    '//除去回车
    str = Replace(str2Decode, vbCrLf, "")
 
    '//每4个字符一组(4个字符表示3个字)
    For lPtr = 1 To Len(str) Step 4
        iLen = 4
        For iCtr = 0 To 3
            '//查找字符在BASE64字符串中的位置
            iValue = InStr(1, Base64Chr, Mid$(str, lPtr + iCtr, 1), vbBinaryCompare)
            Select Case iValue  'A~Za~z0~9+/
                Case 1 To 64:
                    Bits(iCtr + 1) = iValue - 1
                Case 65         '=
                    iLen = iCtr
                    Exit For
                    '//没有发现
                Case 0: Exit Function
            End Select
        Next
 
        '//转换4个6比特数成为3个8比特数
        Bits(1) = Bits(1) * &H4 + (Bits(2) And &H30) \ &H10
        Bits(2) = (Bits(2) And &HF) * &H10 + (Bits(3) And &H3C) \ &H4
        Bits(3) = (Bits(3) And &H3) * &H40 + Bits(4)
 
        '//计算数组的起始位置
        lFrom = lTo
        lTo = lTo + (iLen - 1) - 1
               
        '//重新定义输出数组
        ReDim Preserve Output(0 To lTo)
       
        For iIndex = lFrom To lTo
            Output(iIndex) = Bits(iIndex - lFrom + 1)
        Next
 
        lTo = lTo + 1
       
    Next
    DecodeBase64Byte = Output
End Function
 
'将一个字节数组进行Base64编码,并返回字符串
Public Function EncodeBase64Byte(sValue() As Byte) As String
    Dim lCtr                As Long
    Dim lPtr                As Long
    Dim lLen                As Long
    Dim sEncoded            As String
    Dim Bits8(1 To 3)       As Byte
    Dim Bits6(1 To 4)       As Byte
    Dim i As Integer
    For lCtr = 1 To UBound(sValue) + 1 Step 3
        For i = 1 To 3
            If lCtr + i - 2 <= UBound(sValue) Then
                Bits8(i) = sValue(lCtr + i - 2)
                lLen = 3
            Else
                Bits8(i) = 0
                lLen = lLen - 1
            End If
        Next
 
        '//转换字符串为数组,然后转换为4个6位(0-63)
        Bits6(1) = (Bits8(1) And &HFC) \ 4
        Bits6(2) = (Bits8(1) And &H3) * &H10 + (Bits8(2) And &HF0) \ &H10
        Bits6(3) = (Bits8(2) And &HF) * 4 + (Bits8(3) And &HC0) \ &H40
        Bits6(4) = Bits8(3) And &H3F
 
        '//添加4个新字符
        For lPtr = 1 To lLen + 1
            sEncoded = sEncoded & psBase64Chr(Bits6(lPtr))
        Next
    Next
 
    '//不足4位,以=填充
    Select Case lLen + 1
        Case 2: sEncoded = sEncoded & NullChr & NullChr
        Case 3: sEncoded = sEncoded & NullChr
        Case 4:
    End Select
 
    EncodeBase64Byte = sEncoded
End Function
 

'对字符串进行Base64编码并返回字符串
Public Function EncodeBase64String(str2Encode As String) As String
    Dim sValue()            As Byte
    sValue = StrConv(str2Encode, vbFromUnicode)
    EncodeBase64String = EncodeBase64Byte(sValue)
End Function

'对文件进行Base64编码并返回编码后的Base64字符串
Public Function EncodFileToBase64String(strFileSource As String)
    Dim lpdata() As Byte, i As Long, n As Long
    i = FreeFile
    Open strFileSource For Binary Access Read Lock Write As i
        n = LOF(i) - 1
        ReDim lpdata(0 To n)
        Get i, , lpdata
    Close i
    EncodFileToBase64String = EncodeBase64Byte(lpdata)
End Function


'将一个Base64字符串解码,并写入二进制文件
Public Function DecodeBase64StringToFile(strBase64 As String, strFilePath As String)
    Dim i As Integer
    i = FreeFile
    Open strFilePath For Binary Access Write As i
        Put i, , DecodeBase64Byte(strBase64)
    Close i
End Function



Private Sub Class_Initialize()
    Call InitBase("")
End Sub

 

点击这里获取该日志的TrackBack引用地址

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于本文

您正在阅读的是:VB下的Base64编码类模块
V3D5 发表于:2008-9-20 16:43:17
分类:算法
关键词:VB  编码  加密  
订阅威山滴屋

什么是RSS订阅?查看解释
订阅到您的在线阅读器

抓虾 google reader my yahoo bloglines 鲜果 哪吒

博客作者

威山

最近发表

最新评论及回复

最近留言

控制面板

Search

网站分类

文章归档

图标汇集

  • RainbowSoft Studio Z-Blog
  • RainbowSoft Studio Z-Blog
  • 本站支持WAP访问
  • 订阅本站的 RSS 2.0 新闻聚合

Auto Publisher 无忧创业网 | 搜遍青岛 | 明晨网络
Copyright 2008 v3d5.com. All Rights Reserved. 鄂ICP备07002202号

Powered By Z-Blog 1.8 Spirit Build 80722 Code detection by Codefense Template Designed By houbenbo