gpt4 book ai didi

java - 无效 key 异常 : Key Spec Not Recognised

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:21 42 4
gpt4 key购买 nike

我得到一个

InvalidKeyException: Illegal key size or default parameters

尝试运行已部署的 WAR 网络应用程序时。我在 Linux 环境中将其托管在 Tomcat 上。我已经将这两个 UnlimitedJCEPolicy 文件放入目标 /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();
}

使用日志,我能够发现错误发生在什么时候

InputStream clear = publicKeyEncryptedData.getDataStream(new  
JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(secretKey));

但我不知道为什么。就像我说的,我已经适本地放置了 JCEUnlimited 文件,但错误仍然存​​在。

编辑 我修复了非法 key 大小问题,但现在出现“无法识别 key 规范”

编辑 关于错误“无法识别关键规范”的更多详细信息:所以正如我所说,非法 key 大小已经消失,但“无法识别 key 规范”似乎仍然是一个问题。奇怪的是,我的 encryptFile 方法运行良好,但 decryptFile 抛出错误。我不完全确定为什么。下类前又测试了一次,好像没有报错。我几乎觉得这个错误是随机发生的,这取决于将 WAR 部署到 tomcat。如果我部署我的 WAR,错误不会在某些时候发生,但如果我取消部署并使用更新的 WAR 文件重新部署,则会抛出错误。我不知道是什么原因造成的,也没有人真正知道基于研究的结果。显然这曾经是 1.5 之前 Bouncy CaSTLe 中的一个错误,但 1.5 是我正在运行的版本,所以这不是问题。如果我发现任何可能修复此错误的内容,我会发布。

最佳答案

解决:

java.security.spec.InvalidKeySpecException: key spec not recognised

修改安全提供者:

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。

关于java - 无效 key 异常 : Key Spec Not Recognised,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810805/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com