'base64加密算法
Public Function Base64_Encode(strSource) As String
Const BASE64_TABLE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim strTempLine As String
Dim j As Integer
For j = 1 To (Len(strSource) - Len(strSource) Mod 3) Step 3
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 _
+ Asc(Mid(strSource, j + 2, 1)) \ 64) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 2, 1)) Mod 64) + 1, 1)
Next j
If Not (Len(strSource) Mod 3) = 0 Then
If (Len(strSource) Mod 3) = 2 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 + 1, 1)
strTempLine = strTempLine & "="
ElseIf (Len(strSource) Mod 3) = 1 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, Asc(Mid(strSource, j, 1)) \ 4 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 + 1, 1)
strTempLine = strTempLine & "=="
End If
End If
Base64_Encode = strTempLine
End Function 'base64解密算法
Public Function DecodeBase64String(str2Decode As String) As String'******************************************************************************
'
' Synopsis: Decode a Base 64 string
'
' Parameters: str2Decode - The base 64 encoded input string
'
' Return: decoded string
'
' Description:
' Coerce 4 base 64 encoded bytes into 3 decoded bytes by converting 4, 6 bit
' values (0 to 63) into 3, 8 bit values. Transform the 8 bit value into its
' ascii character equivalent. Stop converting at the end of the input string
' or when the first '=' (equal sign) is encountered.
'
'******************************************************************************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' for each 4 character group....
For lPtr = 1 To Len(str2Decode) Step 4
iLen = 4
For iCtr = 0 To 3
' retrive the base 64 value, 4 at a time
iValue = InStr(1, BASE64CHR, Mid$(str2Decode, 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
' not found
Case 0: Exit Function
End Select
Next
' convert the 4, 6 bit values into 3, 8 bit values
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)
' add the three new characters to the output string
For iCtr = 1 To iLen - 1
strDecode = strDecode & Chr$(Bits(iCtr))
NextNextDecodeBase64String = strDecodeEnd Function
Public Function Base64_Encode(strSource) As String
Const BASE64_TABLE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim strTempLine As String
Dim j As Integer
For j = 1 To (Len(strSource) - Len(strSource) Mod 3) Step 3
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 _
+ Asc(Mid(strSource, j + 2, 1)) \ 64) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 2, 1)) Mod 64) + 1, 1)
Next j
If Not (Len(strSource) Mod 3) = 0 Then
If (Len(strSource) Mod 3) = 2 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 + 1, 1)
strTempLine = strTempLine & "="
ElseIf (Len(strSource) Mod 3) = 1 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, Asc(Mid(strSource, j, 1)) \ 4 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 + 1, 1)
strTempLine = strTempLine & "=="
End If
End If
Base64_Encode = strTempLine
End Function 'base64解密算法
Public Function DecodeBase64String(str2Decode As String) As String'******************************************************************************
'
' Synopsis: Decode a Base 64 string
'
' Parameters: str2Decode - The base 64 encoded input string
'
' Return: decoded string
'
' Description:
' Coerce 4 base 64 encoded bytes into 3 decoded bytes by converting 4, 6 bit
' values (0 to 63) into 3, 8 bit values. Transform the 8 bit value into its
' ascii character equivalent. Stop converting at the end of the input string
' or when the first '=' (equal sign) is encountered.
'
'******************************************************************************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' for each 4 character group....
For lPtr = 1 To Len(str2Decode) Step 4
iLen = 4
For iCtr = 0 To 3
' retrive the base 64 value, 4 at a time
iValue = InStr(1, BASE64CHR, Mid$(str2Decode, 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
' not found
Case 0: Exit Function
End Select
Next
' convert the 4, 6 bit values into 3, 8 bit values
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)
' add the three new characters to the output string
For iCtr = 1 To iLen - 1
strDecode = strDecode & Chr$(Bits(iCtr))
NextNextDecodeBase64String = strDecodeEnd Function
解决方案 »
- 安装程序无法创建以下文件夹%commfiles\microsoft shared\ADO
- VB
- VB6 执行bat文件时 无法将dos显示结果 输出到txt文本
- 急急,VB调用DELPHI写的标准DLL问题
- 有什么办法可以将数据库中的文件直接用OLE打开。。
- 百思不解,关于文件操作的问题,请帮下忙
- 请高手帮帮忙,我用winsock编了一个邮件发送的,应该如何添加发送附件
- picture1如何设置让图片自动大小适合picture1大小
- 请问哪个安装软件比较好?:
- 请问在win2000下怎么屏蔽键盘。?
- 怎样在vb中利用data控件导入access中的指定表的指定字段的内容,并赋值给一个指定变量
- 如何用VB代码创建一个ACCESS的MDB文件
http://www.aslike.net
Const BASE64_TABLE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim strTempLine As String
Dim j As Integer
For j = 1 To (Len(strSource) - Len(strSource) Mod 3) Step 3
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 _
+ Asc(Mid(strSource, j + 2, 1)) \ 64) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 2, 1)) Mod 64) + 1, 1)
Next j
If Not (Len(strSource) Mod 3) = 0 Then
If (Len(strSource) Mod 3) = 2 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 + 1, 1)
strTempLine = strTempLine & "="
ElseIf (Len(strSource) Mod 3) = 1 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, Asc(Mid(strSource, j, 1)) \ 4 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 + 1, 1)
strTempLine = strTempLine & "=="
End If
End If
Base64_Encode = strTempLine
End Function
===============================================================================
Public Function Base64_Decode(strSource As String) As String
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String For n = 1 To Len(strSource) Step 4
w1 = mimedecode(Mid$(strSource, n, 1))
w2 = mimedecode(Mid$(strSource, n + 1, 1))
w3 = mimedecode(Mid$(strSource, n + 2, 1))
w4 = mimedecode(Mid$(strSource, n + 3, 1))
If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) And 255))
If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) And 255))
If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255))
Next
Base64_Decode = retry
End Function
Private Function mimedecode(strSource As String) As Integer
If Len(strSource) = 0 Then mimedecode = -1: Exit Function
mimedecode = InStr(base64, strSource) - 1
End Function