Skip to content

Instantly share code, notes, and snippets.

@icetan
Created October 27, 2015 01:38
Show Gist options
  • Save icetan/5c3ee01f93587b34bf30 to your computer and use it in GitHub Desktop.
Save icetan/5c3ee01f93587b34bf30 to your computer and use it in GitHub Desktop.
Create a public RSA key from modulus and exponent on the command line
#!/bin/bash
set -e
b2hex() { echo -n $1|base64 --decode | xxd -p -u | tr -d \\n; }
modulus=$(b2hex u2/nlDLMbqLY+XBnWlqHv74a/wvmPoefKv+5NkTU0sbQAEMN7Gar9Hgp50uMUQhiOhwO6L4hezrY021etPyh2Q==)
exponent=$(b2hex AQAB)
asn1conf=$(echo -e "asn1=SEQUENCE:pubkeyinfo\n[pubkeyinfo]\nalgorithm=SEQUENCE:rsa_alg\npubkey=BITWRAP,SEQUENCE:rsapubkey\n[rsa_alg]\nalgorithm=OID:rsaEncryption\nparameter=NULL\n[rsapubkey]\nn=INTEGER:0x$modulus\ne=INTEGER:0x$exponent" | openssl asn1parse -genconf /dev/stdin -noout -out /dev/stdout | base64)
echo "Secret message wohoo!" | openssl rsautl -encrypt -keyform der -pubin -inkey <(base64 --decode <<<$asn1conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment