Code for rsa encryption and decryption through the vbs class in asp

Source: Internet
Author: User
Tags crypt modulus

Implement rsa encryption and decryption through vbs in asp.

This article consists of two files:
Test. asp test demo File
Vbs files encrypted and decrypted by rsa using clsrsa. asp
The following code is used:

1. test. asp
Copy codeThe Code is as follows: <%
Rem article title: implement rsa encryption and decryption through the vbs class in asp
Rem collection: yanek
Rem contact: aspboy@263.net

%>
<% 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

Copy codeThe Code is as follows: <%
Rem implements rsa encryption and decryption of vbs files
Rem article title: implement rsa encryption and decryption through the vbs class in asp
Rem collection: yanek
Rem contact: aspboy@263.net

'Rsa Encryption Class
'
'. PrivateKey
'Ur personal private key. Keep this hidden.
'
'. PublicKey
'Key for others to encrypt data.
'
'. 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)
'Crypts 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
Euler = Abs (lLngP (1) + pLngPHI
End If

End Function

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

Private Function IsPrime (ByRef pLngNumber)
Dim lLngSquare
Dim lLngIndex
IsPrime = False
If pLngNumber <2 Then Exit Function
If pLngNumber Mod 2 = 0 Then Exit Function
LLngSquare = Sqr (pLngNumber)
For lLngIndex = 3 To lLngSquare Step 2
If pLngNumber Mod lLngIndex = 0 Then Exit Function
Next
IsPrime = True
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
%>

Demo address: http://www.cnaspol.com/myrsa/test.asp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.