Skip to content

Instantly share code, notes, and snippets.

@venik
Created September 12, 2017 23:02
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 venik/a9c02e065adaf13be4dba4b305869e93 to your computer and use it in GitHub Desktop.
Save venik/a9c02e065adaf13be4dba4b305869e93 to your computer and use it in GitHub Desktop.
jsrsasign doesnt support creating RSA object from public in the ASN.1 form, one needs to extract modulus and exponent manually (typescript)
function getRsaFromPubKey(pubKeyB64: string): RSAKey {
const pubKeyDecoded = b64tohex(pubKeyB64);
// jsrsasign cannot build key out of PEM or ASN.1 string, so we have to extract modulus and exponent
// you can get some idea what happens from the link below (keep in mind that in JS every char is 2 bytes)
// https://crypto.stackexchange.com/questions/18031/how-to-find-modulus-from-a-rsa-public-key/18034#18034
const modulus = pubKeyDecoded.substr(50, 128);
const exp = pubKeyDecoded.substr(182, pubKeyDecoded.length);
return KEYUTIL.getKey({n: modulus, e: exp});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment