Base64位加密解密算法,支持中文,[VB-Dll & ASP].

[idea] 经过一天的研究,终于成功了,关键在CHRB()和CHR()的用法上,我都做了注释,各位拿了去自己研究研究吧. [idea]
另外我这还有VB版的,可以生成DLL然后与ASP结合使用,需要的朋友留个声儿,说一下. [heart]
‘================================================
‘Edit by jena.want at 2006-09-07
‘=============http:www.ishere.cn=================
‘================================================
Dim sBASE_64_CHARACTERS
sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)

Private Function strUnicode2Ansi(asContents)
‘convert unicode string to ansi string
strUnicode2Ansi = ""
len1 = Len(asContents)
For i = 1 To len1
VarChar = Mid(asContents, i, 1)
varasc = Asc(VarChar)
If varasc < 0 Then varasc = varasc + 65536
If varasc > 255 Then
varHex = Hex(varasc)
varlow = Left(varHex, 2)
varhigh = Right(varHex, 2)
strUnicode2Ansi = strUnicode2Ansi & ChrB("&H" & varlow) & ChrB("&H" & varhigh)
Else
strUnicode2Ansi = strUnicode2Ansi & ChrB(varasc)
End If
Next
End Function

Private Function strAnsi2Unicode(asContents)
‘convert ansi string to unicode string
strAnsi2Unicode = ""
len1 = LenB(asContents)
If len1 = 0 Then Exit Function
For i = 1 To len1
VarChar = MidB(asContents, i, 1)
varasc = AscB(VarChar)
If varasc > 127 Then
strAnsi2Unicode = strAnsi2Unicode & Chr(AscW(MidB(asContents, i + 1, 1) & VarChar))
i = i + 1
Else
strAnsi2Unicode = strAnsi2Unicode & Chr(varasc)
End If
Next
End Function

Public Function EncryptText(asContents)
‘let the ansicode string put up coding with base64bit
Dim lnPosition
Dim lsResult
Dim Char1
Dim Char2
Dim Char3
Dim Char4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsGroupBinary
Dim lsGroup64
Dim M4, len1, len2

len1 = LenB(asContents)
If len1 < 1 Then
EncryptText = ""
Exit Function
End If

M3 = len1 Mod 3
‘If M3 > 0 Then asContents = asContents & String(3 – M3, Chrb(0))
‘Don’t use Chrb() In Ansicode format case , use chr() , and convert to Ansicode format later .2006-09-08 jena.want
If M3 > 0 Then asContents = asContents & strUnicode2Ansi(String(3 – M3, Chr(0)))
‘complement digit easy to count

If M3 > 0 Then
len1 = len1 + (3 – M3)
len2 = len1 – 3
Else
len2 = len1
End If

lsResult = ""

For lnPosition = 1 To len2 Step 3
lsGroup64 = ""
lsGroupBinary = MidB(asContents, lnPosition, 3)

Byte1 = AscB(MidB(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = AscB(MidB(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = AscB(MidB(lsGroupBinary, 3, 1))

Char1 = MidB(sBASE_64_CHARACTERS, ((Byte1 And 252) 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERS, (((Byte2 And 240) 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERS, (((Byte3 And 192) 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = MidB(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
lsGroup64 = Char1 & Char2 & Char3 & Char4

lsResult = lsResult & lsGroup64
Next

‘dispose the last residual chars
If M3 > 0 Then
lsGroup64 = ""
lsGroupBinary = MidB(asContents, len2 + 1, 3)

Byte1 = AscB(MidB(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = AscB(MidB(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = AscB(MidB(lsGroupBinary, 3, 1))

Char1 = MidB(sBASE_64_CHARACTERS, ((Byte1 And 252) 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERS, (((Byte2 And 240) 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERS, (((Byte3 And 192) 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)

If M3 = 1 Then
lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) ‘use "=" complement digit
Else
lsGroup64 = Char1 & Char2 & Char3 & ChrB(61) ‘use "=" complement digit
End If

lsResult = lsResult & lsGroup64
End If
‘convert ansicode format to unicode format and put out
EncryptText = strAnsi2Unicode(lsResult)

End Function

Public Function DecryptText(asContents)
‘let the base64bit format string convert to ansicode format
asContents = strUnicode2Ansi(asContents)
Dim lsResult
Dim lnPosition
Dim lsGroup64, lsGroupBinary
Dim Char1, Char2, Char3, Char4
Dim Byte1, Byte2, Byte3
Dim M4, len1, len2

len1 = LenB(asContents)
M4 = len1 Mod 4

‘the string’s length should be multiple with 4
If len1 < 1 Or M4 > 0 Then
DecryptText = ""
Exit Function
End If

‘judge the last bit is "=" or not
‘judge the reciprocal second bit is "=" or not
‘this "m4" means the count of chars where last residual width dispose by oneself
If MidB(asContents, len1, 1) = ChrB(61) Then M4 = 3
If MidB(asContents, len1 – 1, 1) = ChrB(61) Then M4 = 2

If M4 = 0 Then
len2 = len1
Else
len2 = len1 – 4
End If

For lnPosition = 1 To len2 Step 4
lsGroupBinary = ""
lsGroup64 = MidB(asContents, lnPosition, 4)
Char1 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 1, 1)) – 1
Char2 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 2, 1)) – 1
Char3 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 3, 1)) – 1
Char4 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 4, 1)) – 1
Byte1 = ChrB(((Char2 And 48) 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & ChrB(((Char3 And 60) 4) Or (Char2 * 16) And &HFF)
Byte3 = ChrB((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
lsGroupBinary = Byte1 & Byte2 & Byte3

lsResult = lsResult & lsGroupBinary
Next

‘dispose the last residual chars
If M4 > 0 Then
lsGroupBinary = ""
lsGroup64 = MidB(asContents, len2 + 1, M4) & ChrB(65) ‘chr(65)=A convert to value is 0
If M4 = 2 Then ‘complement digit to 4 for easy to count
lsGroup64 = lsGroup64 & ChrB(65)
End If
Char1 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 1, 1)) – 1
Char2 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 2, 1)) – 1
Char3 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 3, 1)) – 1
Char4 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 4, 1)) – 1
Byte1 = ChrB(((Char2 And 48) 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & ChrB(((Char3 And 60) 4) Or (Char2 * 16) And &HFF)
Byte3 = ChrB((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))

If M4 = 2 Then
lsGroupBinary = Byte1
ElseIf M4 = 3 Then
lsGroupBinary = Byte1 & Byte2
End If

lsResult = lsResult & lsGroupBinary
End If

DecryptText = strAnsi2Unicode(lsResult)

End Function

Leave a Comment

Your email address will not be published.

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据