猫七ASP版本的数据加密、解密类
在很多场合,特别重要的要加密传输。
使用成熟的加密算法是一个不错的选择,但是~ 有些算法这个语言支持而另一种语言不支持。
或者直接要安装某某组件,实在是太烦琐了,为了方便以后使用,自己抽空写了一个。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104'=====================================================================
'= 猫七数据加密、解密类 =
'= Copyright (c) 2011 猫七(QQ:77068320) All rights reserverd. =
'= 请尊重作者劳动成果,转载请保留代码的完整性 =
'=====================================================================
'= 作者:苗启源(博客:http://www.miaoqiyuan.cn) =
'= 发布地址:http://www.zzzyk.com/show/6bb4b497081dbd7b.htm
'= 最新:http://www.miaoqiyuan.cn/products/Catseven.Coding.rar
'=====================================================================
'= 文件名:Class.Catseven.Coding.asp =
'= 功 能:猫七数据加密、解密函数 =
'=====================================================================
class Catseven_Coding
public akey,ekey,keylen,keymax,autolen
'类初始化
' akey 编码表
' ekey 密钥
' keylen 标准编码长度
' keymax 最大编码长度,默认1000,代表1000-数据长度必须是3位数字,即数据的长度可以为0-900
' autolen 如果不足,是否补全
public sub class_initialize()
akey =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
ekey =
"f67RSTUOPDp02qd34ABbMijQFxyr5szZnot89+Y/=EghkVavwuHCXWmKLJNGIcel1"
keylen = 900
keymax = 1000
autolen= true
end sub
'函数:randkey
'功能:创建随机字符
'参数:rndkeylen 随机字符长度
private function randkey(byval rndkeylen)
dim rndnum,keymap,rstr,i
keymap =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
rstr =
""
for i = 1 to rndkeylen
randomize
rndnum = cLng(len(keymap) - 1) * rnd() + 1
rstr = rstr & mid(keymap,rndnum,1)
next
randkey = rstr
end function
'函数:encode
'功能:加密字符串
'参数:str 要加密的字符串
public function encode(byval str)
dim alen,i,vstr,kstr,vkey
str = replace(str,
"\","
")
alen = len(str)
if alen = 0 then
encode =
""
else
vstr =
""
vkey = randkey(1)
for i = 1 to len(str)
kstr = mid(str,i,1)
vstr = vstr & mid(akey,instr(ekey,kstr),1)
next
alen = keymax - alen
vstr = len(alen) & vkey & randkey(9 - len(keymax)) & alen & UCase(vkey) & vstr
alen = len(vstr)
if keylen < alen then keylen = alen
if keylen > keymax * 0.9 then keylen = keymax * 0.9
vstr = mid(vstr,1,keylen)
if alen < keylen and autolen = true then
vstr = vstr & randkey(keylen - alen)
end if
encode = vstr
end if
end function
'函数:decode
'功能:解密字符串
'参数:str 要解密的字符串
public function decode(byval str)
dim alen,i,vstr,kstr,vkey
alen = mid(str,1,1)
vkey = mid(str,2,1)
if not isnumeric(alen) then
decode =
""
else
alen = mid(str,11 - alen,alen)
if not isnumeric(alen) then
decode =
""
else
alen = keymax - alen + 1
str = mid(str,11,alen)
if mid(str,1,1) = UCase(vkey) then
vstr =
""
str = mid(str,2)
for i = 1 to len(str)
kstr = mid(str,i,1)
vstr = vstr & mid(ekey,instr(akey,kstr),1)
next
decode = vstr
else
decode =
""
end if
end if
end if
end function
end class
使用的时候比较简单,只支持英文加密,所以使用的时候,请配合base64函数(相关代码:http://www.miaoqiyuan.cn/p/tag/base64)使用。加密的时候用encode。
123456
set a = new Catseven_Coding
a.autolen = true
a.keylen = 150
response.write a.encode(
"5qyi6L+O5ZKM5oiR6K6o6K6677yM5pys5paH5Y6f5paH5Zyw5Z2A77yaaHR0cDovL3d3dy5taWFvcWl5dWFuLmNuL3AvQ2F0c2V2ZW4tQ29kaW5n"
)
'base64编码后的字符
解密的时候直接用decode。
1234
set a = new Catseven_Coding
response.write a.decode(
"3kfRpeT888KcNaVB4lHcf3UchVDB3BhB3BBCCaUcKadcKuycmBAcKuycfawcfMRCCauuyDL9Jhv4POPOaciu1Yv91/cO1Yx426x4PRvXMYL9MtMf1QiXMksu1cgkwbbM5c2IZDXHMqcZSnkCb4C6LP"
)
'解码后,为base64编码
可以指定ekey指定对照表