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