Java Bouncy Castle方法仅在调试模式下工作
如果我正常运行代码,它会抛出一个异常。一旦我切换到调试模式,它就会正常运行。有人知道为什么下面的代码只能在调试模式下运行吗?我曾尝试在测试服务器上部署,但它也引发了一个异常 尝试运行extractPrivateKey时代码中断 我得到以下例外Java Bouncy Castle方法仅在调试模式下工作,java,bouncycastle,Java,Bouncycastle,如果我正常运行代码,它会抛出一个异常。一旦我切换到调试模式,它就会正常运行。有人知道为什么下面的代码只能在调试模式下运行吗?我曾尝试在测试服务器上部署,但它也引发了一个异常 尝试运行extractPrivateKey时代码中断 我得到以下例外 org.bouncycastle.openpgp.PGPException: exception on setup: java.security.NoSuchAlgorithmException: class configured for MessageD
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"));
}
我曾经有过类似的问题。解决方案是,我在调试器中有一个表达式,它被执行并产生副作用,导致实际应用程序在有调试器和没有调试器的情况下表现出真正的不同可能是在运行代码之前未注册提供程序。在何处执行Security.addProvidernew BouncyCastleProvider;?嘘,我不用看屏幕就可以打那行了。只需在调用重新注册提供程序之前添加该提供程序,它将只添加一次。@MaartenBodewes在类的开头执行静态{Security.addProvidernew BounchyScastleProvider;}
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"));
}