充气城堡 PGPObjectFactory nextObject returns 空

bouncy castle PGPObjectFactory nextObject returns null

我正在尝试使用 RSA public 和私钥解密和加密消息。

当我尝试阅读 PGPObjectFactory.nextObject() 时,我得到 null 下面是我的代码。

  Security.addProvider(new BouncyCastleProvider());
        InputStream in = PGPUtil.getDecoderStream(new ByteArrayInputStream(encryptedString.getBytes()));
        JcaPGPObjectFactory pgpFact;
        PGPObjectFactory pgpF = new PGPObjectFactory(in, new JcaKeyFingerprintCalculator());

        Object o = pgpF.nextObject(); // I get this null
        PGPEncryptedDataList encList;

        if(o instanceof PGPEncryptedDataList)
        {
            encList = (PGPEncryptedDataList) o;
        }else {

            encList = (PGPEncryptedDataList) pgpF.nextObject();
        }

        Iterator<PGPPublicKeyEncryptedData> itt = encList.getEncryptedDataObjects(); 

相同的代码在 java 1.8 中工作,但是当 运行 在 java 1.7 中出现这个问题时,

我正在使用所有 bouncy castel 1.63

请帮忙 谢谢

以防万一有人遇到问题,即使尝试了所有方法仍然无法正常工作。

对于我的情况,它的 bit size 默认情况下只允许 256 因为我的密钥位大小是 2048 它返回 null

要解决此问题,请在 $JRE_HOME/lib/secuirty/ 中添加 policy.jarus-export.jar,因为 java8 您不需要添加。

如果您想检查您的 java 是否允许您的位大小,您可以通过以下方式检查。

System.out.println( javax.crypto.Cipher.getMaxAllowedKeyLength("AES"));