Implementation of RSA encryption and decryption in ASP

Source: Internet
Author: User
Tags crypt decrypt modulus
Encryption | Decryption article has two file composition, test.asp test demo file; clsrsa.asp implementation of RSA encryption and decryption of the VBS class file
Here's the code:

1. test.asp

<%
REM Article title: Implementing RSA Encryption and decryption via the VBS class in ASP

%>
<%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>
<P>
You'll 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>
&LT;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>
&LT;TD colspan= "3" >
Test message:<br>
<textarea name= "message" cols= "rows=" 7 "><%=server.htmlencode (strmessage)%></textarea>
</TD>
</TR>
<TR>
&LT;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 class file for RSA encryption and decryption
REM Article title: Implementing RSA Encryption and decryption via the VBS class in ASP

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

Todo
Todo

' 2 random primary numbers (0 to 1000)
Todo
p = Rnd * 1000 \ 1
Loop while not IsPrime (p)

Todo
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



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.