簡體   English   中英

將.der文件中的私鑰導出為PKCS#1

[英]export private key in .der file as PKCS#1

我正在編寫一個Java代碼以生成密鑰並將其保存在文件中,我正在使用BouncyCastle庫使用pemwriter(如果在PKCS#1中)將私鑰寫入.pem文件中,並使用常規的FileOutputStream將其導出到PKCS中#8。

現在,當導出到DER時,嘗試在PKCS#1中導出它時就會出現問題。

我進行了大量搜索,但找不到合適的方法來在PKCS#1中對DSAPrivateKey進行RSAPrivateKey或將RSAPrivateKey DSAPrivateKey (PKCS#8)的常規編碼轉換為PKCS#1,或者是否可以指導我將PrivateKey轉換為RSAPrivateKeyDSAPrivateKeyECPrivateKey 這是我要導出的代碼的片段

        JcePEMEncryptorBuilder builder = new JcePEMEncryptorBuilder("DES-EDE3-CBC");
        PEMEncryptor enc = builder.build(password);

        FileOutputStream fis = new FileOutputStream(new File(privatekey.der));
        if (isPKCS8) {
            if (!encrypt) {
                fis.write(privateKeyBytes);
            } else {
                fis.write(enc.encrypt(privateKeyBytes));
            }
       fis.flush();
       fis.close(); 

其中privateKeyBytes是PrivateKey.getEncoded().的返回字節PrivateKey.getEncoded(). 它們在PKCS#8中,如果我可以將PrivateKey轉換為RSAPrivateKey或DSAPrivateKey,它們表示PKCS#1格式的私鑰

顯然,您可以使用類型信息並執行類轉換

   PrivateKey privKey = ...;
   if (privKey instance of RSAPrivateKey) {
       RSAPrivateKey rsaPrivKey = (RSAPrivateKey)privKey;
   if (privKey instance of DSAPrivateKey) {
       DSAPrivateKey dsaPrivKey = (DSAPrivateKey)privKey;
   if (privKey instance of ECPrivateKey) {
       ECPrivateKey ecPrivKey = (ECPrivateKey)privKey;
   }

暫無
暫無

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

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