VB BASE64



Option Explicit

Public Function Base64Encode2(Infile As String, Outfile As String)
Dim FnumIn As Integer, FnumOut As Integer
Dim mInByte(3) As Byte, mOutByte(4) As Byte
Dim myByte As Byte
Dim i As Integer, LineLen As Integer, j As Integer
FnumIn = FreeFile()
Open Infile For Binary As #FnumIn
FnumOut = FreeFile()
Open Outfile For Binary As #FnumOut
While Not EOF(FnumIn)
    i = 0
    Do While i < 3
    Get #FnumIn, , myByte
    If Not EOF(FnumIn) Then
        mInByte(i) = myByte
        i = i + 1
    Else
        Exit Do
    End If
    Loop
    Base64EncodeByte mInByte, mOutByte, i
    For j = 0 To 3
        Put #FnumOut, , mOutByte(j)
    Next j
    LineLen = LineLen + 1
    If LineLen * 4 > 70 Then
        Put #FnumOut, , vbCrLf
        LineLen = 0
    End If
Wend
Close (FnumOut)
Close (FnumIn)
End Function

Private Sub Base64EncodeByte(mInByte() As Byte, mOutByte() As Byte, Num As Integer)
Dim tByte As Byte
Dim i As Integer

If Num = 1 Then
    mInByte(1) = 0
    mInByte(2) = 0
ElseIf Num = 2 Then
    mInByte(2) = 0
End If

tByte = mInByte(0) And &HFC
mOutByte(0) = tByte / 4
tByte = ((mInByte(0) And &H3) * 16) + (mInByte(1) And &HF0) / 16
mOutByte(1) = tByte
tByte = ((mInByte(1) And &HF) * 4) + ((mInByte(2) And &HC0) / 64)
mOutByte(2) = tByte
tByte = (mInByte(2) And &H3F)
mOutByte(3) = tByte

For i = 0 To 3
    If mOutByte(i) >= 0 And mOutByte(i) <= 25 Then
        mOutByte(i) = mOutByte(i) + Asc("A")
    ElseIf mOutByte(i) >= 26 And mOutByte(i) <= 51 Then
        mOutByte(i) = mOutByte(i) - 26 + Asc("a")
    ElseIf mOutByte(i) >= 52 And mOutByte(i) <= 61 Then
        mOutByte(i) = mOutByte(i) - 52 + Asc("0")
    ElseIf mOutByte(i) = 62 Then
        mOutByte(i) = Asc("+")
    Else
        mOutByte(i) = Asc("/")
    
    End If
Next i

If Num = 1 Then
    mOutByte(2) = Asc("=")
    mOutByte(3) = Asc("=")
ElseIf Num = 2 Then
    mOutByte(3) = Asc("=")
End If
End Sub


Public Function Base64Decode2(Infile As String, Outfile As String)
Dim FnumIn As Integer, FnumOut As Integer
Dim mInByte(4) As Byte, mOutByte(3) As Byte
Dim myByte As Byte
Dim i As Integer, LineLen As Integer, j As Integer
Dim ByteNum As Integer
FnumIn = FreeFile()
Open Infile For Binary As #FnumIn
FnumOut = FreeFile()
Open Outfile For Binary As #FnumOut

While Not EOF(FnumIn)
    i = 0
    Do While i < 4
    Get #FnumIn, , myByte
    If Not EOF(FnumIn) Then
        If myByte <> &HA And myByte <> &HD Then
        '把回车符和换行符去掉
            mInByte(i) = myByte
            i = i + 1
        End If
    Else
        Exit Do
    End If
    Loop
    Base64DecodeByte mInByte, mOutByte, ByteNum
    
    For j = 0 To 2 - ByteNum
        Put #FnumOut, , mOutByte(j)
    Next j
    'LineLen = LineLen + 1
Wend
Close (FnumOut)
Close (FnumIn)
End Function

Private Sub Base64DecodeByte(mInByte() As Byte, mOutByte() As Byte, ByteNum As Integer)
Dim tByte As Byte
Dim i As Integer
ByteNum = 0
For i = 0 To 3
    If mInByte(i) >= Asc("A") And mInByte(i) <= Asc("Z") Then
        mInByte(i) = mInByte(i) - Asc("A")
    ElseIf mInByte(i) >= Asc("a") And mInByte(i) <= Asc("z") Then
        mInByte(i) = mInByte(i) - Asc("a") + 26
    ElseIf mInByte(i) >= Asc("0") And mInByte(i) <= Asc("9") Then
        mInByte(i) = mInByte(i) - Asc("0") + 52
    ElseIf mInByte(i) = Asc("+") Then
        mInByte(i) = 62
    ElseIf mInByte(i) = Asc("/") Then
        mInByte(i) = 63
    Else '"="
        ByteNum = ByteNum + 1
        mInByte(i) = 0
    End If
Next i
'取前六位
tByte = (mInByte(0) And &H3F) * 4 + (mInByte(1) And &H30) / 16
'0的六位和1的前两位
mOutByte(0) = tByte
tByte = (mInByte(1) And &HF) * 16 + (mInByte(2) And &H3C) / 4
'1的后四位和2的前四位
mOutByte(1) = tByte
tByte = (mInByte(2) And &H3) * 64 + (mInByte(3) And &H3F)
mOutByte(2) = tByte
'2的后两位和3的六位
End Sub

'Private Sub cmdBase64_Click(Index As Integer)
'If Index = 0 Then
'    Call Base64Encode2(App.Path & "\121.JPG", App.Path & "\out.txt")
'Else
'    Call Base64Decode2(App.Path & "\out.txt", App.Path & "\in3.JPG")
'End If
'End Sub


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值