2012-04-05 77 views
6

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

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

我已經通過網絡搜索,但沒有找到任何有效的例子:

  1. BouncyCastle的文檔:http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation 生成X.509證書密鑰 -
  2. BouncyCastle的例子 - org.bouncycastle.openpgp。 examples.DirectKeySignature: 將certificat(類型爲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源代碼),但使用一些工具將有助於太例子。

謝謝!

回答

0

我認爲你應該從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看到X509CertificateGenerate random certificates

0

如果您只想保存公鑰,爲什麼不能將關鍵內容保存到Java密鑰庫中?然後檢索內容並在需要時將其轉換爲PGPPublicKey對象。

創建一個包裝類第一

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對象的示例。

相關問題