我正在使用(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();
}
最佳答案
这是一个有根据的猜测,也许你遇到了/dev/random 的问题?
PGP 将使用安全散列,这在 Java 中可能依赖于 SecureRandom。 Linux(但不是 Windows)中 SecureRandom 的默认源是/dev/random。
问题是,如果 SecureRandom 当前不能满足请求的位数,它会阻塞等待/dev/random 收集更多的熵。
尝试安装一个名为“haveged”的实用程序(apt-get install 或其他)。它将为您的 Linux 系统收集更多的熵并防止这种行为。
关于java - 在 Unix 上使用 BouncycaSTLe 的 OpenPgp 加密非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582894/