ASP Base64位 加密解密

网上找了很多,运行时都会提示某个错误,有点乱。后面找到测试,这个转ASNI码的能实现

sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS) Function strUnicodeLen(asContents)
'计算unicode字符串的Ansi编码的长度
asContents1="a"&asContents
len1=len(asContents1)
k=
for i= to len1
asc1=asc(mid(asContents1,i,))
if asc1< then asc1=+asc1
if asc1> then
k=k+
else
k=k+
end if
next
strUnicodeLen=k-
End Function Function strUnicode2Ansi(asContents)
'将Unicode编码的字符串,转换成Ansi编码的字符串
strUnicode2Ansi=""
len1=len(asContents)
for i= to len1
varchar=mid(asContents,i,)
varasc=asc(varchar)
if varasc< then varasc=varasc+
if varasc> then
varHex=Hex(varasc)
varlow=left(varHex,)
varhigh=right(varHex,)
strUnicode2Ansi=strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh )
else
strUnicode2Ansi=strUnicode2Ansi & chrb(varasc)
end if
next
End function Function strAnsi2Unicode(asContents)
'将Ansi编码的字符串,转换成Unicode编码的字符串
strAnsi2Unicode = ""
len1=lenb(asContents)
if len1= then exit function
for i= to len1
varchar=midb(asContents,i,)
varasc=ascb(varchar)
if varasc > then
strAnsi2Unicode = strAnsi2Unicode & chr(ascw(midb(asContents,i+,) & varchar))
i=i+
else
strAnsi2Unicode = strAnsi2Unicode & chr(varasc)
end if
next
End function Function Base64encode(asContents)
'将Ansi编码的字符串进行Base64编码
'asContents应当是ANSI编码的字符串(二进制的字符串也可以)
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< then
Base64encode=""
exit Function
end if m3=Len1 Mod
If M3 > Then asContents = asContents & String(-M3, chrb())
'补足位数是为了便于计算 IF m3 > THEN
len1=len1+(-m3)
len2=len1-
else
len2=len1
end if lsResult = "" For lnPosition = To len2 Step
lsGroup64 = ""
lsGroupBinary = Midb(asContents, lnPosition, ) Byte1 = Ascb(Midb(lsGroupBinary, , )): SaveBits1 = Byte1 And
Byte2 = Ascb(Midb(lsGroupBinary, , )): SaveBits2 = Byte2 And
Byte3 = Ascb(Midb(lsGroupBinary, , )) Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And ) / ) + , )
Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And ) / ) Or (SaveBits1 * ) And &HFF) + , )
Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And ) / ) Or (SaveBits2 * ) And &HFF) + , )
Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And ) + , )
lsGroup64 = Char1 & Char2 & Char3 & Char4 lsResult = lsResult & lsGroup64
Next '处理最后剩余的几个字符
if M3 > then
lsGroup64 = ""
lsGroupBinary = Midb(asContents, len2+, ) Byte1 = Ascb(Midb(lsGroupBinary, , )): SaveBits1 = Byte1 And
Byte2 = Ascb(Midb(lsGroupBinary, , )): SaveBits2 = Byte2 And
Byte3 = Ascb(Midb(lsGroupBinary, , )) Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And ) / ) + , )
Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And ) / ) Or (SaveBits1 * ) And &HFF) + , )
Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And ) / ) Or (SaveBits2 * ) And &HFF) + , ) if M3= then
lsGroup64 = Char1 & Char2 & ChrB() & ChrB() '用=号补足位数
else
lsGroup64 = Char1 & Char2 & Char3 & ChrB() '用=号补足位数
end if
lsResult = lsResult & lsGroup64
end if
Base64encode = lsResult
End Function Function Base64decode(asContents)
'将Base64编码字符串转换成Ansi编码的字符串
'asContents应当也是ANSI编码的字符串(二进制的字符串也可以)
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 if len1 < or M4 > then
'字符串长度应当是4的倍数
Base64decode = ""
exit Function
end if '判断最后一位是不是 = 号
'判断倒数第二位是不是 = 号
'这里m4表示最后剩余的需要单独处理的字符个数
if midb(asContents, len1, ) = chrb() then m4=
if midb(asContents, len1-, ) = chrb() then m4= if m4 = then
len2=len1
else
len2=len1-
end if For lnPosition = To Len2 Step
lsGroupBinary = ""
lsGroup64 = Midb(asContents, lnPosition, )
Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Byte1 = Chrb(((Char2 And ) / ) Or (Char1 * ) And &HFF)
Byte2 = lsGroupBinary & Chrb(((Char3 And ) / ) Or (Char2 * ) And &HFF)
Byte3 = Chrb((((Char3 And ) * ) And &HFF) Or (Char4 And ))
lsGroupBinary = Byte1 & Byte2 & Byte3 lsResult = lsResult & lsGroupBinary
Next '处理最后剩余的几个字符
if M4 > then
lsGroupBinary = ""
lsGroup64 = Midb(asContents, len2+, m4) & chrB() 'chr(65)=A,转换成值为0
if M4= then '补足4位,是为了便于计算
lsGroup64 = lsGroup64 & chrB()
end if
Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, , )) -
Byte1 = Chrb(((Char2 And ) / ) Or (Char1 * ) And &HFF)
Byte2 = lsGroupBinary & Chrb(((Char3 And ) / ) Or (Char2 * ) And &HFF)
Byte3 = Chrb((((Char3 And ) * ) And &HFF) Or (Char4 And )) if M4= then
lsGroupBinary = Byte1
elseif M4= then
lsGroupBinary = Byte1 & Byte2
end if lsResult = lsResult & lsGroupBinary
end if Base64decode = lsResult End Function

调用如下:

'加密
strAnsi2Unicode(Base64encode(strUnicode2Ansi("加密字符串")))
'解密
strAnsi2Unicode(Base64decode(strUnicode2Ansi("解密字符串")))
上一篇:php中类继承和接口继承的对比


下一篇:Vue.js_构造器及其实例化概念