Skip to content

Instantly share code, notes, and snippets.

@chicagoworks
Created December 28, 2010 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chicagoworks/757210 to your computer and use it in GitHub Desktop.
Save chicagoworks/757210 to your computer and use it in GitHub Desktop.
Port of the RSA algo to javascript and vb-script. Used to for low-level encryption on non-https site.
<%
REM: An excerpt from RSA Security site FAW "Is RSA patented?"
REM: http://www.rsasecurity.com/rsalabs/faq/6-3-1.html
REM: The patent for the RSA algorithm (U.S. Patent 4,405,829) was issued on September 20, 1983,
REM: exclusively licensed to RSA Security Inc. by the Massachusetts Institute of Technology,
REM: with an expiration date of September 20, 2000. RSA Security maintained a standard,
REM: royalty-based licensing policy that could be modified for special circumstances.
REM: In the U.S., a license has been needed to ëëmake, use or sellíí products that included
REM: the RSA algorithm. However, RSA Security has long allowed free non-commercial use of
REM: the RSA algorithm, with written permission, for academic or university research purposes.
REM:
REM: On September 6, 2000, RSA Security made the RSA algorithm publicly available and waived
REM: its rights to enforce the RSA patent for any development activities that include the
REM: algorithm occurring after September 6, 2000. From this date forward, companies are able
REM: to develop products that incorporate their own implementation of the RSA algorithm and
REM: sell these products in the U.S.
%>
<script>
function dostuff() {
form.jsencrypted.value = encrypt(form.message.value,form.public.value,form.modulus.value);
form.jsdecrypted.value = decrypt(form.jsencrypted.value,form.private.value,form.modulus.value);
return
}
function encrypt(txt, key, mkey) {
//return Math.pow(num,key) % mkey
if (txt.length == 0) return
encrypt = ""
for (i = 0; i < txt.length; i++) {
char = txt.substr(i,1); //get single characters
dec = char.toString().charCodeAt(0); //convert each character to ASCII decimal
cipher = crypt(dec, key, mkey) //encrypt ASCII decimals
encrypt = encrypt.concat(padNum(jsToHex(cipher, 4))) //convert decimals to hex (4 character blocks)
}
return encrypt;
}
function decrypt(hex, key, mkey) {
decrypt = ""
for (i = 0; i < hex.length; i=i+4) {
block = hex.substr(i,4); //get hex block
cipher = jsToDec(block); //convert each hex block to cipher
dec = crypt(cipher, key, mkey) //decrypt the cipher back to ASCII decimal
decrypt = decrypt.concat(String.fromCharCode(dec)) //convert decimals to hex (4 character blocks)
}
return decrypt;
}
function crypt (num, key, mkey) {
y = (num*num) % mkey
if ( key % 2 == 0) {
x = 1
} else {
x = num
}
for ( var i = 1; i <= key/2; i++) {
x = (x * y) % mkey
}
return x
}
function jsToHex(dec) {
dec=dec*1;
return padNum(dec.toString(16),4);
}
function jsToDec(hex) {
preHex = "0x"
return Math.floor(preHex.concat(hex));
}
function padNum(str,len) {
pad = "0"
while(str.length < 4) {
str = pad.concat(str);
}
return str.toUpperCase()
}
</script>
<%
Function NumberToHex(ByRef pLngNumber, ByRef pLngLength)
NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength)
End Function
Function HexToNumber(ByRef pStrHex)
HexToNumber = CLng("&h" & pStrHex)
End Function
p1 = request.form("prime1")
p2 = request.form("prime2")
pubKey = request.form("public")
priKey = request.form("private")
modKey = request.form("modulus")
origText = request.form("message")
coded = encrypt(origText,pubKey,modKey)
convText = decrypt(coded,priKey,modKey)
Function Encrypt(txt, key, mkey)
REM: solve for RSA encryption formula
REM: Cipher = Txt^key % modulus
If Len(txt) = 0 Then Exit Function
For i = 1 To Len(txt)
dec = Asc(Mid(txt, i, 1)) REM: convert each character to ASCII decimal
cipher = Crypt(dec, key, mkey) REM: encrypt ASCII decimals
Encrypt = Encrypt & NumberToHex(cipher, 4) REM: convert decimals to hex (4 character blocks)
Next
End Function
Function Decrypt(hex, key, mkey)
REM: Solve for RSA decryption formula
REM: Txt = Cipher^key % modulus
Decode = ""
For i = 1 To Len(hex) Step 4
cipher = HexToNumber(Mid(hex, i, 4)) REM: convert the hex back to cipher
dec = Crypt(cipher, key, mkey) REM: decrypt cipher
Decrypt = Decrypt & Chr(dec) REM: convert ASCII decimal back to character, build result
Next
End Function
Function Crypt(num, key, mkey)
REM: Modular exponentiation and Fermat/Eular theorem x^p-1 = 1%P
REM: If I really knew what that meant I'd be making more money :-)
y = (num * num) Mod mkey
If key Mod 2 = 0 Then
x = 1
Else
x = num
End If
For i = 1 To key / 2
x = (x * y) Mod mkey
Next
Crypt = x
End Function
%>
<form method="post" name=form>
<table>
<tr><td>prime 1: </td><td>p1: </td><td><input type=text name=prime1 value=11> </td></tr>
<tr><td>prime 2: </td><td>p2: </td><td><input type=text name=prime1 value=211> </td></tr>
<tr><td>modulus: </td><td> N: </td><td><input type=text name=modulus value=2321> </td></tr>
<tr><td>public: </td><td> E: </td><td><input type=text name=public value=11> </td></tr>
<tr><td>private: </td><td> D: </td><td><input type=text name=private value=191> </td></tr>
<tr><td>message: </td><td> T: </td><td><input type=text name=message value="<%=origText%>"> </td></tr>
<tr><td>encrypted: </td><td> C: </td><td><input type=text name=encrypted value="<%=coded%>"> </td><td><input type=text name=jsencrypted value=""> </td></tr>
<tr><td>decrypted: </td><td> T: </td><td><input type=text name=decrypted value="<%=convText%>"> </td><td><input type=text name=jsdecrypted value=""> </td></tr>
<tr><td colspan=2><input type=submit></td><td><button onclick="dostuff();">dostuff</button></td></tr>
</table>
</form>
// Better alternatives
// http://www.ohdave.com/rsa/
// http://www.hanewin.net/encrypt/rsa/rsa.htm
// http://www.cs.pitt.edu/~kirk/cs1501/notes/rsademo/
// https://ziyan.info/2008/10/javascript-rsa/
//====================================================
// An excerpt from RSA Security site FAW "Is RSA patented?"
// http://www.rsasecurity.com/rsalabs/faq/6-3-1.html
// The patent for the RSA algorithm (U.S. Patent 4,405,829) was issued on September 20, 1983,
// exclusively licensed to RSA Security Inc. by the Massachusetts Institute of Technology,
// with an expiration date of September 20, 2000. RSA Security maintained a standard,
// royalty-based licensing policy that could be modified for special circumstances.
// In the U.S., a license has been needed to ‘‘make, use or sell’’ products that included
// the RSA algorithm. However, RSA Security has long allowed free non-commercial use of
// the RSA algorithm, with written permission, for academic or university research purposes.
//
// On September 6, 2000, RSA Security made the RSA algorithm publicly available and waived
// its rights to enforce the RSA patent for any development activities that include the
// algorithm occurring after September 6, 2000. From this date forward, companies are able
// to develop products that incorporate their own implementation of the RSA algorithm and
// sell these products in the U.S.
//
// ModulusKey = 2321
// PublicKey = 11
// Private Key = 191
//The following is an example of how to call the encryption function:
// <script for="form" event="onsubmit">
// form.js_asp.value = encrypt(form.password.value,11,2321);
// form.submit();
// </script>
//
// <script for="window" event="onload">
// form.asp_js.value = decrypt(form.asp_js.value,11,2321);
// </script>
function encrypt(txt, key, mkey) {
//return Math.pow(num,key) % mkey
if (txt.length == 0) return
strEncrypt = ""
for (i = 0; i < txt.length; i++) {
char = txt.substr(i,1); //get single characters
dec = char.toString().charCodeAt(0); //convert each character to ASCII decimal
cipher = crypt(dec, key, mkey) //encrypt ASCII decimals
strEncrypt = strEncrypt.concat(padNum(jsToHex(cipher, 4))) //convert decimals to hex (4 character blocks)
}
return strEncrypt;
}
function decrypt(hex, key, mkey) {
decrypt = ""
for (i = 0; i < hex.length; i=i+4) {
block = hex.substr(i,4); //get hex block
cipher = jsToDec(block); //convert each hex block to cipher
dec = crypt(cipher, key, mkey) //decrypt the cipher back to ASCII decimal
decrypt = decrypt.concat(String.fromCharCode(dec)) //convert decimals to hex (4 character blocks)
}
return decrypt;
}
function crypt (num, key, mkey) {
y = (num*num) % mkey
if ( key % 2 == 0) {
x = 1
} else {
x = num
}
for ( var i = 1; i <= key/2; i++) {
x = (x * y) % mkey
}
return x
}
function jsToHex(dec) {
dec=dec*1;
return padNum(dec.toString(16),4);
}
function jsToDec(hex) {
preHex = "0x"
return Math.floor(preHex.concat(hex));
}
function padNum(str,len) {
pad = "0"
while(str.length < 4) {
str = pad.concat(str);
}
return str.toUpperCase()
}
REM: An excerpt from RSA Security site FAW "Is RSA patented?"
REM: http://www.rsasecurity.com/rsalabs/faq/6-3-1.html
REM: The patent for the RSA algorithm (U.S. Patent 4,405,829) was issued on September 20, 1983,
REM: exclusively licensed to RSA Security Inc. by the Massachusetts Institute of Technology,
REM: with an expiration date of September 20, 2000. RSA Security maintained a standard,
REM: royalty-based licensing policy that could be modified for special circumstances.
REM: In the U.S., a license has been needed to ëëmake, use or sellíí products that included
REM: the RSA algorithm. However, RSA Security has long allowed free non-commercial use of
REM: the RSA algorithm, with written permission, for academic or university research purposes.
REM:
REM: On September 6, 2000, RSA Security made the RSA algorithm publicly available and waived
REM: its rights to enforce the RSA patent for any development activities that include the
REM: algorithm occurring after September 6, 2000. From this date forward, companies are able
REM: to develop products that incorporate their own implementation of the RSA algorithm and
REM: sell these products in the U.S.
REM: The following is based on:
REM: Prime 1 = 11
REM: Prime 2 = 211
REM: ModulusKey = 2321
REM: PublicKey = 11
REM: PrivateKey = 191 This should not be used on the external javascript
Function Encrypt(txt, key, mkey)
REM: solve for RSA encryption formula
REM: Cipher = Txt^key % modulus
If Len(txt) = 0 Then Exit Function
On Error Resume Next
For i = 1 To Len(txt)
dec = Asc(Mid(txt, i, 1)) REM: convert each character to ASCII decimal
cipher = Crypt(dec, key, mkey) REM: encrypt ASCII decimals
Encrypt = Encrypt & NumberToHex(cipher, 4) REM: convert decimals to hex (4 character blocks)
Next
If Err Then
Session.Abandon()
End If
On Error GoTo 0
End Function
Function Decrypt(hex, key, mkey)
REM: Solve for RSA decryption formula
REM: Txt = Cipher^key % modulus
REM: If hex is NULL then "For i = 1 To Len(hex)" throws an error
If Len(hex) = 0 OR isNull(hex) Then Exit Function
Decode = ""
For i = 1 To Len(hex) Step 4
cipher = HexToNumber(Mid(hex, i, 4)) REM: convert the hex back to cipher
dec = Crypt(cipher, key, mkey) REM: decrypt cipher
On Error Resume Next
Decrypt = Decrypt & Chr(dec) REM: convert ASCII decimal back to character, build result
If Err Then
Session.Abandon()
End If
On Error GoTo 0
Next
End Function
Function Crypt(num, key, mkey)
REM: Modular exponentiation and Fermat/Eular theorem x^p-1 = 1%P
REM: If I really knew what that meant I'd be making more money :-)
On Error Resume Next
y = (num * num) Mod mkey
If key Mod 2 = 0 Then
x = 1
Else
x = num
End If
For i = 1 To key / 2
x = (x * y) Mod mkey
Next
If (Err) Then
Session.Abandon()
End If
On Error GoTo 0
Crypt = x
End Function
Function NumberToHex(ByRef pLngNumber, ByRef pLngLength)
'NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength)
On Error Resume Next
NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength)
If Err Then
Session.Abandon()
End If
On Error GoTo 0
End Function
Function HexToNumber(ByRef pStrHex)
'HexToNumber = CLng("&h" & pStrHex)
On Error Resume Next
HexToNumber = CLng("&h" & pStrHex)
If Err Then
Session.Abandon()
End If
On Error GoTo 0
End Function
Function jsEscapeQuote(string)
Return Replace(string,"'","\'")
End Function
@chicagoworks
Copy link
Author

Wrote this a loooong time ago (circa 2001?). Thought people might find it useful. There are several better alternatives, t least, I assume they know more about encryption than I do.

http://www.ohdave.com/rsa/

http://www.hanewin.net/encrypt/rsa/rsa.htm

http://www.cs.pitt.edu/~kirk/cs1501/notes/rsademo/

https://ziyan.info/2008/10/javascript-rsa/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment