Sha*_*zal 3 java bouncycastle openpgp
我正在使用(bcpg-jdk16-145.jar,bcprov-jdk16-145.jar)jar文件对12 GB的文本文件进行签名和加密。在Windows Vista,jdk 1.6中,文件将被加密并签名大约18分钟。但是,当我尝试在LINUX / UNIX系统上对其进行加密时,进程将变得非常缓慢,我需要花费1到1:30个小时。请提示。
签名文件的代码如下:
private static void signFile(String fileName, InputStream keyIn,
OutputStream out, char[] pass, boolean armor, int bufferSize)
throws IOException, NoSuchAlgorithmException,
NoSuchProviderException, PGPException, SignatureException {
if (armor) {
out = new ArmoredOutputStream(out);
}
PGPSecretKey pgpSec = readSecretKey(keyIn);
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(pass, "BC");
PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSec
.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");
sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
Iterator it = pgpSec.getPublicKey().getUserIDs();
if (it.hasNext()) {
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, (String) it.next());
sGen.setHashedSubpackets(spGen.generate());
}
PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
PGPCompressedData.ZLIB);
BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out));
sGen.generateOnePassVersion(false).encode(bOut);
File file = new File(fileName);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, file);
FileInputStream fIn = new FileInputStream(file);
byte[] byteArray = new byte[bufferSize];
while (fIn.read(byteArray) >= 0) {
lOut.write(byteArray);
sGen.update(byteArray);
}
lGen.close();
sGen.generate().encode(bOut);
cGen.close();
out.close();
}
Run Code Online (Sandbox Code Playgroud)
这是有根据的猜测,也许您在使用/ dev / random时遇到问题?
PGP将使用安全哈希,在Java中可能会依赖SecureRandom。Linux(而非Windows)中SecureRandom的默认源是/ dev / random。
问题在于,如果SecureRandom当前无法满足请求的位数,它将阻止等待/ dev / random收集更多的熵。
尝试安装名为“ haveged”的实用程序(apt-get install或其他工具)。它将为您的linux系统收集更多的熵,并防止此行为。