欢迎光临
我们一直在努力

base64编码、解码函数-ASP教程,安全加密

建站超值云服务器,限时71元/月

base64编码、解码函数

这是我看完几个base64编码、解码函数后自己改写的。

因为,在中文操作系统的vbscript中,使用的是unicode字符集,所以

很多base64编码、解码函数在理论上是正确的,但实际不能运行!

文件名称base64test.asp

<%

sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"

sbase_64_characters = strunicode2ansi(sbase_64_characters)

function strunicodelen(ascontents)

计算unicode字符串的ansi编码的长度

ascontents1="a"&ascontents

len1=len(ascontents1)

k=0

for i=1 to len1

asc1=asc(mid(ascontents1,i,1))

if asc1<0 then asc1=65536+asc1

if asc1>255 then

k=k+2

else

k=k+1

end if

next

strunicodelen=k-1

end function

function strunicode2ansi(ascontents)

将unicode编码的字符串,转换成ansi编码的字符串

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

function stransi2unicode(ascontents)

将ansi编码的字符串,转换成unicode编码的字符串

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

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<1 then

base64encode=""

exit function

end if

m3=len1 mod 3

if m3 > 0 then ascontents = ascontents & string(3-m3, chrb(0))

补足位数是为了便于计算

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

处理最后剩余的几个字符

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) 用=号补足位数

else

lsgroup64 = char1 & char2 & char3 & chrb(61) 用=号补足位数

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 4

if len1 < 1 or m4 > 0 then

字符串长度应当是4的倍数

base64decode = ""

exit function

end if

判断最后一位是不是 = 号

判断倒数第二位是不是 = 号

这里m4表示最后剩余的需要单独处理的字符个数

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

处理最后剩余的几个字符

if m4 > 0 then

lsgroupbinary = ""

lsgroup64 = midb(ascontents, len2+1, m4) & chrb(65) chr(65)=a,转换成值为0

if m4=2 then 补足4位,是为了便于计算

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

base64decode = lsresult

end function

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » base64编码、解码函数-ASP教程,安全加密
分享到: 更多 (0)

相关推荐

  • 暂无文章