ASP加密解密函数
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
[code]
<%
key="sdfjal;dfjaoiejojfafnsanf;aufweijoanfanfaieufowejrwnf;aupagyuweranfsajfagfuoag" '私钥
sub rc4initialize(strpwd,byref sbox,byref key)
dim tempswap
dim a
dim b
intlength = len(strpwd)
for a = 0 to 255
key(a) = asc(mid(strpwd, (a mod intlength)+1, 1))
sbox(a) = a
next
b = 0
for a = 0 to 255
b = (b + sbox(a) + key(a)) mod 256
tempswap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempswap
next
end sub
function encrypt(plaintxt, psw)
dim sbox(255)
dim key(255)
dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher
i = 0
j = 0
rc4initialize psw,sbox,key
for a = 1 to len(plaintxt)
i = (i + 1) mod 256
j = (j + sbox(i)) mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + (sbox(j) mod 256)) mod 256)
cipherby = asc(mid(plaintxt, a, 1)) xor k
cipher = cipher & ""&cipherby&";" 'eg: €
next
encrypt = cipher
end function
function decrypt(cryptxt, psw)
dim sbox(255)
dim key(255)
dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher
i = 0
j = 0
rc4initialize psw,sbox,key
crypchar=split(cryptxt,";",-1,1) '将密文拆分成每个密文字符放入数组crypchar,但是每个数组元素少一个分号“;”
crypttxtlen=ubound(crypchar) '返回数组的个数,即加密字符的个数+1(数组中最后一个元素为空)
for a = 1 to crypttxtlen
i = (i + 1) mod 256
j = (j + sbox(i)) mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + (sbox(j) mod 256)) mod 256)
tmpcryptxt=mid(crypchar(i-1),3)
cipherby = tmpcryptxt xor k
cipher = cipher & ""&cipherby&";" 'eg: €
next
decrypt = cipher
end function
if request.servervariables("request_method") = "post" then
plaintext=request.form("plaintext")
response.write "
输入的明文字符串是:"&plaintext cryptext=encrypt(plaintext,key) response.write " 加密后的密文字符串:" &cryptext decryptext=decrypt(cryptext,key) response.write " 解密输出的字符串是:" &decryptext end if %> 该文章在 2010/5/8 15:57:36 编辑过 |
关键字查询
相关文章
正在查询... |