def export_private_key(private_key, filename):
with open(filename, "wb") as file:
file.write(private_key.exportKey('PEM', True, 'MyPassphrase'))
file.close()
Please note that we are writing binary data to a file. So, we need to open the file in “wb” mode. We have passed three arguments to the exportKey() function. The ‘PEM’ argument specifies that the encoding for the output is ‘PEM’. The second argument is pkcs8 and it specifies whether the key is encoded with PKCS#8 (Public-Key Cryptography Standards #8). If pkcs8 is False, then an obsolete PEM encryption scheme is used. The third argument of the exportKey() function is the passphrase. While importing the private key from the file, we would need to use the same passphrase.
The DSA public key can be exported using the following function:
def export_public_key(public_key1, filename):
with open(filename, "wb") as file:
file.write(public_key1.exportKey('PEM'))
file.close()
Here also we are using the PEM format to export the public key.
Next, we would import the private key and the public key from the two files. The private key can be imported from the file private_key_dsa.pem using the following function:
def import_private_key(filename):
with open(filename, "rb") as file:
private_key = DSA.importKey(file.read(), 'MyPassphrase')
return private_key
Please note that we need to import the private key from the file using the same passphrase. Here also we are reading binary data from a file. So, the file needs to be opened in “rb” mode.
The DSA public key can be imported from the file public-key_dsa.pem using the following function:
def import_public_key(filename):
with open(filename, "rb") as file:
public_key1 = DSA.importKey(file.read())
return public_key1
The complete code for exporting and importing DSA keys is given below:
from Crypto.PublicKey import DSA
def export_private_key(private_key, filename):
with open(filename, "wb") as file:
file.write(private_key.exportKey('PEM', True, 'MyPassphrase'))
file.close()
def export_public_key(public_key1, filename):
with open(filename, "wb") as file:
file.write(public_key1.exportKey('PEM'))
file.close()
def import_private_key(filename):
with open(filename, "rb") as file:
private_key = DSA.importKey(file.read(), 'MyPassphrase')
return private_key
def import_public_key(filename):
with open(filename, "rb") as file:
public_key1 = DSA.importKey(file.read())
return public_key1
keypair = DSA.generate(2048)
public_key = keypair.publickey()
export_private_key(keypair, 'private_key_dsa.pem')
export_public_key(public_key, 'public_key_dsa.pem')
keypair = import_private_key('private_key_dsa.pem')
public_key = import_public_key('public_key_dsa.pem')
Interested readers, who want to know more on how to create and verify DSA signatures using the PyCryptodome module in Python, please refer to the following article: How to create and verify DSA signatures using the PyCryptodome module in Python? And, readers who want to know more on how to implement the DSA algorithm in Python, please refer to the following articles:
How to implement the DSA key generation algorithm in Python?
How to implement the DSA signature creation and verification algorithm in Python?
I hope this helps. However, readers who want to know more about how different cryptographic algorithms work and how they are used in various secure network protocols can refer to the book “Cryptography And Public Key Infrastructure.”










































0 Comments