Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Bouncycastle PGPContentSigner清除对字节数组的签名?_Java_Bouncycastle_Pgp - Fatal编程技术网

Java 如何使用Bouncycastle PGPContentSigner清除对字节数组的签名?

Java 如何使用Bouncycastle PGPContentSigner清除对字节数组的签名?,java,bouncycastle,pgp,Java,Bouncycastle,Pgp,我试图在bouncycastle 1.49版中使用未弃用的构造函数,但我很难弄清楚如何使用这些构造函数创建的对象,因为它与我在web上找到的任何教程都有点不同 这是我到目前为止的代码;有人能告诉我应该如何处理PGPContentSigner,以及如何将它连接到OutputStream吗?我想要实现的是在数据上附加一个签名,而不必向任何人加密数据(很像gpg--clearsign-a) 我已经研究了ArmoredOutputStream及其方法,BeginLearText(int)看起来很有希望,

我试图在bouncycastle 1.49版中使用未弃用的构造函数,但我很难弄清楚如何使用这些构造函数创建的对象,因为它与我在web上找到的任何教程都有点不同

这是我到目前为止的代码;有人能告诉我应该如何处理PGPContentSigner,以及如何将它连接到OutputStream吗?我想要实现的是在数据上附加一个签名,而不必向任何人加密数据(很像
gpg--clearsign-a

我已经研究了
ArmoredOutputStream
及其方法,
BeginLearText(int)
看起来很有希望,但是仅仅调用它,将数据转储到输出流中,调用
endClearText
,然后将签名字节写入
ArmoredOutputStream
是行不通的。看起来似乎需要对流进行低级操作,将控制字节插入流中以发出签名开始的信号,等等。在我看来,应该有某种固定装置将签名者和铠装输出流连接在一起,以处理数据包的篡改

/**
 * Generate a signature for the given bytes so that they can be sent off and the recipient can verify
 * that the bytes have not been tampered with in transit.
 *
 * @param dataBytes the data to sign
 * @return the data along with the signature
 * @throws PGPException if there's a problem generating the signature
 */
public static byte[] clearSignBytes(byte[] dataBytes, PGPSecretKeyRingCollection skrCollection, String keyPass) throws PGPException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); // this is where we put the signed data

    try {
        // get our secret key so we can init the signature generator
        Iterator<PGPSecretKeyRing> it = skrCollection.getKeyRings();
        PGPSecretKeyRing skr = it.next();
        PGPSecretKey skey = skr.getSecretKey();
        PGPPrivateKey prKey = skey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(keyPass.toCharArray()));
        BcPGPContentSignerBuilder signerBuilder = new BcPGPContentSignerBuilder(skey.getPublicKey().getAlgorithm(), PGPUtil.SHA256);
        PGPContentSigner signer = signerBuilder.build(PGPSignature.BINARY_DOCUMENT, prKey);

        // Now, we're supposed to write dataBytes somewhere and we're supposed to hand them to the signer somehow
        // and ultimately we're supposed to tell the signer to output a signature and we put the signature and
        // dataBytes together into baos.
        // TODO ??????
    } catch (Exception e) {
        __l.error("Exception generating signature", e);
        throw new PGPException("Exception while signing the data", e);
    }
    return baos.toByteArray();
}
/**
*为给定字节生成签名,以便发送这些字节并让收件人验证
*字节在传输过程中未被篡改。
*
*@param dataBytes要签名的数据
*@返回数据和签名
*如果生成签名时出现问题,@将抛出PGPException
*/
公共静态字节[]清除信号字节(字节[]数据字节,PGPSecretKeyRingCollection skrCollection,字符串keyPass)引发PGPEException{
ByteArrayOutputStream baos=new ByteArrayOutputStream();//这是我们放置签名数据的地方
试一试{
//获取我们的密钥,这样我们就可以初始化签名生成器
迭代器it=skrCollection.getKeyRings();
PGPSecretKeyRing skr=it.next();
PGPSecretKey skey=skr.getSecretKey();
PGPPrivateKey prKey=skey.extractPrivateKey(新的BcPBESecretKeyDecryptorBuilder(新的BcPGPDigestCalculatorProvider()).build(keyPass.toCharArray());
BcPGPContentSignerBuilder signerBuilder=新的BcPGPContentSignerBuilder(skey.getPublicKey().getAlgorithm(),PGPUtil.SHA256);
PGPContentSigner signer=signerBuilder.build(PGPSignature.BINARY_文档,prKey);
//现在,我们应该在某个地方写入数据字节,然后以某种方式将它们交给签名者
//最终我们应该告诉签名者输出一个签名,然后我们把签名和
//将数据字节合并到BAO中。
//待办事项??????
}捕获(例外e){
__l、 错误(“生成签名的异常”,e);
抛出新的PGPEException(“数据签名时异常”,e);
}
返回baos.toByteArray();
}

结果是我没有使用正确的类来完成工作。下面是该方法的相关部分以及实际工作的代码。我希望这能帮助其他有同样困惑的人。在我们获得
PGPPrivateKey prKey…

        PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(skey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC"));
        PGPSignatureSubpacketGenerator  spGen = new PGPSignatureSubpacketGenerator();

        sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, prKey);
        Iterator userIDs = skey.getPublicKey().getUserIDs();
        if (it.hasNext()) {
            spGen.setSignerUserID(false, (String)userIDs.next());
            sGen.setHashedSubpackets(spGen.generate());
        }

        ArmoredOutputStream aos = new ArmoredOutputStream(baos);
        aos.beginClearText(PGPUtil.SHA256);

        sGen.update(dataBytes);
        aos.write(dataBytes);

        aos.endClearText();

        BCPGOutputStream bOut = new BCPGOutputStream(aos);
        sGen.generate().encode(bOut);

        aos.flush();
        aos.close();