2017-10-04 48 views
0

セッション鍵を個別に解読できるように、セッション鍵を抽出したいPGP Public-Key Encrypted Session Packetがあります。私ははBouncyCastleライブラリを使用していますし、このようなセッションキーを抽出しています:Bouncy Castle公開鍵暗号化セッションパケットからのPGPセッション鍵の抽出

private static void outputSessionKey(String path) throws FileNotFoundException, IOException { 
    BCPGInputStream input = new BCPGInputStream(PGPUtil.getDecoderStream(new FileInputStream(path))); 
    Packet packet; 
    while((packet = input.readPacket()) != null) { 
     if (packet instanceof PublicKeyEncSessionPacket) { 
      PublicKeyEncSessionPacket encPacket = (PublicKeyEncSessionPacket) packet; 
      byte[] encKey = encPacket.getEncSessionKey()[0]; 
      FileOutputStream output = new FileOutputStream("session_key_enc.bin"); 
      output.write(encKey); 
      output.close(); 
     } 
    } 

    input.close(); 
} 

私は、OpenSSLを使用してセッション鍵を復号することができるように期待していた。

openssl rsautl -decrypt -in session_key_enc.bin -out session_key_decoded.bin -inkey private.pem 

session_key_enc.binはバイナリ形式の私の暗号化されたセッション鍵で、private.pemはGPGで自分のデータを暗号化するために使用した公開鍵の対応する秘密鍵です。データを暗号化する前に、自分のRSA鍵ペアの公開鍵部分をPGP形式の鍵に変換し、GPGにインポートしました。私は、ファイルが258バイトであることがわかっsession_key_enc.binを調べる際に

RSA operation error 
140624851898072:error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len:rsa_eay.c:518: 

私はopensslコマンドを実行すると、私はこのエラーを取得します。

Algorithm Specific Fields for RSA encryption - multiprecision integer (MPI) of RSA encrypted value m**e mod n.

The value "m" in the above formulas is derived from the session key as follows. First, the session key is prefixed with a one-octet algorithm identifier that specifies the symmetric encryption algorithm used to encrypt the following Symmetrically Encrypted Data Packet. Then a two-octet checksum is appended, which is equal to the sum of the preceding session key octets, not including the algorithm identifier, modulo 65536. This value is then encoded as described in PKCS#1 block encoding EME-PKCS1-v1_5 in Section 7.2.1 of [RFC3447] to form the "m" value used in the formulas above. See Section 13.1 of this document for notes on OpenPGP's use of PKCS#1.

このパズルを解決する方法上の任意のアドバイスをします:それは私が2048ビットのRSAキーを使用していますし、仕様は暗号化されたセッション鍵をnモッドされていることを示し考えることが可能なはずであるようにこれがいないようです感謝しています、ありがとう!

+0

パケットの最初のいくつかのヘッダーバイトを分割しましたか(またはBouncy Castleがすでにそうしていることを確認しましたか)。 'pgpdump -pi'もセッションキーを整数値として出力する必要があります。あなたはJavaコードの結果をこの値と比較できるはずです。 –

+0

@ JensErat Bouncy Castleはセッション鍵をMPI形式で出力し、最初の2バイトは合計の側を表します。それらを取り除いた後で、私はセッション鍵を解読することができますが、まだ解読された生のバイトから感知できる鍵を得ることができません –

答えて

0

Bouncy Castleは、最初の2バイトが長さであるMPIフォーマットを使用して暗号化されたセッションキーをエクスポートします。これは、元の問題を解決しました。セッションキーが256ではなく258バイトだったからです。

--override-session-keyを使用してファイルを解読することはできません今復号化されたセッション鍵の生のバイトとを含む。

関連する問題