0
我正在嘗試編寫一個用私鑰簽名文件的Java程序。該程序需要3個參數 - 文件,密鑰環和密碼。輸出應該在分離的文件* .bpg中。問題是,我得到以下錯誤,當我嘗試編譯我的代碼:帶BouncyCastle的Java簽名文件 - 使用密鑰環創建文件簽名
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);
任何人有任何建議我可以解決這個問題?非常感謝!
感謝您的回答。我真的很困惑與不贊成的方法,我沒有意識到代碼將工作。謝謝@Daniel! –