在ASP中實現RsA加密與解密

來源:互聯網
上載者:User
加密|解密 文章有兩檔案組成,test.asp 測試示範檔案;clsrsa.asp 實現rsa加密與解密的vbs類檔案
下面是代碼:

  1. test.asp

<%
rem 文章標題:在asp中通過vbs類實現rsa加密與解密

%>
<%Option Explicit%>
<!--#INCLUDE FILE="clsRSA.asp"-->
<%

Dim LngKeyE
Dim LngKeyD
Dim LngKeyN
Dim StrMessage
Dim ObjRSA
If Not Request.Form = "" Then

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

Set ObjRSA = New clsRSA

Select Case Request.Form("Action")
Case "Generate Keys"
Call ObjRSA.GenKey()
LngKeyE = ObjRSA.PublicKey
LngKeyD = ObjRSA.PrivateKey
LngKeyN = ObjRSA.Modulus
Case "Encrypt"
ObjRSA.PublicKey = LngKeyE
ObjRSA.Modulus = LngKeyN
StrMessage = ObjRSA.Encode(StrMessage)
Case "Decrypt"
ObjRSA.PrivateKey = LngKeyD
ObjRSA.Modulus = LngKeyN
StrMessage = ObjRSA.Decode(StrMessage)
End Select

Set ObjRSA = Nothing

End If
%>
<HTML>
<HEAD>
<TITLE>RSA Cipher Demonstration</TITLE>
</HEAD>
<BODY>
<H1>RSA Cipher Demonstration</H1>
<P>
You will first need to generate your public/privage key-pair
before you can encrypt/decrypt messages.
</P>
<FORM method="post">
<TABLE>
<TR>
<TD>Public Key</TD>
<TD><INPUT name="KeyE" value="<%=Server.HTMLEncode(LngKeyE)%>"></TD>
<TD rowspan="3">
<INPUT type="Submit" name="Action" value="Generate Keys">
</TD>
</TR>
<TR>
<TD>Private Key</TD>
<TD><INPUT name="KeyD" value="<%=Server.HTMLEncode(LngKeyD)%>"></TD>
</TR>
<TR>
<TD>Modulus</TD>
<TD><INPUT name="KeyN" value="<%=Server.HTMLEncode(LngKeyN)%>"></TD>
</TR>
<TR>
<TD colspan="3">
Test Message:<BR>
<TEXTAREA name="Message" cols="50" rows="7"><%=Server.HTMLEncode(StrMessage)%></TEXTAREA>
</TD>
</TR>
<TR>
<TD align="right" colspan="3">
<INPUT type="Submit" name="Action" value="Encrypt">
<INPUT type="Submit" name="Action" value="Decrypt">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

clsrsa.asp

<%
rem 實現rsa加密與解密的vbs類檔案
rem 文章標題:在asp中通過vbs類實現rsa加密與解密

' RSA Encryption Class
'
' .PrivateKey
' Your personal private key. Keep this hidden.
'
' .PublicKey
' Key for others to encrypt data with.
'
' .Modulus
' Used with both public and private keys when encrypting
' and decrypting data.
'
' .GenKey()
' Creates Public/Private key set and Modulus
'
' .Crypt(pLngMessage, pLngKey)
' Encrypts/Decrypts message and returns
' as a string.
'
' .Encode(pStrMessage)
' Encrypts message and returns in double-hex format
'
' .Decode(pStrMessage)
' Decrypts message from double-hex format and returns a string
'
Class clsRSA

Public PrivateKey
Public PublicKey
Public Modulus

Public Sub GenKey()
Dim lLngPhi
Dim q
Dim p

Randomize

Do
Do

' 2 random primary numbers (0 to 1000)
Do
p = Rnd * 1000 \ 1
Loop While Not IsPrime(p)

Do
q = Rnd * 1000 \ 1
Loop While Not IsPrime(q)


' n = product of 2 primes
Modulus = p * q \ 1

' random decryptor (2 to n)
PrivateKey = Rnd * (Modulus - 2) \ 1 + 2

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

Loop While PublicKey = 0 Or PublicKey = 1

' Loop if we can't crypt/decrypt a byte
Loop While Not TestCrypt(255)

End Sub

Private Function TestCrypt(ByRef pBytData)
Dim lStrCrypted
lStrCrypted = Crypt(pBytData, PublicKey)
TestCrypt = Crypt(lStrCrypted, PrivateKey) = pBytData
End Function

Private Function Euler(ByRef pLngPHI, ByRef pLngKey)

Dim lLngR(3)
Dim lLngP(3)
Dim lLngQ(3)

Dim lLngCounter
Dim lLngResult

Euler = 0

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

lLngCounter = -1

Do Until lLngR(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) Mod lLngR(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))

If lLngResult > 0 Then
Euler = lLngP(1)
Else
Eul



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.