Skip to content

Instantly share code, notes, and snippets.

@luk0y
Forked from icetan/gen-rsa-pubkey.sh
Created November 10, 2020 06:23
Show Gist options
  • Save luk0y/843b0651525164dc5d58cf0abf88b719 to your computer and use it in GitHub Desktop.
Save luk0y/843b0651525164dc5d58cf0abf88b719 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