Encryption | Decryption This article is composed of two documents
Test.asp Test Demo file
Clsrsa.asp to implement 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
REM Collection and collation: 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>
<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>
<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= "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 class file for RSA encryption and decryption
REM Article title: Implementing RSA Encryption and decryption via the VBS class in ASP
REM Collection and collation: Yanek
REM Contact: aspboy@263.net
' 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 Testcry