It's unclear why you are doing this, but if all you need is a DSA private key that is compatible with openssl, you should simply run the openssl dsa (1) command in the manual page :
The DER option using the private key uses the ASN1 encoded DER form ASN.1 SEQUENCE, consisting of the values ββversion (currently zero), p, q, g, public and private key components respectively as ASN.1 INTEGER.
This is an example of how to export / import DSA private keys in openssl format:
from Crypto.PublicKey import DSA from Crypto.Util import asn1 key = DSA.generate(1024) # export seq = asn1.DerSequence() seq[:] = [ 0, key.p, key.q, key.g, key.y, key.x ] exported_key = "-----BEGIN DSA PRIVATE KEY-----\n%s-----END DSA PRIVATE KEY-----" % seq.encode().encode("base64") print exported_key # import seq2 = asn1.DerSequence() data = "\n".join(exported_key.strip().split("\n")[1:-1]).decode("base64") seq2.decode(data) p, q, g, y, x = seq2[1:] key2 = DSA.construct((y, g, p, q, x)) assert key == key2
source share