6161import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
6262import org .bouncycastle .asn1 .ASN1Primitive ;
6363import org .bouncycastle .asn1 .ASN1Sequence ;
64+ import org .bouncycastle .asn1 .DERBitString ;
65+ import org .bouncycastle .asn1 .DERInteger ;
6466import org .bouncycastle .asn1 .DERNull ;
6567import org .bouncycastle .asn1 .DERSequence ;
6668import org .bouncycastle .asn1 .DLSequence ;
6769import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
6870import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
6971import org .bouncycastle .asn1 .sec .ECPrivateKeyStructure ;
7072import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
73+ import org .bouncycastle .asn1 .x509 .DSAParameter ;
7174import org .bouncycastle .asn1 .x509 .SubjectPublicKeyInfo ;
7275import org .bouncycastle .asn1 .x9 .X9ObjectIdentifiers ;
7376import org .bouncycastle .jce .ECNamedCurveTable ;
@@ -328,7 +331,15 @@ public static byte[] toDerRSAKey(RSAPublicKey pubKey, RSAPrivateCrtKey privKey)
328331
329332 public static byte [] toDerDSAKey (DSAPublicKey pubKey , DSAPrivateKey privKey ) throws IOException {
330333 if ( pubKey != null && privKey == null ) {
331- return pubKey .getEncoded ();
334+ // pubKey.getEncoded() :
335+ final DSAParams params = pubKey .getParams ();
336+ if (params == null ) {
337+ return new SubjectPublicKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ),
338+ new ASN1Integer (pubKey .getY ())).getEncoded (ASN1Encoding .DER );
339+ }
340+ return new SubjectPublicKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
341+ new DSAParameter (params .getP (), params .getQ (), params .getG ())), new ASN1Integer (pubKey .getY ())
342+ ).getEncoded (ASN1Encoding .DER );
332343 }
333344 if ( privKey != null && pubKey != null ) {
334345 ASN1EncodableVector vec = new ASN1EncodableVector ();
@@ -339,12 +350,15 @@ public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) thr
339350 vec .add (new ASN1Integer (params .getG ()));
340351 vec .add (new ASN1Integer (pubKey .getY ()));
341352 vec .add (new ASN1Integer (privKey .getX ()));
342- return new DLSequence (vec ).getEncoded ();
353+ return new DERSequence (vec ).toASN1Primitive (). getEncoded (ASN1Encoding . DER );
343354 }
344355 if ( privKey == null ) {
345356 throw new IllegalArgumentException ("private key as well as public key are null" );
346357 }
347- return privKey .getEncoded ();
358+ final DSAParams params = privKey .getParams ();
359+ return new PrivateKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
360+ new DSAParameter (params .getP (), params .getQ (), params .getG ())), new ASN1Integer (privKey .getX ())
361+ ).getEncoded (ASN1Encoding .DER );
348362 }
349363
350364 public static byte [] toDerDHKey (BigInteger p , BigInteger g ) throws IOException {
0 commit comments