2017-06-19 1 views
7

Je suis en train de convertir une clé ECG gpg blindée à la classe java correspondante ECPrivateKey/ECPublicKey.Parse Armored ECC clés publiques/privées (générés à partir de gpg cli) en Java

Pour générer la paire de clés J'utilise: gpg --expert --full-generate-key

puis en sélectionnant (9) ECC et ECC (ou (10) ECC (signe seulement))

puis en sélectionnant (3) NIST P- 256

Entraînant:

-----BEGIN PGP PUBLIC KEY BLOCK----- 

mFIEWUdzwhMIKoZIzj0DAQcCAwQkAvZC1PIJ8ke1myyKhNny9vN78TIYo2MuAOY+ 
F38L9S3+Za9cKV/iIHOqfapbMoqdSmSnqDkevwQSr5MF2UOXtCJzaWduZWNjIChF 
Q0Mgc2lnbiBvbmx5KSA8c3NAc3MuY28+iJAEExMIADgWIQRiC+kefVkjnjKovKy5 
XANFl5+n1gUCWUdzwgIbAwULCQgHAgYVCAkKCwIEFgIDAQIeAQIXgAAKCRC5XANF 
l5+n1mzGAQDsgutymxDTTXPKFfpFFVp4fxacx1MSqxP71gNJYjguXwD8CEXD20Vm 
aU1WMi2jU7JC6oJn94Y4vWHwTLOU1zmQ19o= 
=swfS 
-----END PGP PUBLIC KEY BLOCK----- 

-----BEGIN PGP PRIVATE KEY BLOCK----- 

lHcEWUdzwhMIKoZIzj0DAQcCAwQkAvZC1PIJ8ke1myyKhNny9vN78TIYo2MuAOY+ 
F38L9S3+Za9cKV/iIHOqfapbMoqdSmSnqDkevwQSr5MF2UOXAAD9FhS2HZoWOyIi 
l9nj+WPa9S1o50jM5bNIRALzcyS8SgoP97Qic2lnbmVjYyAoRUNDIHNpZ24gb25s 
eSkgPHNzQHNzLmNvPoiQBBMTCAA4FiEEYgvpHn1ZI54yqLysuVwDRZefp9YFAllH 
c8ICGwMFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQuVwDRZefp9ZsxgEA7ILr 
cpsQ001zyhX6RRVaeH8WnMdTEqsT+9YDSWI4Ll8A/AhFw9tFZmlNVjIto1OyQuqC 
Z/eGOL1h8EyzlNc5kNfa 
=qHBB 
-----END PGP PRIVATE KEY BLOCK----- 

Comment puis-je obtenir de ce format de texte blindé à un java.security.interfaces.ECPrivateKey valide et java.security.interfaces.ECP ublicKey classes Java?

Mon objectif final est de signer de la manière suivante:

String createSignatureFromJson(String jsonPayload, byte[] privateKey) { 
     Payload payload = new Payload(jsonPayload) 
     def key = privateKeyParse(privateKey) 

     JWSSigner signer = new ECDSASigner((ECPrivateKey)key) 
     JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.ES256).build() 

     JWSObject jwsObject = new JWSObject(header, payload) 
     jwsObject.sign(signer) 
     jwsObject.signature 
    } 

Répondre

1
public static ECPrivateKey privateKeyParse(byte[] privateKey) throws Exception { 

     InputStream pgpIn = PGPUtil.getDecoderStream(new ByteArrayInputStream(privateKey)); 

     PGPObjectFactory pgpFact = new PGPObjectFactory(pgpIn, new JcaKeyFingerprintCalculator()); 
     PGPSecretKeyRing pgpSecRing = (PGPSecretKeyRing) pgpFact.nextObject(); 
     PGPSecretKey pgpSec = pgpSecRing.getSecretKey(); 

     PGPPrivateKey pgpPriv = pgpSec.extractPrivateKey(null); 

     JcaPGPKeyConverter converter = new JcaPGPKeyConverter(); 
     // this is the part i was missing from Peter Dettman's answer. pass BC provider to the converter 
     converter.setProvider(new BouncyCastleProvider()); 
     PrivateKey key = converter.getPrivateKey(pgpPriv); 
     return (ECPrivateKey) key; 
    } 
4

Si vous venez de passer dans le « bloc clé privée », ce extraira les ECPrivateKey:

private static ECPrivateKey privateKeyParse(byte[] privateKey) throws Exception 
{ 
    InputStream pgpIn = PGPUtil.getDecoderStream(new ByteArrayInputStream(privateKey)); 

    PGPObjectFactory pgpFact = new PGPObjectFactory(pgpIn, new JcaKeyFingerprintCalculator()); 
    PGPSecretKeyRing pgpSecRing = (PGPSecretKeyRing)pgpFact.nextObject(); 
    PGPSecretKey pgpSec = pgpSecRing.getSecretKey(); 
    PGPPrivateKey pgpPriv = pgpSec.extractPrivateKey(null); 

    return (ECPrivateKey)new JcaPGPKeyConverter().getPrivateKey(pgpPriv); 
} 

Pour répondre à un question de commentaires quant à la façon de la façon d'obtenir « privateKey », si l'ensemble:

-----BEGIN PGP PRIVATE KEY BLOCK----- 
... 
-----END PGP PRIVATE KEY BLOCK----- 

est dans un fichier, puis juste lire le fichier entier dans un octet []:

InputStream fIn = new BufferedInputStream(new FileInputStream(...)); 
byte[] privateKey = org.bouncycastle.util.io.Streams.readAll(fIn); 
+0

peut vous être plus précis sur la façon d'obtenir du bloc clé privée à l'octet [] paramètre privateKey? –

+0

Je reçois java.io.IOException: inconnu algorithme à clé publique PGP rencontré sur: PGPSecretKeyRing pgpSecRing = (PGPSecretKeyRing) pgpFact.nextObject() lors de la fourniture du bloc clé privée à votre méthode de la façon suivante: privateKeyBlock. split ('\ n'). join(). bytes –

+0

Quelle version de BC utilisez-vous? Je pense que les clés PGP EC n'étaient supportées qu'environ 1,50. –