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