首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JAR中的java.security.spec.InvalidKeySpecException错误不在集成开发环境中

JAR中的java.security.spec.InvalidKeySpecException错误不在集成开发环境中
EN

Stack Overflow用户
提问于 2018-04-13 22:23:22
回答 1查看 453关注 0票数 1

我写了一个RSA加密代码,它在集成开发环境下运行良好,但在生成JAR文件后,它给出了错误- java.security.spec.InvalidKeySpecException

代码语言:javascript
复制
private static PublicKey readPubKey()throws Exception{
    //reading public key from the path specified in the configuration file
    Config conf = new Config();
    InputStream keyfis = conf.getFileInputStream(publicKeyPath);
    byte[] encKey = new byte[keyfis.available()];
    keyfis.read(encKey);
    keyfis.close();

    // making the instance of Public key from the bytes of the Public Key
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
    java.security.KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    //---------------problem in this line of code-----------
    PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
    return publicKey;
}

问题就在这里-

代码语言:javascript
复制
PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);

异常的完整堆栈跟踪是-

代码语言:javascript
复制
Exception Caught : java.security.spec.InvalidKeySpecException: 
java.security.InvalidKeyException: exponent is smaller than 3
java.security.spec.InvalidKeySpecException: 
java.security.InvalidKeyException: exponent is smaller than 3
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205)
at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
at com.token.TokenVerifier.readPubKey(TokenVerifier.java:55)
at com.token.TokenVerifier.verify(TokenVerifier.java:77)
at com.test.Main.main(Main.java:28)
Caused by: java.security.InvalidKeyException: exponent is smaller than 3
at sun.security.rsa.RSAPublicKeyImpl.checkExponentRange(RSAPublicKeyImpl.java:99)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:88)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:298)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:201)
EN

回答 1

Stack Overflow用户

发布于 2020-07-31 04:34:37

使用以下代码将inputstream转换为字节数组。

代码语言:javascript
复制
InputStream keyfis = conf.getFileInputStream(publicKeyPath);
byte[] encKey = toByteArray(keyfis);
keyfis .close();

public static byte[] toByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    // read bytes from the input stream and store them in buffer
    while ((len = in.read(buffer)) != -1) {
        // write bytes from the buffer into output stream
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}

请参考下面的url。感谢实用程序方法toByteArray的以下url的作者。

https://www.techiedelight.com/convert-inputstream-byte-array-java/

如果你在同一个程序中验证签名,请在验证签名之前使用相同的方法转换为字节数组,否则也会在那里给出错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49819211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档