在ASP中加密方法有对应的解密方法好象不多,现在根据前辈资料整理出在asp中加密与解密函数
rsa.asp
<%
rem在ASP中实现加密与解密,加密方法:根据RSA
rem联系:hnsoso@sina.com
ClassclsRSA
PublicPrivateKey
PublicPublicKey
PublicModulus
PublicFunctionCrypt(pLngMessage,pLngKey)
OnErrorResumeNext
DimlLngMod
DimlLngResult
DimlLngIndex
IfpLngKeyMod2=0Then
lLngResult=1
ForlLngIndex=1TopLngKey/2
lLngMod=(pLngMessage^2)ModModulus
'Modmayerroronkeygeneration
lLngResult=(lLngMod*lLngResult)ModModulus
IfErrThenExitFunction
Next
Else
lLngResult=pLngMessage
ForlLngIndex=1TopLngKey/2
lLngMod=(pLngMessage^2)ModModulus
OnErrorResumeNext
'Modmayerroronkeygeneration
lLngResult=(lLngMod*lLngResult)ModModulus
IfErrThenExitFunction
Next
EndIf
Crypt=lLngResult
EndFunction
PublicFunctionEncode(ByValpStrMessage)
DimlLngIndex
DimlLngMaxIndex
DimlBytAscii
DimlLngEncrypted
lLngMaxIndex=Len(pStrMessage)
IflLngMaxIndex=0ThenExitFunction
ForlLngIndex=1TolLngMaxIndex
lBytAscii=Asc(Mid(pStrMessage,lLngIndex,1))
lLngEncrypted=Crypt(lBytAscii,PublicKey)
Encode=Encode&NumberToHex(lLngEncrypted,4)
Next
EndFunction
PublicFunctionDecode(ByValpStrMessage)
DimlBytAscii
DimlLngIndex
DimlLngMaxIndex
DimlLngEncryptedData
Decode=""
lLngMaxIndex=Len(pStrMessage)
ForlLngIndex=1TolLngMaxIndexStep4
lLngEncryptedData=HexToNumber(Mid(pStrMessage,lLngIndex,4))
lBytAscii=Crypt(lLngEncryptedData,PrivateKey)
Decode=Decode&Chr(lBytAscii)
Next
EndFunction
PrivateFunctionNumberToHex(ByRefpLngNumber,ByRefpLngLength)
NumberToHex=Right(String(pLngLength,"0")&Hex(pLngNumber),pLngLength)
EndFunction
PrivateFunctionHexToNumber(ByRefpStrHex)
HexToNumber=CLng("&h"&pStrHex)
EndFunction
EndClass
%>
test.asp
<>
<%
functionEncryptstr(Message)
DimLngKeyE
DimLngKeyD
DimLngKeyN
DimStrMessage
DimObjRSA
LngKeyE="32823"
LngKeyD="20643"
LngKeyN="29893"
StrMessage=Message
SetObjRSA=NewclsRSA
ObjRSA.PublicKey=LngKeyE
ObjRSA.Modulus=LngKeyN
Encryptstr=ObjRSA.Encode(StrMessage)
SetObjRSA=Nothing
endfunction
functiondecryptstr(Message)
DimLngKeyE
DimLngKeyD
DimLngKeyN
DimStrMessage
DimObjRSA
LngKeyE="32823"
LngKeyD="20643"
LngKeyN="29893"
StrMessage=Message
SetObjRSA=NewclsRSA
ObjRSA.PrivateKey=LngKeyD
ObjRSA.Modulus=LngKeyN
decryptstr=ObjRSA.Decode(StrMessage)
SetObjRSA=Nothing
endfunction
dimlast,first
first="sohu"
Response.Write"加密前为:"&first
last=Encryptstr(first)
Response.Write"加密后为"&last
Response.Write"解密后为"&decryptstr(last)
%>