5454import java .security .spec .X509EncodedKeySpec ;
5555import javax .crypto .spec .DHParameterSpec ;
5656
57+ import org .bouncycastle .asn1 .ASN1Encodable ;
5758import org .bouncycastle .asn1 .ASN1EncodableVector ;
5859import org .bouncycastle .asn1 .ASN1Encoding ;
5960import org .bouncycastle .asn1 .ASN1InputStream ;
@@ -311,12 +312,9 @@ public static KeyPair readECPrivateKey(final KeyFactory ecFactory, final byte[]
311312
312313 public static byte [] toDerRSAKey (RSAPublicKey pubKey , RSAPrivateCrtKey privKey ) throws IOException {
313314 if ( pubKey != null && privKey == null ) {
314- ASN1EncodableVector vec = new ASN1EncodableVector ();
315- vec .add (new ASN1Integer (pubKey .getModulus ()));
316- vec .add (new ASN1Integer (pubKey .getPublicExponent ()));
317315 // pubKey.getEncoded() :
318316 return KeyUtil .getEncodedSubjectPublicKeyInfo (
319- new AlgorithmIdentifier (PKCSObjectIdentifiers .rsaEncryption , DERNull .INSTANCE ), new DERSequence ( vec )
317+ new AlgorithmIdentifier (PKCSObjectIdentifiers .rsaEncryption , DERNull .INSTANCE ), toASN1Primitive ( pubKey )
320318 );
321319 }
322320 ASN1EncodableVector vec = new ASN1EncodableVector ();
@@ -332,16 +330,28 @@ public static byte[] toDerRSAKey(RSAPublicKey pubKey, RSAPrivateCrtKey privKey)
332330 return new DERSequence (vec ).toASN1Primitive ().getEncoded (ASN1Encoding .DER );
333331 }
334332
333+ public static ASN1Sequence toASN1Primitive (RSAPublicKey pubKey ) {
334+ ASN1EncodableVector vec = new ASN1EncodableVector ();
335+ vec .add (new ASN1Integer (pubKey .getModulus ()));
336+ vec .add (new ASN1Integer (pubKey .getPublicExponent ()));
337+ return new DERSequence (vec );
338+ }
339+
335340 public static byte [] toDerDSAKey (DSAPublicKey pubKey , DSAPrivateKey privKey ) throws IOException {
336341 if ( pubKey != null && privKey == null ) {
337342 // pubKey.getEncoded() :
338343 final DSAParams params = pubKey .getParams ();
339344 if (params == null ) {
340- return new SubjectPublicKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ),
341- new ASN1Integer (pubKey .getY ())).getEncoded (ASN1Encoding .DER );
345+ return new SubjectPublicKeyInfo (
346+ new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ),
347+ toASN1Primitive (pubKey )
348+ ).getEncoded (ASN1Encoding .DER );
342349 }
343- return new SubjectPublicKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
344- new DSAParameter (params .getP (), params .getQ (), params .getG ())), new ASN1Integer (pubKey .getY ())
350+ return new SubjectPublicKeyInfo (
351+ new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
352+ new DSAParameter (params .getP (), params .getQ (), params .getG ())
353+ ),
354+ toASN1Primitive (pubKey )
345355 ).getEncoded (ASN1Encoding .DER );
346356 }
347357 if ( privKey != null && pubKey != null ) {
@@ -359,11 +369,17 @@ public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) thr
359369 throw new IllegalArgumentException ("private key as well as public key are null" );
360370 }
361371 final DSAParams params = privKey .getParams ();
362- return new PrivateKeyInfo (new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
363- new DSAParameter (params .getP (), params .getQ (), params .getG ())), new ASN1Integer (privKey .getX ())
372+ return new PrivateKeyInfo (
373+ new AlgorithmIdentifier (X9ObjectIdentifiers .id_dsa ,
374+ new DSAParameter (params .getP (), params .getQ (), params .getG ())),
375+ new ASN1Integer (privKey .getX ())
364376 ).getEncoded (ASN1Encoding .DER );
365377 }
366378
379+ public static ASN1Primitive toASN1Primitive (DSAPublicKey pubKey ) {
380+ return new ASN1Integer (pubKey .getY ());
381+ }
382+
367383 public static byte [] toDerDHKey (BigInteger p , BigInteger g ) throws IOException {
368384 ASN1EncodableVector vec = new ASN1EncodableVector ();
369385 if ( p != null ) vec .add ( new ASN1Integer (p ) );
0 commit comments