在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)
%>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器