【发布时间】:2014-05-10 15:28:53
【问题描述】:
我正在使用 (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();
}
【问题讨论】:
-
请发布您正在使用的 -jmx 等标志/设置。两个系统的 IO/CPU/内存性能是否相同?
-
两台机器都有默认的虚拟机设置。我要传递的缓冲区大小是 2000 字节。当我增加缓冲区大小时,Windows 7 系统的性能甚至会下降
-
那么系统本身的性能呢?您是否运行了性能测试以比较两个系统(或者在同一台机器上比 linux 更好地启动第一个窗口)?
-
您忽略了
fIn.read(byteArray)的返回值。忽略速度问题,该代码的结果很有可能是完全垃圾,因为不能保证对fIn.read(byteArray)的调用可以准确读取byteArray.length字节,但是您将整个数组传递给lOut.write()和 @987654326 @. -
除了来自 @OlegEstekhin 的 cmets 之外,尝试使用 BufferedInputStream 而不是 FileInputStream。
标签: java bouncycastle openpgp