Bouncy Castle方法仅适用于调试模式

时间:2015-11-10 05:33:56

标签: java bouncycastle

如果我正常运行代码,它会引发异常。一旦我切换到调试模式,它运行正常。有谁知道为什么以下只能在调试模式下运行?我试图在测试服务器上部署它也会引发异常。

尝试运行extractPrivateKey时代码中断。

private PGPPrivateKey privateKeyFromSecretKey(PGPSecretKey secretKey, String pass) throws PGPException {
    return secretKey.extractPrivateKey(
    new JcePBESecretKeyDecryptorBuilder(
            new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass.toCharArray()));
  }

我收到以下异常。

org.bouncycastle.openpgp.PGPException: exception on setup: java.security.NoSuchAlgorithmException: class configured for MessageDigest(provider: BC)cannot be found.
    at org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder$1.get(Unknown Source)
    at org.bouncycastle.openpgp.operator.PGPUtil.makeKeyFromPassPhrase(Unknown Source)
    at org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor.makeKeyFromPassPhrase(Unknown Source)
    at org.bouncycastle.openpgp.PGPSecretKey.extractKeyData(Unknown Source)
    at org.bouncycastle.openpgp.PGPSecretKey.extractPrivateKey(Unknown Source)

在进入上面显示的privateKeyFromSecretKey函数之前运行正常的其他代码。

private void encryptorInit(String publicKeyRing, boolean integrityCheck) throws IOException, PGPException {
    PGPPublicKey publicKey = readPublicKey(publicKeyRing);   

    JcePGPDataEncryptorBuilder PgpDataEncryptorBuilder = new JcePGPDataEncryptorBuilder(PGPEncryptedData.AES_256)
    .setWithIntegrityPacket(integrityCheck)
    .setSecureRandom(new SecureRandom())
    .setProvider("BC");

    encryptor = new PGPEncryptedDataGenerator(PgpDataEncryptorBuilder);
    encryptor.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider("BC"));
}

0 个答案:

没有答案