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
<% rem 实现rsa加密与解密的vbs类文件 rem 文章标题:在asp中通过vbs类实现rsa加密与解密 rem 收集整理:yanek rem 联系:[email protected]
' 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 message 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
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
' loop if we can't crypt/decrypt a byte loop while not testcrypt(255)
end sub
private function testcry |||pt(byref pbytdata) dim lstrcrypted lstrcrypted = crypt(pbytdata, publickey) testcrypt = crypt(lstrcrypted, privatekey) = pbytdata end function
private function euler(byref plngphi, byref plngkey)
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