Skip to content

Instantly share code, notes, and snippets.

@srialdabaoth
Created March 7, 2018 01:24
Show Gist options
  • Select an option

  • Save srialdabaoth/7464f8b65c638baa55c6e08398b4586c to your computer and use it in GitHub Desktop.

Select an option

Save srialdabaoth/7464f8b65c638baa55c6e08398b4586c to your computer and use it in GitHub Desktop.
PEM Parsing
import json
from asn1crypto import pem, x509
with open('key.pub', 'rb') as f:
der_bytes = f.read()
if pem.detect(der_bytes):
type_name,headers,decoded_bytes = pem.unarmor(der_bytes)
cert = x509.Certificate.load(decoded_bytes)
print(type_name)
print(headers)
print(decoded_bytes)
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