在 java 中解析 Armored ECC public/private 密钥(从 gpg cli 生成)
Parse Armored ECC public/private keys (generated from gpg cli) in java
我正在尝试将装甲 ECC gpg 密钥转换为相应的 java class
ECPrivateKey/ECPublicKey.
要生成我正在使用的密钥对:gpg --expert --full-generate-key
然后选择 (9) ECC 和 ECC(或 (10) ECC(仅符号))
然后选择 (3) NIST P-256
导致:
-----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-----
如何从这种装甲文本格式转换为有效的 java.security.interfaces.ECPrivateKey 和 java.security.interfaces.ECPublicKey java classes?
我的最终目标是通过以下方式签到:
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
}
如果您只传入 'private key block',这将提取 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);
}
回答关于如何获得'privateKey'的评论问题,如果整个:
-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----
在一个文件中,那么就把整个文件读入一个byte[]:
InputStream fIn = new BufferedInputStream(new FileInputStream(...));
byte[] privateKey = org.bouncycastle.util.io.Streams.readAll(fIn);
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;
}
我正在尝试将装甲 ECC gpg 密钥转换为相应的 java class ECPrivateKey/ECPublicKey.
要生成我正在使用的密钥对:gpg --expert --full-generate-key
然后选择 (9) ECC 和 ECC(或 (10) ECC(仅符号))
然后选择 (3) NIST P-256
导致:
-----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-----
如何从这种装甲文本格式转换为有效的 java.security.interfaces.ECPrivateKey 和 java.security.interfaces.ECPublicKey java classes?
我的最终目标是通过以下方式签到:
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
}
如果您只传入 'private key block',这将提取 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);
}
回答关于如何获得'privateKey'的评论问题,如果整个:
-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----
在一个文件中,那么就把整个文件读入一个byte[]:
InputStream fIn = new BufferedInputStream(new FileInputStream(...));
byte[] privateKey = org.bouncycastle.util.io.Streams.readAll(fIn);
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;
}