gpt4 book ai didi

java - 充气城堡 "encoded key spec not recognised"

转载 作者:行者123 更新时间:2023-12-02 05:49:43 26 4
gpt4 key购买 nike

在亚马逊 Linux 中运行我的 jar 后,我遇到了这个特定的错误。它在我的 OsX 中运行良好。我在两台机器上使用相同的公钥和私钥。唯一的区别是 java 版本是

我制作、测试和编译脚本的机器:

Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)

亚马逊服务器:

OpenJDK Runtime Environment (amzn-2.4.7.1.40.amzn1-x86_64 u55-b13)

这是导致错误的脚本部分:

public PublicKey getPublicKey(String _file)
throws
NoSuchAlgorithmException,
NoSuchProviderException,
InvalidKeySpecException,
IOException
{
X509EncodedKeySpec _spec = new X509EncodedKeySpec(_getFileContents(_file));
KeyFactory _keyFactory = KeyFactory.getInstance(this._keyFactoryAlgo, this._provider);

this._publicKey = _keyFactory.generatePublic(_spec);

return this._publicKey;
}

获取文件内容:

private byte[] _getFileContents(String _fileName) throws IOException
{

File _file = new File(_fileName);
FileInputStream _fileStream = new FileInputStream(_file);

byte[] _contents = new byte[(int) _file.length()];

_fileStream.read(_contents);

if(_fileStream != null)
{
_fileStream.close();
_fileStream = null;
}

return _contents;
}

以下是完整的错误消息:

java.security.spec.InvalidKeySpecException: encoded key spec not recognised
at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(KeyFactory.java:328)
at xxx.CryptKey.getPublicKey(CryptKey.java:167)
at xxx.CryptSession.encryptWithPublicKey(CryptSession.java:316)
at xxx.Crypt.encrypt(Crypt.java:57)
at snippet.Snippet.main(Snippet.java:201)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

最佳答案

您确实错误地读取了文件。您忽略了 InputStream.read() 的返回值。您应该循环调用此方法,因为单独的调用不能保证读取整个字节数组。

您可以手动阅读:

int offset = 0;
int read = _fileStream.read(_contents, 0, contents.length);
while (read > 0) {
offset += read;
read = _fileStream.read(_contents, offset , contents.length - offset );

}

或者您可以将底层输入流包装到 DataInputStream 中并仅使用 DataInputStream.readFully()方法。

关于java - 充气城堡 "encoded key spec not recognised",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691995/

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