Java InvalidKeyException:未识别密钥规范

Java InvalidKeyException:未识别密钥规范,java,linux,bouncycastle,pgp,Java,Linux,Bouncycastle,Pgp,我得到一份工作 InvalidKeyException:密钥大小或默认参数非法 当尝试运行一个web应用程序时,它是一场已部署的战争。我在Linux环境中的Tomcat上托管它。我已经将这两个不受限制的cepolicy文件放入了目标/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/lib/security,似乎错误仍在发生。请注意,只有在linux环境中运行时才会引发此问题。在本地,它运行良好。这是我的密码: public static fi

我得到一份工作

InvalidKeyException:密钥大小或默认参数非法

当尝试运行一个web应用程序时,它是一场已部署的战争。我在Linux环境中的Tomcat上托管它。我已经将这两个不受限制的cepolicy文件放入了目标
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/lib/security
,似乎错误仍在发生。请注意,只有在linux环境中运行时才会引发此问题。在本地,它运行良好。这是我的密码:

public static final void decryptFile(File inputFile, File outputFile) throws 
IOException, PGPException {
    // Add Bouncy Castle provider
    Security.addProvider(new BouncyCastleProvider());

    // Grab secret key that's in folder with AE classes
    Resource resource = new ClassPathResource(Env.getSecretKeyAE());
    log.debug("Resource: " + Env.getSecretKeyAE());
    File keyFileName = resource.getFile();
    log.debug("Key File Name: " + keyFileName);
    // Decryption password
    String pass = "pass";
    char[] passwd = pass.toCharArray();

    // Read files into streams
    log.info("Reading files into streams");
    InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
    InputStream in = PGPUtil.getDecoderStream(new BufferedInputStream(new 
FileInputStream(inputFile)));

    // I don't even know what these do
    PGPObjectFactory pgpObjFactory = new PGPObjectFactory(in);
    PGPEncryptedDataList pgpEncryptedDataList = null;

    Object o = pgpObjFactory.nextObject();
    log.info("Checking instance of PGPEncryptedDataList");
    if (o instanceof PGPEncryptedDataList) {
        pgpEncryptedDataList = (PGPEncryptedDataList)o;
    }
    else {
        pgpEncryptedDataList = (PGPEncryptedDataList)pgpObjFactory.nextObject();
    }

    // This will be the PGPPrivateKey we use to decrypt
    log.info("Initializing secret key");
    PGPPrivateKey secretKey = null;
    PGPPublicKeyEncryptedData publicKeyEncryptedData = null;
    PGPSecretKeyRingCollection pgpSecretKeyRingCollection = new    
PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn));

    // This iterates the key file as if it has many keys, this file has only one
    // This is the only way I could find to construct a PGPPrivateKey
    log.info("Iterating through key file");
    Iterator<?> it = pgpEncryptedDataList.getEncryptedDataObjects();
    while(it.hasNext() && secretKey == null) {
        publicKeyEncryptedData = (PGPPublicKeyEncryptedData) it.next();
        PGPSecretKey pgpSecKey = 
pgpSecretKeyRingCollection.getSecretKey(publicKeyEncryptedData.getKeyID());

        if (pgpSecKey != null) {
            Provider provider = Security.getProvider("BC");
            secretKey = pgpSecKey.extractPrivateKey(new   

JcePBESecretKeyDecryptorBuilder(new  
JcaPGPDigestCalculatorProviderBuilder().setProvider(provider)
.build()).setProvider(provider).build(passwd));
           }
    }
    log.info("PGPPrivateKey has been constructed");
    if (secretKey == null) {
        throw new IllegalArgumentException("secret key for message not found.");
    }
    log.info("Secret Key found!");

    if(publicKeyEncryptedData == null) {
        throw new NullPointerException("cannot continue with null public key encryption 
data.");
    }
    log.info("Public Key Encrypted Data found!");

    // More stuff I don't fully understand, I think this is just standard way to   
decrypt files once the above is all set up
    log.info("Starting actual decryption");
    //get data stream where our publicKeyDataDecrypterFactory sets ours provider to BC 
and we build our secretKey
    //secretkey is our PGPPrivateKey

    log.info("start");

    //=====================================================================
    //ERROR IS OCCURRING HERE
    InputStream clear = publicKeyEncryptedData.getDataStream(new  
JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(secretKey));
    log.info("1");
    PGPObjectFactory plainFact = new PGPObjectFactory(clear);
    log.info("2");
    PGPCompressedData compressedData = (PGPCompressedData)plainFact.nextObject();
    log.info("3");
    InputStream compressedStream = new 
BufferedInputStream(compressedData.getDataStream());
    log.info("4");
    PGPObjectFactory pgpFact = new PGPObjectFactory(compressedStream);
    log.info("5");
    Object message = pgpFact.nextObject();
    log.info("6");

    if (message instanceof PGPLiteralData) {
        log.info("Our message is an instance of PGP Literal Data.");
        PGPLiteralData literalData = (PGPLiteralData)message;
        InputStream literalDataInputStream = literalData.getInputStream();
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
        Streams.pipeAll(literalDataInputStream, out);
        out.close();
    }
    else if (message instanceof PGPOnePassSignatureList) {
        log.error("encrypted message contains a signed message - not literal data.");
        throw new PGPException("encrypted message contains a signed message - not  

literal data.");
    }
    else {
        log.error("message is not a simple encrypted file - type unknown.");
        throw new PGPException("message is not a simple encrypted file - type  
unknown.");
    }
    log.info("Checking if public key encrypted data is integrity protected");
    if (publicKeyEncryptedData.isIntegrityProtected()) {
        if (!publicKeyEncryptedData.verify()) {
            throw new PGPException("message failed integrity check");
        }
    }

    keyIn.close();
    in.close();
}
但我不知道为什么。正如我所说,我已经适当地放置了JCEUnlimited文件,错误仍然发生

编辑我修复了非法密钥大小问题,但现在得到“密钥规格未识别”

编辑关于错误“未识别关键规范”的详细说明: 正如我所说,非法密钥大小已经消失,但“密钥规范未被识别”似乎仍然是一个问题。奇怪的是,我的encryptFile方法工作得很好,但decryptFile抛出了错误。我不完全清楚为什么。在我离开工作之前,我又测试了一次,似乎没有抛出错误。我几乎觉得这个错误是随机发生的,这取决于对tomcat的WAR部署。如果部署WAR,在某些时候不会发生错误,但是如果取消部署并使用更新的WAR文件重新部署,则会抛出错误。我不知道是什么原因导致了这一现象,基于研究的结果也没有人真正知道。显然,这在Bouncy Castle 1.5之前是一个bug,但是1.5是我正在运行的版本,所以这不是问题所在。我会张贴,如果我发现任何可能修复此错误

if (o instanceof PGPEncryptedDataList) {
        pgpEncryptedDataList = (PGPEncryptedDataList)o;
如果o已经是PGPEncryptedDataList的实例,为什么要将其强制转换为PGPEncryptedDataList

我对您正在做的事情的细节了解不够,所以我只是想提供一些通用的代码分析。很抱歉,我无法提供更多帮助。

为了防止出现“非法密钥大小或默认参数”错误,我只需将UnlimitedJCEPolicy文件放在我的工作java目录/opt/jre1.7.0_60/lib/security中。在把文件放在那里,并重新部署我的war文件之后,我不再遇到这个问题

为了防止出现“密钥规范未识别”错误,在重新部署WAR文件时,我必须重新启动tomcat服务器。

要解决此问题:

java.security.spec.InvalidKeySpecException:无法识别密钥规范

修改安全提供程序:

sudo nano $JAVA_HOME/jre/lib/security/java.security
添加安全提供程序:

security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
将bcprov-jdk15on-1.54.jar复制到:

$JAVA_HOME/jre/lib/ext/bcprov-jdk15on-1.54.jar

重新启动Tomcat。

老实说,这是一位前同事,所以我自己甚至不能告诉你为什么会这样。谢谢你让我注意到它!因为我使用的是Tomcat,所以我的java主页实际上在我的opt文件夹中。我必须放置UnlimitedJCEPolicy文件的确切位置是/opt/jre1.7.0_60/lib/security。在把文件放在那里,并重新部署我的war文件之后,我不再遇到这个问题。
$JAVA_HOME/jre/lib/ext/bcprov-jdk15on-1.54.jar