Skip to content

Instantly share code, notes, and snippets.

@srialdabaoth
Created March 13, 2018 13:43
Show Gist options
  • Select an option

  • Save srialdabaoth/1a636839a85610687633d29cfd5f6f78 to your computer and use it in GitHub Desktop.

Select an option

Save srialdabaoth/1a636839a85610687633d29cfd5f6f78 to your computer and use it in GitHub Desktop.
import json
from asn1crypto import pem, keys
with open('components/key.pub', 'rb') as f:
der_bytes = f.read()
if pem.detect(der_bytes):
type_name,headers,decoded_bytes = pem.unarmor(der_bytes)
key = keys.PublicKeyInfo.load(decoded_bytes)
n = key.native["public_key"]["modulus"]
e = key.native["public_key"]["public_exponent"]
print("{:#x}".format(n))
print("{:#x}".format(e))
#
# print(type_name)
# print(headers)
# print(decoded_bytes)
# der_hex = bytes(decoded_bytes).hex()
# print(der_hex)
# print(key)
headers_s = json.dumps(headers)
decoded_bytes_s = str(decoded_bytes)
with open('test_file.txt', 'w') as f:
# pem_bytes = pem.armor('CERTIFICATE', der_bytes)
f.write(type_name)
f.write(headers_s)
f.write(decoded_bytes_s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment