Skip to content

Instantly share code, notes, and snippets.

@ledeuns
Forked from jonlundy/conv.py
Last active May 28, 2022 16:52
Show Gist options
  • Save ledeuns/c8736a03320fd6dcf73eda9031969e76 to your computer and use it in GitHub Desktop.
Save ledeuns/c8736a03320fd6dcf73eda9031969e76 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# Converted from https://gist.github.com/JonLundy/f25c99ee0770e19dc595 to Python3
import sys,json,base64,binascii
with open(sys.argv[1]) as fp:
pkey=json.load(fp)
def enc(data):
missing_padding = 4 - len(data) % 4
if missing_padding:
data += b'='* missing_padding
return '0x'+binascii.hexlify(base64.b64decode(data,b'-_')).upper()
for k,v in list(pkey.items()):
if k == 'kty': continue
pkey[k] = enc(v.encode())
print("asn1=SEQUENCE:private_key\n[private_key]\nversion=INTEGER:0")
print("n=INTEGER:{}".format(pkey['n']))
print("e=INTEGER:{}".format(pkey['e']))
print("d=INTEGER:{}".format(pkey['d']))
print("p=INTEGER:{}".format(pkey['p']))
print("q=INTEGER:{}".format(pkey['q']))
print("dp=INTEGER:{}".format(pkey['dp']))
print("dq=INTEGER:{}".format(pkey['dq']))
print("qi=INTEGER:{}".format(pkey['qi']))
# Let's encrypt converter
# Create a DER encoded private key
openssl asn1parse -noout -out private_key.der -genconf <(python conv.py private_key.json)
# Convert to PEM
openssl rsa -in private_key.der -inform der
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment