郑州网站建设-凯讯公司网站宣传口号图片
郑州网站建设-凯讯公司网站电话标志图片
0371-53778175
15333818157
当前位置:网站首页 >> 建站知识 >> 网站技术 >>


ASP网页上常用的加密和解密方式:base64编解码的实现


来源:郑州凯讯网站    发布人:郑州凯讯公司    发布日期:2013-12-28

我们在从事网站建设工作的时候,有时候会用到为客户的某些信息进行加密的情况,而有时候这种加密还需要是可逆的,也就是说,对加密以后的结果还需要能够解密,也就是能够解码回原来的字符。在这种情况下,我们的选择并不多,因为有很多的加密技术是不可逆的,一旦加密了,就无法解密了。在这种情况下,我们就可以采取Base64编码的方式进行加密,然后使用Base64解码的方式进行解密。在ASP网页编程语言里,Base64的编码和解码的实现比较简单,稍微有ASP编程经验的技术人员都能够看的明白,下面,笔者整理了一下在郑州网站制作的时候的base64的编码和解码的代码,在下面列出来,各位读者可以参考一下,需要使用编码的可以参考编码部分的代码,需要使用解码的可以参考解码部分的代码。如果您对此技术已经比较熟练了,那么您可以不必花时间再研究本代码了,如果您对之还不太熟练,又对此感兴趣,那么笔者将很欣慰,希望能对同行们有所帮助。本代码,如果各位感兴趣,可以直接复制更您使用,笔者对此没有任何意见,只希望能为各位提供一些助益,我们一同在编程上面取得进步。

◆Base64编码代码:

<%
OPTION EXPLICIT
const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
dim newline
dim Base64EncMap(63)
dim Base64DecMap(127)

'功能初始化函数源代码
PUBLIC SUB Initialize()
'初始化变量
newline = "<P>" & chr(13) & chr(10)
dim max, idx
max = len(BASE_64_MAP_INIT)
for idx = 0 to max - 1
Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
next
for idx = 0 to max - 1
Base64DecMap(ASC(Base64EncMap(idx))) = idx
next
END SUB

'Base64编码(加密)函数源代码
PUBLIC FUNCTION base64Encode(srcstr)
if len(srcstr) = 0 then
base64Encode = ""
exit function
end if
dim ret, ndx, by3, first, second, third
by3 = (len(srcstr) \ 3) * 3
ndx = 1
do while ndx <= by3
first = asc(mid(srcstr, ndx+0, 1))
second = asc(mid(srcstr, ndx+1, 1))
third = asc(mid(srcstr, ndx+2, 1))
ret = ret & Base64EncMap( (first \ 4) AND 63 )
ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
ret = ret & Base64EncMap( third AND 63)
ndx = ndx + 3
loop
if by3 < len(srcstr) then
first = asc(mid(srcstr, ndx+0, 1))
ret = ret & Base64EncMap( (first \ 4) AND 63 )
if (len(srcstr) MOD 3 ) = 2 then
second = asc(mid(srcstr, ndx+1, 1))
ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
ret = ret & Base64EncMap( ((second * 4) AND 60) )
else
ret = ret & Base64EncMap( (first * 16) AND 48)
ret = ret '& "="
end if
ret = ret '& "="
end if
base64Encode = ret
END FUNCTION

'调用初始化功能函数
call Initialize
'以下为测试代码和测试数据
dim srcstring, encodestr
srcstring = "abcefghijklmn1234567890"
encodestr = base64Encode(srcstring)
response.write "编码(加密)前字符串为:" & srcstring
response write "<br>"
response.write "编码(加密)后字符串为:" & encodestr
%>

Base64解码代码:

<%
OPTION EXPLICIT
const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
dim newline
dim Base64EncMap(63)
dim Base64DecMap(127)

'功能初始化函数源代码
PUBLIC SUB Initialize()
'初始化变量
newline = "<P>" & chr(13) & chr(10)
dim max, idx
max = len(BASE_64_MAP_INIT)
for idx = 0 to max - 1
Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
next
for idx = 0 to max - 1
Base64DecMap(ASC(Base64EncMap(idx))) = idx
next
END SUB

'Base64解码(解密)函数源代码
PUBLIC FUNCTION base64Decode(srcstr)
if len(srcstr) = 0 then
base64Decode = ""
exit function
end if
dim realLen
realLen = len(srcstr)
do while mid(srcstr, realLen, 1) = "="
realLen = realLen - 1
loop
dim ret, ndx, by4, first, second, third, fourth
ret = ""
by4 = (realLen \ 4) * 4
ndx = 1
do while ndx <= by4
first = Base64DecMap(asc(mid(srcstr, ndx+0, 1)))
second = Base64DecMap(asc(mid(srcstr, ndx+1, 1)))
third = Base64DecMap(asc(mid(srcstr, ndx+2, 1)))
fourth = Base64DecMap(asc(mid(srcstr, ndx+3, 1)))
ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
ret = ret & chr( ((third * 64) AND 255) + (fourth AND 63))
ndx = ndx + 4
loop
if ndx < realLen then
first = Base64DecMap(asc(mid(srcstr, ndx+0, 1)))
second = Base64DecMap(asc(mid(srcstr, ndx+1, 1)))
ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
if realLen MOD 4 = 3 then
third = Base64DecMap(asc(mid(srcstr,ndx+2,1)))
ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
end if
end if
base64Decode = ret
END FUNCTION

'调用功能初始化函数
call Initialize

'下面是测试代码和测试数据
dim srctring, encodestr
srctring = "abcefghijklmn1234567890"
encodestr = base64Encode(srctring)
response.write "编码(加密)前字符串为:" & srctring
response.write "编码(加密)后字符串为:" & encodestr
response.write "解码(解密)后字符串为:" & base64Decode(encodestr)
%>

本站优惠活动图片



业务联系方式图片


本页页脚banner图片

咨询热线:0371-53778175,15333818157    业务QQ:业务联系QQ图标1765879842   业务联系QQ图标2632505191   业务联系QQ图标2236519391

版权所有:郑州凯讯通信科技有限公司    备案号:豫ICP备11027744号-2