Untitled
unknown
plain_text
3 years ago
1.4 kB
10
Indexable
from OpenSSL import crypto
def load_private_keys(file):
with open(file, 'r') as f:
keys = f.read().split('-----END RSA PRIVATE KEY-----')
keys = [k + '-----END RSA PRIVATE KEY-----' for k in keys[:-1]] # reattach the end marker
return [crypto.load_privatekey(crypto.FILETYPE_PEM, k) for k in keys]
def load_certificate(file):
with open(file, 'rb') as f:
cert = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
return cert
def find_matching_key(cert, private_keys):
cert_public_key = crypto.dump_publickey(crypto.FILETYPE_PEM, cert.get_pubkey())
for i, key in enumerate(private_keys):
key_public_key = crypto.dump_publickey(crypto.FILETYPE_PEM, key)
if key_public_key == cert_public_key:
return i, key
return None, None
# replace these with your actual file paths
certificate_file = '/path/to/certificate.pem'
private_keys_file = '/path/to/private_keys.pem'
certificate = load_certificate(certificate_file)
private_keys = load_private_keys(private_keys_file)
index, matching_key = find_matching_key(certificate, private_keys)
if matching_key is not None:
print(f'Matching key found at index {index}:')
print(crypto.dump_privatekey(crypto.FILETYPE_PEM, matching_key).decode())
else:
print('No matching key found.')Editor is loading...