非Java 生成签名文件,使用BouncyCastle进行Java签名文件-使用秘密密钥环创建文件签名...

本文介绍了一个Java程序在使用Bouncy Castle库进行文件签名时遇到的编译警告问题。该程序接受文件路径、密钥环和密码作为输入参数,并生成一个*.bpg的独立签名文件。文章详细解释了编译时出现的警告原因,并提供了相应的解决建议。

I'm trying to write a Java program that signs a file with a private key. The program takes 3 arguments - file, secret keyring and a password. The output should be in a detached file *.bpg. The problem is that I get the following errors when I try to compile my code:

C:\CNS3\BCastle>javac Sign.java

Note: Sign.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

My code is as follows:

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();

}

}

The errors come from the following lines:

PGPPrivateKey pgpPrivKey =

key.extractPrivateKey(args[2].toCharArray(), "BC");

PGPSignatureGenerator sGen = new PGPSignatureGenerator(

key.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

Anyone have any suggestion on how I might fix this? Thanks a lot!

解决方案

First of all, the messages mentioned are not errors. They are warnings. Your program will run fine, but the methods or classes you use are marked as deprecated. That means you can still use them, but it is not recommended to do so, because in future versions of bouncy castle, these methods or classes might be removed.

Go to an up to date API documentation of these classes. There should be information what to use instead of the deprecated methods/classes.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值