Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
import subprocess
import tempfile

def get_modulus_md5(file, is_cert):
    if is_cert:
        cmd = ['openssl', 'x509', '-noout', '-modulus', '-in', file]
    else:
        cmd = ['openssl', 'rsa', '-noout', '-modulus', '-in', file]
    result = subprocess.run(cmd, capture_output=True, text=True)
    modulus = result.stdout

    cmd = ['openssl', 'md5']
    result = subprocess.run(cmd, input=modulus, capture_output=True, text=True)
    md5 = result.stdout
    return md5

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 keys

# replace these with your actual file paths
certificate_file = '/path/to/certificate.pem'
private_keys_file = '/path/to/private_keys.pem'

cert_md5 = get_modulus_md5(certificate_file, True)

private_keys = load_private_keys(private_keys_file)

for i, key in enumerate(private_keys):
    with tempfile.NamedTemporaryFile(delete=True) as temp:
        temp.write(key.encode())
        temp.flush()
        key_md5 = get_modulus_md5(temp.name, False)
    if cert_md5 == key_md5:
        print(f'Matching key found at index {i}:')
        print(key)
        break
else:
    print('No matching key found.')