gpt4 book ai didi

使用 BouncyCaSTLe 进行 Java 签名文件 - 使用 secret key 环创建文件签名

转载 作者:行者123 更新时间:2023-12-01 15:01:35 26 4
gpt4 key购买 nike

我正在尝试编写一个使用私钥对文件进行签名的 Java 程序。该程序需要 3 个参数 - 文件、 secret key 环和密码。输出应位于独立文件 *.bpg 中。问题是当我尝试编译代码时出现以下错误:

C:\CNS3\BCastle>javac Sign.java
Note: Sign.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

我的代码如下:

import java.io.*;
import java.security.*;
import java.util.Iterator;

import org.bouncycastle.bcpg.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.*;

public class Sign {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
FileInputStream keyIn = new FileInputStream(args[1]);
FileOutputStream out = new FileOutputStream(args[0] + ".bpg");
InputStream in = PGPUtil.getDecoderStream(keyIn);
PGPSecretKeyRingCollection pgpSec =
new PGPSecretKeyRingCollection(in);
PGPSecretKey key = null;
Iterator rIt = pgpSec.getKeyRings();
while (key == null && rIt.hasNext()) {
PGPSecretKeyRing kRing = (PGPSecretKeyRing)rIt.next();
Iterator kIt = kRing.getSecretKeys();
while ( key == null && kIt.hasNext() ) {
PGPSecretKey k = (PGPSecretKey)kIt.next();
if ( k.isSigningKey() ) { key = k; }
}
}
if (key == null) {
throw new IllegalArgumentException("Can't find key");
}
PGPPrivateKey pgpPrivKey =
key.extractPrivateKey(args[2].toCharArray(), "BC");
PGPSignatureGenerator sGen = new PGPSignatureGenerator(
key.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");
sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
PGPCompressedDataGenerator.ZLIB);
BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out));
FileInputStream fIn = new FileInputStream(args[0]);
int ch = 0;
while ( (ch = fIn.read()) >= 0 ) { sGen.update((byte)ch); }
sGen.generate().encode(bOut);
cGen.close();
out.close();
}
}

错误来自以下几行:

    PGPPrivateKey pgpPrivKey = 
key.extractPrivateKey(args[2].toCharArray(), "BC");
PGPSignatureGenerator sGen = new PGPSignatureGenerator(
key.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");
sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

有人对我如何解决这个问题有任何建议吗?非常感谢!

最佳答案

首先,提到的消息不是错误。它们是警告。您的程序将运行良好,但您使用的方法或类被标记为已弃用。这意味着您仍然可以使用它们,但不建议这样做,因为在充气城堡的 future 版本中,这些方法或类可能会被删除。

访问这些类的最新 API 文档。应该有关于使用什么来代替已弃用的方法/类的信息。

关于使用 BouncyCaSTLe 进行 Java 签名文件 - 使用 secret key 环创建文件签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com