在asp中通过vbs类实现rsa加密与解密的代码

2018-09-06 12:52

阅读:101

  在asp中通过vbs类实现rsa加密与解密,建议入精华

本文章有两文件组成
test.asp测试演示文件
clsrsa.asp实现rsa加密与解密的vbs类文件
下面是代码:

1.test.asp

复制代码 代码如下:
<%
rem文章标题:在asp中通过vbs类实现rsa加密与解密
rem收集整理:yanek
rem联系nbsp;

%>
<%OptionExplicit%>
<!--#INCLUDEFILE=clsRSA.asp-->
<%

DimLngKeyE
DimLngKeyD
DimLngKeyN
DimStrMessage
DimObjRSA
IfNotRequest.Form=Then

LngKeyE=Request.Form(KeyE)
LngKeyD=Request.Form(KeyD)
LngKeyN=Request.Form(KeyN)
StrMessage=Request.Form(Message)

SetObjRSA=NewclsRSA

SelectCaseRequest.Form(Action)
CaseGenerateKeys
CallObjRSA.GenKey()
LngKeyE=ObjRSA.PublicKey
LngKeyD=ObjRSA.PrivateKey
LngKeyN=ObjRSA.Modulus
CaseEncrypt
ObjRSA.PublicKey=LngKeyE
ObjRSA.Modulus=LngKeyN
StrMessage=ObjRSA.Encode(StrMessage)
CaseDecrypt
ObjRSA.PrivateKey=LngKeyD
ObjRSA.Modulus=LngKeyN
StrMessage=ObjRSA.Decode(StrMessage)
EndSelect

SetObjRSA=Nothing

EndIf
%>
<HTML>
<HEAD>
<TITLE>RSACipherDemonstration</TITLE>
</HEAD>
<BODY>
<H1>RSACipherDemonstration</H1>
<P>
Youwillfirstneedtogenerateyourpublic/privagekey-pair
beforeyoucanencrypt/decryptmessages.
</P>
<FORMmethod=post>
<TABLE>
<TR>
<TD>PublicKey</TD>
<TD><INPUTname=KeyEvalue=<%=Server.HTMLEncode(LngKeyE)%>></TD>
<TDrowspan=3>
<INPUTtype=Submitname=Actionvalue=GenerateKeys>
</TD>
</TR>
<TR>
<TD>PrivateKey</TD>
<TD><INPUTname=KeyDvalue=<%=Server.HTMLEncode(LngKeyD)%>></TD>
</TR>
<TR>
<TD>Modulus</TD>
<TD><INPUTname=KeyNvalue=<%=Server.HTMLEncode(LngKeyN)%>></TD>
</TR>
<TR>
<TDcolspan=3>
TestMessage:<BR>
<TEXTAREAname=Messagecols=50rows=7><%=Server.HTMLEncode(StrMessage)%></TEXTAREA>
</TD>
</TR>
<TR>
<TDalign=rightcolspan=3>
<INPUTtype=Submitname=Actionvalue=Encrypt>
<INPUTtype=Submitname=Actionvalue=Decrypt>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>



clsrsa.asp

复制代码 代码如下:
<%
rem实现rsa加密与解密的vbs类文件
rem文章标题:在asp中通过vbs类实现rsa加密与解密
rem收集整理:yanek
rem联系nbsp;

RSAEncryptionClass

.PrivateKey
Yourpersonalprivatekey.Keepthishidden.

.PublicKey
Keyforotherstoencryptdatawith.

.Modulus
Usedwithbothpublicandprivatekeyswhenencrypting
anddecryptingdata.

.GenKey()
CreatesPublic/PrivatekeysetandModulus

.Crypt(pLngMessage,pLngKey)
Encrypts/Decryptsmessageandreturns
asastring.

.Encode(pStrMessage)
Encryptsmessageandreturnsindouble-hexformat

.Decode(pStrMessage)
Decryptsmessagefromdouble-hexformatandreturnsastring

ClassclsRSA

PublicPrivateKey
PublicPublicKey
PublicModulus

PublicSubGenKey()
DimlLngPhi
Dimq
Dimp

Randomize

Do
Do

2randomprimarynumbers(0to1000)
Do
p=Rnd*1000\1
LoopWhileNotIsPrime(p)

Do
q=Rnd*1000\1
LoopWhileNotIsPrime(q)


n=productof2primes
Modulus=p*q\1

randomdecryptor(2ton)
PrivateKey=Rnd*(Modulus-2)\1+2

lLngPhi=(p-1)*(q-1)\1
PublicKey=Euler(lLngPhi,PrivateKey)

LoopWhilePublicKey=0OrPublicKey=1

Loopifwecantcrypt/decryptabyte
LoopWhileNotTestCrypt(255)

EndSub

PrivateFunctionTestCrypt(ByRefpBytData)
DimlStrCrypted
lStrCrypted=Crypt(pBytData,PublicKey)
TestCrypt=Crypt(lStrCrypted,PrivateKey)=pBytData
EndFunction

PrivateFunctionEuler(ByRefpLngPHI,ByRefpLngKey)

DimlLngR(3)
DimlLngP(3)
DimlLngQ(3)

DimlLngCounter
DimlLngResult

Euler=0

lLngR(1)=pLngPHI:lLngR(0)=pLngKey
lLngP(1)=0:lLngP(0)=1
lLngQ(1)=2:lLngQ(0)=0

lLngCounter=-1

DoUntillLngR(0)=0

lLngR(2)=lLngR(1):lLngR(1)=lLngR(0)
lLngP(2)=lLngP(1):lLngP(1)=lLngP(0)
lLngQ(2)=lLngQ(1):lLngQ(1)=lLngQ(0)

lLngCounter=lLngCounter+1

lLngR(0)=lLngR(2)ModlLngR(1)
lLngP(0)=((lLngR(2)\lLngR(1))*lLngP(1))+lLngP(2)
lLngQ(0)=((lLngR(2)\lLngR(1))*lLngQ(1))+lLngQ(2)

Loop

lLngResult=(pLngKey*lLngP(1))-(pLngPHI*lLngQ(1))

IflLngResult>0Then
Euler=lLngP(1)
Else
Euler=Abs(lLngP(1))+pLngPHI
EndIf

EndFunction

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

PrivateFunctionIsPrime(ByRefpLngNumber)
DimlLngSquare
DimlLngIndex
IsPrime=False
IfpLngNumber<2ThenExitFunction
IfpLngNumberMod2=0ThenExitFunction
lLngSquare=Sqr(pLngNumber)
ForlLngIndex=3TolLngSquareStep2
IfpLngNumberModlLngIndex=0ThenExitFunction
Next
IsPrime=True
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
%>
演示地址:


评论


乖,登录后才可以留言!