2017-12-06 23 views
1

私はBouncy Castle PGPの実装を探しています。「署名して暗号化する」。理想的には1つの操作で、それが何らかの違いがある場合。Bouncy Castle PGPはワンパスで署名し、暗号化しますか?

私はencrypt examplesigning exampleを取得し、それを「ワンパス」暗号化と符号操作に変えようとしました。

この比較的古い日付の実装Boncodeを参照してください。これは、2つの操作が単にリンクされていることを示しているようです。

私は消費者にコードの復号化を依頼していません。署名が確認できるようです。これは、マージされた操作を使用するか、別の暗号化した符号を使用するかにかかわらず当てはまります。

Bouncy Castle PGPの実装が改善されていますか?

+0

メイシングの3つの方法があります:Authenticate Then Encrypt、Authenticate And Encrypt(あなたの望むもの)とEncrypt Then Authenticate。セキュリティは3つのすべてで同じではありません。 Moxieの立場は、解読時の認証に先立って可能な限り行うべきであるということです。実際にTLS AES/CBCの1つの脆弱性は、それがMacの暗号化ではないということです。選択はセキュリティに影響します。 [The Cryptographic Doom Principle](https://moxie.org/blog/the-cryptographic-doom-principle/) – zaph

答えて

0

ここに私の現在の実装であるBouncy Castle PGP encrypt + signがあります。署名は検証されたように見えますが、ペイロードは復号化されません。

public class SinglePassSignedEncryptedFileProcessor { 
private static final Logger logger = LoggerFactory.getLogger(SinglePassSignedEncryptedFileProcessor.class); 

/* 
* This is the primary function that will create encrypt a file and sign it 
* with a one pass signature. This leans on an C# example by John Opincar 
* @author Bilal Soylu 
* @param targetFileName 
*   -- file name on drive systems that will contain encrypted content 
* @param embeddedFileName 
*   -- the original file name before encryption 
* @param secretKeyRingInputStream 
*   -- Private Key Ring File 
* @param targetFileStream 
*   -- The stream for the encrypted target file 
* @param secretKeyPassphrase 
*   -- The private key password for the key retrieved from 
*   collection used for signing 
* @param signPublicKeyInputStream 
*   -- the public key of the target recipient to be used to 
*   encrypt the file 
* @throws Exception 
*/ 
public void encryptOnePassSign(
     String fileName, 
     InputStream keyIn, 
     OutputStream out, 
     char[] pass, 
     PGPPublicKey encryptionKey, 
     boolean armor, 
     boolean withIntegrityCheck, 
     String providerName) 
     throws IOException, NoSuchAlgorithmException, NoSuchProviderException, PGPException, SignatureException { 
    if (armor) { 
     out = new ArmoredOutputStream(out); 
    } 

    // Compress 
    byte[] bytes = PGPEncryptUtil.compressFile(fileName, CompressionAlgorithmTags.ZIP); 

    // Encryption process. 
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
      new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC")); 

    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encryptionKey).setProvider("BC")); 

    ByteArrayOutputStream encryptedOutputStream = new ByteArrayOutputStream(); 
    OutputStream encryptedOut = encGen.open(encryptedOutputStream, bytes); 
    encryptedOut.write(bytes); 
    encryptedOut.close(); 
    byte[] bytesEncrypted = encryptedOutputStream.toByteArray(); 
    encryptedOutputStream.close(); 

    // Signing process. 
    PGPSecretKey pgpSec = PGPEncryptUtil.readSecretKey(keyIn); 
    PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass)); 

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setProvider("BC")); 

    sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); 

    Iterator it = pgpSec.getPublicKey().getUserIDs(); 
    if (it.hasNext()) { 
     PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); 

     spGen.setSignerUserID(false, (String) it.next()); 
     sGen.setHashedSubpackets(spGen.generate()); 
    } 

    PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
      PGPCompressedData.UNCOMPRESSED); 

    // Write to the output stream. 
    BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out)); 
    sGen.generateOnePassVersion(false).encode(bOut); 

    File file = new File(fileName); 
    PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); 
    // file is encoding name. 
    Date lastModified = new Date(file.lastModified()); 
    OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, fileName, lastModified, bytesEncrypted); 


    //FileInputStream fIn = new FileInputStream(file); 
    //int ch; 

    //while ((ch = fIn.read()) >= 0) { 
     lOut.write(bytesEncrypted); 
     sGen.update(bytesEncrypted); 
    // } 

    // ? 
    lGen.close(); 

    sGen.generate().encode(bOut); 
    cGen.close(); 

    if (armor) { 
     out.close(); 
    } 
    // close everything down we are done 
    /* 
    literalOut.close(); 
    literalDataGenerator.close(); 
    signatureGenerator.generate().encode(compressedOut); 
    compressedOut.close(); 
    compressedDataGenerator.close(); 
    encryptedOut.close(); 
    encryptedDataGenerator.close(); 
    */ 

    // if (armor) targetFileStream.close(); 

} 
} 
+0

の間違った公開鍵が提供されている可能性があります。 – Interlated

関連する問題