2016-10-04 15 views
0

ファイルをpgp形式に暗号化する必要があります。私の公開鍵は.asc形式のです。Java:.ascファイルからjava.security.Keyを作成する方法は?

暗号のinit()メソッドには、渡される公開鍵が必要です。ファイルを使用してその鍵を作成する方法は何ですか。私の場合、それは.ascファイルです。

Cipher cipher; 
    Key publicKey = null; 

    try 
    { 
     cipher = Cipher.getInstance("RSA", "BC"); 




    } catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) { 
     String msg = "failed to create output stream"; 
     LOGGER.error(msg, e); 
     throw new RuntimeException(msg, e); 
    } 

    try { 
     publicKey = getPublicKey(publicKeyPath); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return (new CipherOutputStream(outputStream, cipher)); 

私はエラーを取得しています: java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:無効なキーフォーマット

マイgetPublicKeyメソッドは次のようになります(しかし、私が思うに、私は持っていません。

:ファイルは、公開鍵自体)

public static PublicKey getPublicKey(String filename) 
     throws Exception { 

     byte[] keyBytes = Files.readAllBytes(new File(filename).toPath()); 

     X509EncodedKeySpec spec = 
      new X509EncodedKeySpec(keyBytes); 
     KeyFactory kf = KeyFactory.getInstance("RSA"); 
     return kf.generatePublic(spec); 
     } 

Public_key.ascのように見えるがあるとして、このようにそれを行うにはここでは

  -----BEGIN PGP PUBLIC KEY BLOCK----- 
      Version: Encryption Desktop 10.3.2 (Build 16127) 

      mQENBFYhXNoBCACgX/u03wm8cLqmTZiKGx6H/1ZUoBsfaDB2rdG2D8jYQzvaq4MA 
      hZWBEVhA2BGKrNI+a2SDhKGAY4OK7aUZVAVG1bfQNVdNe80TbEF8g/wO2FreYPkb 
      ojPtkwgyzsvb1BKwgRM1UMjkM5OWnhAPDhFDc39SFbmHLsXrURqFqJd9T3xzF6ty 

      ................................................................ 


      D4WXvHpPXCJcwCBe+/81ZpjxlrLkUu8bO79jxZdKcI5ZRpmIe/VPJoDUVKLvl9n3 
      ANvDJGdGcW3x6RyL9QOnoRDf6njimqcTm8UqImdLCz4TFdv94dvM4K0NOWuFdYal 
      E9Q+U0Q7aiaWn+Kt+OYpd6++m7wnJRH/q0H69LIR9v3Td3udzOaxv/gzXF1BFuAS 
      DQs6iA== 
      =ckOV 
      -----END PGP PUBLIC KEY BLOCK----- 

は、このキーのプロパティです:ロバートとマールテンBodewesから

enter image description here

+1

あなたが使用することはできませんAESの公開鍵。 AESは、128,192、または256ビットのランダムキーを持つ対称暗号です。公開鍵は、RSA、EC、または他の非対称アルゴリズムです。あなたのガスタンクに電池を落とすように。 – erickson

+1

X.509はキー形式に関してPGPと互換性がないため、特別なPGPキーローダーが必要です。 – Robert

+1

Bouncy Castle内のPGP機能を使用しないのはなぜですか? –

答えて

1

返信は行くための方法の一つです。

私の公開鍵が.skrで.ASCファイルと秘密鍵(秘密鍵リング)

にいた私はそれをこのように実装され、それが私の仕事:

   public static PublicKey getPublicKey(
        String filePath) 
        throws PGPException, NoSuchProviderException, FileNotFoundException, IOException 
       { 
        PGPPublicKey encKey = readPublicKey(new FileInputStream(filePath)); 
        return new JcaPGPKeyConverter().setProvider("BC").getPublicKey(encKey); 
       } 




       public static PrivateKey getPrivateKey( 
         InputStream in, char[]  passwd) 
         throws IOException, PGPException, NoSuchProviderException 
        { 
         in = PGPUtil.getDecoderStream(in); 

         PGPSecretKeyRingCollection  pgpSec = new PGPSecretKeyRingCollection(in); 

         // 
         // we just loop through the collection till we find a key suitable for encryption, in the real 
         // world you would probably want to be a bit smarter about this. 
         // 

         // 
         // iterate through the key rings. 
         // 
         Iterator<?> rIt = pgpSec.getKeyRings(); 

         while (rIt.hasNext()) 
         { 
          PGPSecretKeyRing kRing = (PGPSecretKeyRing)rIt.next();  
          Iterator<?>      kIt = kRing.getSecretKeys(); 

          while (kIt.hasNext()) 
          { 
           PGPSecretKey k = (PGPSecretKey)kIt.next(); 

           if (k != null) 
           { 
            PGPPrivateKey pk = k.extractPrivateKey(passwd, "BC"); 
            return new JcaPGPKeyConverter().setProvider("BC").getPrivateKey(pk); 
           } 
          } 
         } 

         throw new IllegalArgumentException("Can't find secured key in key ring."); 
        } 

       public static PGPPublicKey readPublicKey( 
         InputStream in) 
         throws IOException, PGPException 
        { 
         in = PGPUtil.getDecoderStream(in); 

         PGPPublicKeyRingCollection  pgpPub = new PGPPublicKeyRingCollection(in); 

         // 
         // we just loop through the collection till we find a key suitable for encryption, in the real 
         // world you would probably want to be a bit smarter about this. 
         // 

         // 
         // iterate through the key rings. 
         // 
         Iterator<?> rIt = pgpPub.getKeyRings(); 

         while (rIt.hasNext()) 
         { 
          PGPPublicKeyRing kRing = (PGPPublicKeyRing)rIt.next();  
          Iterator<?>      kIt = kRing.getPublicKeys(); 

          while (kIt.hasNext()) 
          { 
           PGPPublicKey k = (PGPPublicKey)kIt.next(); 

           if (k.isEncryptionKey()) 
           { 
            return k; 
           } 
          } 
         } 

         throw new IllegalArgumentException("Can't find encryption key in key ring."); 
        } 
関連する問題