我必须在远程 SFTP 服务器中放置一个文件,然后我必须使用私钥对文件进行签名,然后他们将使用公钥对其进行验证。我从响应文件中收到“PGP 签名验证失败”错误。
所以我试图验证来自JAVA的标志。不过,我从签名验证方法中得到的值是错误的。
任何帮助,将不胜感激。
这是我放在一起的代码。
公共课 SignAndVerify {
static final KeyFingerPrintCalculator FP_CALC = new BcKeyFingerprintCalculator();
private static File publicKeyFile = new File("\\publicSign.asc");
private static File privateKeyFile = new File("\\privateSign.asc");
private static final BouncyCastleProvider provider = new BouncyCastleProvider();
static {
Security.addProvider(provider);
}
public static void signFile(String fileName, PGPSecretKey secretKey, String secretPwd, boolean armor, OutputStream out)
throws PGPException {
BCPGOutputStream bOut = null;
OutputStream lOut = null;
InputStream fIn = null;
try {
OutputStream theOut = armor ? new ArmoredOutputStream(out) : out;
PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider(provider).build(secretPwd.toCharArray()));
PGPSignatureGenerator sGen = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1)
.setProvider(provider));
sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
Iterator<String> it = secretKey.getPublicKey().getUserIDs();
if (it.hasNext()) {
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, (String) it.next());
sGen.setHashedSubpackets(spGen.generate());
}
bOut = new BCPGOutputStream(theOut);
sGen.generateOnePassVersion(false).encode(bOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
lOut = lGen.open(bOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[2048]);
fIn = new BufferedInputStream(new FileInputStream(fileName));
byte[] buf = new byte[2048];
}
慕桂英3389331
相关分类