一段在asp中加密与解密对应的函数
程序员文章站
2023-12-03 11:22:10
在asp中加密方法有对应的解密方法好象不多,现在根据前辈资料整理出在asp中加密与解密函数 rsa.asp <% rem 在asp中实现加密与解密,加密方法...
在asp中加密方法有对应的解密方法好象不多,现在根据前辈资料整理出在asp中加密与解密函数
rsa.asp
<%
rem 在asp中实现加密与解密,加密方法:根据rsa
rem 联系:hnsoso@sina.com
class clsrsa
public privatekey
public publickey
public modulus
public function crypt(plngmessage, plngkey)
on error resume next
dim llngmod
dim llngresult
dim llngindex
if plngkey mod 2 = 0 then
llngresult = 1
for llngindex = 1 to plngkey / 2
llngmod = (plngmessage ^ 2) mod modulus
' mod may error on key generation
llngresult = (llngmod * llngresult) mod modulus
if err then exit function
next
else
llngresult = plngmessage
for llngindex = 1 to plngkey / 2
llngmod = (plngmessage ^ 2) mod modulus
on error resume next
' mod may error on key generation
llngresult = (llngmod * llngresult) mod modulus
if err then exit function
next
end if
crypt = llngresult
end function
public function encode(byval pstrmessage)
dim llngindex
dim llngmaxindex
dim lbytascii
dim llngencrypted
llngmaxindex = len(pstrmessage)
if llngmaxindex = 0 then exit function
for llngindex = 1 to llngmaxindex
lbytascii = asc(mid(pstrmessage, llngindex, 1))
llngencrypted = crypt(lbytascii, publickey)
encode = encode & numbertohex(llngencrypted, 4)
next
end function
public function decode(byval pstrmessage)
dim lbytascii
dim llngindex
dim llngmaxindex
dim llngencrypteddata
decode = ""
llngmaxindex = len(pstrmessage)
for llngindex = 1 to llngmaxindex step 4
llngencrypteddata = hextonumber(mid(pstrmessage, llngindex, 4))
lbytascii = crypt(llngencrypteddata, privatekey)
decode = decode & chr(lbytascii)
next
end function
private function numbertohex(byref plngnumber, byref plnglength)
numbertohex = right(string(plnglength, "0") & hex(plngnumber), plnglength)
end function
private function hextonumber(byref pstrhex)
hextonumber = clng("&h" & pstrhex)
end function
end class
%>
test.asp
<!--#include file="rsa.asp"-->
<%
function encryptstr(message)
dim lngkeye
dim lngkeyd
dim lngkeyn
dim strmessage
dim objrsa
lngkeye = "32823"
lngkeyd = "20643"
lngkeyn = "29893"
strmessage = message
set objrsa = new clsrsa
objrsa.publickey = lngkeye
objrsa.modulus = lngkeyn
encryptstr = objrsa.encode(strmessage)
set objrsa = nothing
end function
function decryptstr(message)
dim lngkeye
dim lngkeyd
dim lngkeyn
dim strmessage
dim objrsa
lngkeye = "32823"
lngkeyd = "20643"
lngkeyn = "29893"
strmessage = message
set objrsa = new clsrsa
objrsa.privatekey =lngkeyd
objrsa.modulus=lngkeyn
decryptstr=objrsa.decode(strmessage)
set objrsa = nothing
end function
dim last,first
first="sohu"
response.write "加密前为:"&first
last=encryptstr(first)
response.write "加密后为"&last
response.write "解密后为" &decryptstr(last)
%>
rsa.asp
<%
rem 在asp中实现加密与解密,加密方法:根据rsa
rem 联系:hnsoso@sina.com
class clsrsa
public privatekey
public publickey
public modulus
public function crypt(plngmessage, plngkey)
on error resume next
dim llngmod
dim llngresult
dim llngindex
if plngkey mod 2 = 0 then
llngresult = 1
for llngindex = 1 to plngkey / 2
llngmod = (plngmessage ^ 2) mod modulus
' mod may error on key generation
llngresult = (llngmod * llngresult) mod modulus
if err then exit function
next
else
llngresult = plngmessage
for llngindex = 1 to plngkey / 2
llngmod = (plngmessage ^ 2) mod modulus
on error resume next
' mod may error on key generation
llngresult = (llngmod * llngresult) mod modulus
if err then exit function
next
end if
crypt = llngresult
end function
public function encode(byval pstrmessage)
dim llngindex
dim llngmaxindex
dim lbytascii
dim llngencrypted
llngmaxindex = len(pstrmessage)
if llngmaxindex = 0 then exit function
for llngindex = 1 to llngmaxindex
lbytascii = asc(mid(pstrmessage, llngindex, 1))
llngencrypted = crypt(lbytascii, publickey)
encode = encode & numbertohex(llngencrypted, 4)
next
end function
public function decode(byval pstrmessage)
dim lbytascii
dim llngindex
dim llngmaxindex
dim llngencrypteddata
decode = ""
llngmaxindex = len(pstrmessage)
for llngindex = 1 to llngmaxindex step 4
llngencrypteddata = hextonumber(mid(pstrmessage, llngindex, 4))
lbytascii = crypt(llngencrypteddata, privatekey)
decode = decode & chr(lbytascii)
next
end function
private function numbertohex(byref plngnumber, byref plnglength)
numbertohex = right(string(plnglength, "0") & hex(plngnumber), plnglength)
end function
private function hextonumber(byref pstrhex)
hextonumber = clng("&h" & pstrhex)
end function
end class
%>
test.asp
<!--#include file="rsa.asp"-->
<%
function encryptstr(message)
dim lngkeye
dim lngkeyd
dim lngkeyn
dim strmessage
dim objrsa
lngkeye = "32823"
lngkeyd = "20643"
lngkeyn = "29893"
strmessage = message
set objrsa = new clsrsa
objrsa.publickey = lngkeye
objrsa.modulus = lngkeyn
encryptstr = objrsa.encode(strmessage)
set objrsa = nothing
end function
function decryptstr(message)
dim lngkeye
dim lngkeyd
dim lngkeyn
dim strmessage
dim objrsa
lngkeye = "32823"
lngkeyd = "20643"
lngkeyn = "29893"
strmessage = message
set objrsa = new clsrsa
objrsa.privatekey =lngkeyd
objrsa.modulus=lngkeyn
decryptstr=objrsa.decode(strmessage)
set objrsa = nothing
end function
dim last,first
first="sohu"
response.write "加密前为:"&first
last=encryptstr(first)
response.write "加密后为"&last
response.write "解密后为" &decryptstr(last)
%>