I have an application that stores ssh keys. The user writes his private and public key into 2 text boxes and before storing them, my application is supposed to check if private key matches with the public key (using pycrypto). Validating a RSA pair was easy enough :
message = 'Encrypted message'
if 'ssh-rsa' in public_key:
public_key_container = RSA.importKey(public_key)
private_key_container = RSA.importKey(private_key)
encrypted_message = public_key_container.encrypt(message, 0)
decrypted_message = private_key_container.decrypt(encrypted_message)
if message == decrypted_message:
return True
I have found the code that seems to validate the DSA key pair, but I can't find how to extract the PQG values out of users public and private key :
elif 'ssh-dss' in public_key:
q = "?"
p = "?"
g = "?"
pub_k = ""
for b in bytearray(public_key, 'utf-8'):
pub_k += str(b)
priv_k = ""
for b in bytearray(private_key, 'utf-8'):
priv_k += str(b)
params = ( long(pub_k), long(g), long(p), long(q), long(priv_k))
key = DSA.construct(params)
if key.verify(message, key.sign(message,3)):
return True
Please, do not prompt me to generate a public key out of the private key using a function like ssh-keygen. I know this method, I want to do it using pycrypto.