簡體   English   中英

將 PGP(公共)密鑰存儲在 java 密鑰庫 - Bouncycastle

[英]Store PGP (public) keys in java keystore - Bouncycastle

我在 SSO 的實現中使用 bouncycastle (JAVA) 進行簽名、加密、解密和簽名驗證。 我有原始 PGP 公鑰和私鑰,我需要將它們存儲在 Java 密鑰庫中。 這些 PGP 公鑰沒有證書。

我知道對於公鑰(根據 Keystore 的 javadoc: http://docs.oracle.com/javase/6/docs/api/java/security/KeyStore.html )我必須創建證書。 創建證書后,我可以將其作為 KeyStore.TrustedCertificateEntry 導入密鑰庫。 但是,我無法為類型 org.bouncycastle.openpgp.PGPPublicKey 創建證書條目。

我搜索了 web 但找不到任何有效的示例:

  1. Bouncycastle 文檔: http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation Generates certificate for X.509 keys -
  2. Bouncycastle 示例 - org.bouncycastle.openpgp.examples.DirectKeySignature:將證書(PGPSignature 類型的對象)直接添加到 PGPPublicKey。 總而言之——我已經簽署(認證)PGPPublicKey,但我無法將這種類型的密鑰存儲到 java 密鑰庫中。

     OutputStream out = new ByteArrayOutputStream(); if (armor) { out = new ArmoredOutputStream(out); } PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(secretKeyPass.toCharArray(), "BC"); PGPSignatureGenerator sGen = new PGPSignatureGenerator(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC"); sGen.initSign(PGPSignature.DIRECT_KEY, pgpPrivKey); BCPGOutputStream bOut = new BCPGOutputStream(out); sGen.generateOnePassVersion(false).encode(bOut); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); boolean isHumanReadable = true; spGen.setNotationData(true, isHumanReadable, notationName, notationValue); PGPSignatureSubpacketVector packetVector = spGen.generate(); sGen.setHashedSubpackets(packetVector); bOut.flush(); return PGPPublicKey.addCertification(keyToBeSigned, sGen.generate()).getEncoded();

我主要對編程解決方案(java 源代碼)感興趣,但使用某些工具的示例也會有所幫助。

謝謝!

我認為您應該從您的PGPPublicKey中提取一個java.security.PublicKey並使用它來構建一個可以存儲在密鑰庫中的X509Certificate

JcaPGPKeyConverter c = new JcaPGPKeyConverter();
PublicKey publicKey = c.getPublicKey(pgpPublicKey);
// ... Use Bouncy's X509V3CertificateGenerator or X509v3CertificateBuilder
// ... to construct a self-signed cert
X509Certificate x509Certificate = // ...
// ... add cert to KeyStore

要從PublicKey創建X509Certificate ,請參閱: 生成隨機證書

如果只想保存公鑰,為什么不能直接將密鑰內容保存到Java keystore中呢? 然后檢索內容並在需要時轉換為 PGPPublicKey object。

首先創建一個包裝器 class

public class PgpPublicKeyWrapper implements Key {
    private final String keyContent;
    public PgpPublicKeyWrapper(final String keyContent) {
        this.keyContent = keyContent;
    }
    @Override
    public String getAlgorithm() {
        return "PGP-PublicKey"; // you can call whatever you want
    }
    @Override
    public String getFormat() {
        return "RAW"; // has to be raw format
    }
    @Override
    public byte[] getEncoded() {
        return keyContent.getBytes();
    }
}

然后你可以這樣做來保存它

keyStore.setKeyEntry("think a name for alias", new PgpPublicKeyWrapper(key), PASSWORD, null);

當你想找回它

Key key = this.keyStore.getKey(alias, PASSWORD);
InputStream is = new ByteArrayInputStream(key.getEncoded());
PGPPublicKey publicKey = readPublicKey(is); 

對於readPublicKey(),你可以在網上找到很多關於如何讀取InputStream到一個PGPPublicKey object的例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM