Skip to content

Instantly share code, notes, and snippets.

@pbakondy
Created September 17, 2014 14:57
Show Gist options
  • Save pbakondy/660191dde4be81088d34 to your computer and use it in GitHub Desktop.
Save pbakondy/660191dde4be81088d34 to your computer and use it in GitHub Desktop.
RSA Encrypt with forge
/**
* RSA Encrypt a String with Public Key
*
* @uses forge encryption library
* @param <String> text - string to encode
* @param <BigInteger> modulus - public key modulus
* @param <BigInteger> exponent - public key exponent
* @return <String> - encoded string in hex
*/
function RSAEncrypt(text, modulus, exponent) {
text = forge.util.encodeUtf8(text);
var publicKey = forge.pki.setRsaPublicKey(modulus, exponent);
var encrypted = publicKey.encrypt(text);
return forge.util.bytesToHex(encrypted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment