是RC4还是ARCFOUR?使用 SecretKeySpec 时出现 InvalidKeyException?
我尝试在我的电脑上运行我的应用程序,但我不断收到这个东西。我是否可能缺少一些库?
fabsam.crypto.CryptoException: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:37) ~[bin/:na]
... (skipped my projects stack trace)
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:27) ~[bin/:na]
... 5 common frames omitted
Caused by: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:25) ~[fabsam-08.05.11.jar:na]
... 5 common frames omitted
代码:
cipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(key.getBytes(DEFAULT_CHARSET), ALGORITHM));
是否是因为我在 ALGORITHM 变量中使用的是 RC4
而不是 ARCFOUR
? 当我尝试 ARCFOUR 时,我得到这样的信息:
fabsam.crypto.CryptoException: java.security.InvalidKeyException: Illegal key size or default parameters
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:37) ~[bin/:na]
... (skipped my projects stack trace)
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:27) ~[bin/:na]
... 5 common frames omitted
好的,问题不在于代码。在服务器上运行得很好,没有错误。然而,在我的电脑上,它向我抛出 InvalidKeyException 异常。所以这与 jvm 相关。有什么想法吗?
编辑: 我现在发现我遇到了两个例外。不是立即,而是第一次使用非法密钥大小,然后没有安装提供商。运行我的项目时,我将算法设置为“ARCFOUR”。
I tried running my application on my pc, but I keep getting this thing. Is it possible that I'm missing some libraries?
fabsam.crypto.CryptoException: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:37) ~[bin/:na]
... (skipped my projects stack trace)
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:27) ~[bin/:na]
... 5 common frames omitted
Caused by: java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:25) ~[fabsam-08.05.11.jar:na]
... 5 common frames omitted
Code:
cipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(key.getBytes(DEFAULT_CHARSET), ALGORITHM));
Could it be because I'm using RC4
not ARCFOUR
in the ALGORITHM variable?
When I try ARCFOUR I get this:
fabsam.crypto.CryptoException: java.security.InvalidKeyException: Illegal key size or default parameters
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:37) ~[bin/:na]
... (skipped my projects stack trace)
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
at fabsam.crypto.RC4Decoder.decode(RC4Decoder.java:27) ~[bin/:na]
... 5 common frames omitted
Okay, the problem is not with the code. On server this runs just fine with no errors. However on my pc it throws me that InvalidKeyException exception. So it's something with jvm related.. Any ideas?
Edit:
I now see that I'm getting both exceptions. Not at once, but first time the illegal key size and then no installed provider. I've the ALGORITHM set to "ARCFOUR" when running my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @DaveHowes 所暗示的,在第一个堆栈跟踪的情况下,您可能正在使用第三方 JCE 提供程序,并且在第二个堆栈跟踪的情况下,您的密钥大小无效。
Sun 包含多个提供程序,其参数记录在此处 。请注意,根据 SunJCE 提供程序的文档< /a>,算法的名称是“ARCFOUR”,而不是“RC4”。我的猜测是,当您指定“RC4”时,您将获得“fabsam”提供程序实现,无论它是什么。当您指定“ARCFOUR”时,您将获得 Sun 实现。另请注意密钥大小限制,指定“ARCFOUR”的密钥大小必须介于 40 位和 1024 位(包括 5 字节和 128 字节)之间(包括 5 字节和 128 字节)。您程序中的 String 对象
key
可能太小或太大,请检查这一点。As alluded to by @DaveHowes it is likely you are using a third-party JCE provider in the case of the first stacktrace, and that your keysize is invalid in the case of the second stacktrace.
Sun includes several providers whose parameters are documented here. Note that according to the documentation for the SunJCE provider, the name of the algorithm is "ARCFOUR", not "RC4". My guess is that when you specified "RC4" you got the 'fabsam' provider implementation, whatever that is. When you specified 'ARCFOUR' you got the Sun implementation. Note also the keysize restrictions which specify that 'ARCFOUR' must have a keysize between 40 bits and 1024 bits inclusive (that's 5 bytes and 128 bytes inclusive). The String object
key
in your program may be either too small or too large, please check this.经过大量的挣扎和搜索以及一切,我得到了正确的答案。
在这里检查我的问题:Java 安全性:非法密钥大小或默认参数?< /a> 如果您遇到此问题!
After lot of struggling and searching and everything, I got the right answer.
Check my question here: Java Security: Illegal key size or default parameters? if you are experiencing this problem!