/IT-Sec$ python sign_key.py
gpg: key A0E9DEC8202C21F9: "kafbuy <[email protected]>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
Serial number of friend's public key not found.
import subprocess
def sign_public_key(friend_public_key, private_key, passphrase, output_file):
# Import the friend's public key
subprocess.run(['gpg', '--import', friend_public_key], check=True)
# Get the serial number of the friend's public key
result = subprocess.run(['gpg', '--list-keys', '--with-colons'], stdout=subprocess.PIPE, check=True)
lines = result.stdout.decode('utf-8').split('\n')
serial_number = None
for line in lines:
if line.startswith('pub:'):
parts = line.split(':')
if parts[9] == 'pgp-key-pub':
serial_number = parts[4]
break
if serial_number:
# Sign the friend's public key
subprocess.run(['gpg', '--sign-key', serial_number], check=True)
# Validate the signature
subprocess.run(['gpg', '--check-sigs', serial_number], check=True)
# Export the signed public key
subprocess.run(['gpg', '--armor', '--export', serial_number, '>', output_file], shell=True, check=True)
else:
print("Serial number of friend's public key not found.")
# Using the function
sign_public_key('pgp-key-pub.asc', 'my-private-key.asc', 'Jordan', 'signed_friend_public_key.asc')