Comment importer une key au format PEM (pas OpenPGP) dans GPG?

J'ai des données comme ceci:

-----BEGIN PRIVATE KEY----- MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDPzCORz9kUh4yt 73fiG1amQ16jwjXNzNO0d4xUWlrCP3dTfXnvtF35r2N/5Nefw9CcztBUAZACbwtn (... just sample data ...) jSYDRr88RZI4QYv9pW0+A8vWS2SJnIPW0fP9mcPOdZXxG/V2rL03YV5xcLCdbuBu 1tunEWZ5VcjfyEDfP7qZdWjGIYselOg= -----END PRIVATE KEY----- 

Si gpg --import il dit:

 gpg: no valid OpenPGP data found. gpg: Total number processed: 0 

Comment puis-je get cette key privée dans GPG?

Bien que les primitives de crypto sous-jacentes soient similaires, les formats de files PGP (packages), y compris les keys, sont très différents de ceux utilisés par OpenSSL (principalement ASN.1 et PEM). Vous ne le dites pas, mais cela semble être une key RSA. Si Java vous convient, il peut le faire en utilisant BCPROV plus BCPKIX (pour PEM) et BCPG (pour PGP) à partir de http://www.bouncycastle.org . Ajuster le nom, etc pour goûter.

 // nopackage
 importez java.io.FileOutputStream;
 importez java.io.FileReader;
 importez java.io.IOException;
 importez java.io.OutputStream;
 importez java.security.InvalidKeyException;
 importez java.security.KeyFactory;
 importez java.security.KeyPair;
 importez java.security.NoSuchProviderException;
 importer java.security.Security;
 importez java.security.SignatureException;
 importez java.security.spec.KeySpec;
 importez java.security.spec.PKCS8EncodedKeySpec;
 importez java.security.spec.RSAPublicKeySpec;
 importez java.util.Date;
 import org.bouncycastle.asn1.ASN1Sequence;
 import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
 import org.bouncycastle.bcpg.ArmoredOutputStream;
 import org.bouncycastle.bcpg.HashAlgorithmTags;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 importer org.bouncycastle.openpgp.PGPEncryptedData;
 import org.bouncycastle.openpgp.PGPException;
 import org.bouncycastle.openpgp.PGPKeyPair;
 import org.bouncycastle.openpgp.PGPPublicKey;
 import org.bouncycastle.openpgp.PGPSecretKey;
 import org.bouncycastle.openpgp.PGPSignature;
 import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
 importer org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
 import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
 import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
 importer org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
 import org.bouncycastle.openssl.PEMParser;

 / **
  * Une class d'utilité simple qui convertit PEM PKCS8 (OpenSSL) en une paire RSA PGPPublicKey / PGPSecretKey.
  * <p>
  * utilisation: UnixSE276317 [-a] identité passPhrase inputPEM
  * <p>
  * Où identité est le nom à associer à la key publique.  Les keys sont placées 
  * dans les files {pub, secret} .asc si -a (armure) est spécifié et .bpg sinon.
  * /
 // modifié à partir du package org.bouncycastle.openpgp.examples class RSAPrivateKeyGenerator 
 class publique UnixSE276317
 {
     private static void exportKeyPair (
         OutputStream secretOut,
         OutputStream publicOut,
         Paire KeyPair,
         Identité de string,
         char [] passPhrase,
         armure booleanne)
         throws IOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException
     {    
         si (armure)
         {
             secretOut = nouveau ArmoredOutputStream (secretOut);
         }

         PGPDigestCalculator sha1Calc = nouveau JcaPGPDigestCalculatorProviderBuilder (). Build (). Get (HashAlgorithmTags.SHA1);
         PGPKeyPair keyPair = new JcaPGPKeyPair (PGPPublicKey.RSA_GENERAL, paire, nouvelle date ());
         PGPSecretKey secretKey = nouvelle key PGPSecret (PGPSignature.DEFAULT_CERTIFICATION, keyPair, identité, sha1Calc, null, null, 
                 nouveau JcaPGPContentSignerBuilder (keyPair.getPublicKey (). getAlgorithm (), HashAlgorithmTags.SHA1), 
                 nouveau JcePBESecretKeyEncryptorBuilder (PGPEncryptedData.CAST5, sha1Calc) .setProvider ("BC") .construction (passPhrase));

         secretKey.encode (secretOut);

         secretOut.close ();

         si (armure)
         {
             publicOut = new ArmoredOutputStream (publicOut);
         }

         PGPPublicKey key = secretKey.getPublicKey ();

         key.encode (publicOut);

         publicOut.close ();
     }

     public static void main (
         Ssortingng [] args)
         throws Exception
     {
         Security.addProvider (nouveau BouncyCastleProvider ());

         // KeyPairGenerator kpg = KeyPairGenerator.getInstance ("RSA", "BC");

         //kpg.initialize(1024);

         // KeyPair kp = kpg.generateKeyPair ();

         entier flag = args.length> 0 && args [0] .equals ("- a")?  dix;

         if (args.length! = drapeau + 3)
         {
             System.out.println ("UnixSE276317 [-a] identité passPhrase inputPEM");
             System.exit (0);
         }

         // adapté de org.bouncycastle.openssl.PEMParser $ PrivateKeyParser + RSAKeyPairParser
         FileReader rdr = new FileReader (args [indicateur + 2]);
         PrivateKeyInfo pk8 = (PrivateKeyInfo) nouveau PEMParser (rdr) .readObject ();
         rdr.close (); 
         ASN1Sequence seq = (ASN1Sequence) pk8.parsePrivateKey ();
         org.bouncycastle.asn1.pkcs.RSAPrivateKey keyStruct = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance (seq);
         KeyFactory fait = KeyFactory.getInstance ("RSA");
         KeySpec privSpec = nouveau PKCS8EncodedKeySpec (pk8.getEncoded ());
         KeySpec pubSpec = nouveau RSAPublicKeySpec (keyStruct.getModulus (), keyStruct.getPublicExponent ());
         KeyPair kp = nouveau KeyPair (fact.generatePublic (pubSpec), fact.generatePrivate (privSpec));

         Ssortingng [] suffixe = {"bpg", "asc"};
         FileOutputStream out1 = new FileOutputStream ("secret." + Suffixe [indicateur]);
         FileOutputStream out2 = new FileOutputStream ("pub." + Suffixe [drapeau]);
         exportKeyPair (out1, out2, kp, args [indicateur + 0], args [indicateur + 1] .toCharArray (), indicateur> 0);
     }
 }