3

我使用 (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();
}
4

1 回答 1

7

这是一个有根据的猜测,也许你对 /dev/random 有问题?

PGP 将使用安全散列,在 Java 中可能依赖于 SecureRandom。Linux(但不是 Windows)中 SecureRandom 的默认源是 /dev/random。

问题是 SecureRandom 如果当前不能满足请求的位数,它将阻止等待 /dev/random 收集更多熵。

尝试安装一个名为“haveged”的实用程序(apt-get install 或其他)。它将为您的 linux 系统收集更多熵并防止这种行为。

于 2014-05-17T00:05:14.500 回答