I have code that works well on Android. When I ported it to my Windows 64-bit machine with JRE 1.6, the code did not work.
我有在Android上运行良好的代码。当我用JRE 1.6将它移植到我的Windows 64位机器上时,代码就不起作用了。
When I run the following line of code:
当我运行以下代码行:
final MessageDigest digest = MessageDigest.getInstance("SHA256")
I get the following exception:
我得到了如下的例外:
java.security.NoSuchAlgorithmException: SHA256 MessageDigest not available
at sun.security.jca.GetInstance.getInstance(Unknown Source)
at java.security.Security.getImpl(Unknown Source)
at java.security.MessageDigest.getInstance(Unknown Source)
I found on Internet people claiming that it is possible to use SHA256 with the standard crypto provider that comes with Sun JRE and people saying that I need to use another provider like for example Bouncy Castle.
我在互联网上发现有人声称可以将SHA256与Sun JRE附带的标准加密提供商一起使用,也有人说我需要使用另一个提供商,比如Bouncy Castle。
I would prefer not to use a different provider. Is it possible to make it working?
我不希望使用不同的提供者。有可能让它工作吗?
2 个解决方案
#1
29
When in doubt over what algorithms you can use for a JCA service, your first port of call should be the JCA Standard Algorithm Name Documentation. The algorithms guaranteed to be supported by the MessageDigest service in a JCA-compliant JVM are:
当您对JCA服务使用的算法有疑问时,您的第一个调用端口应该是JCA标准算法名称文档。兼容jca的JVM中的MessageDigest服务保证支持的算法是:
MD2
- MD2
MD5
- MD5
SHA-1
- sha - 1
SHA-256
- sha - 256
SHA-384
- sha - 384
SHA-512
- sha - 512
It's common for providers to supply aliases for these algorithms, which is why it'd probably work with Bouncy Castle, but you should stick to these if you can to maximise portability.
提供者为这些算法提供别名是很常见的,这也是为什么它可能与Bouncy Castle一起工作的原因,但是如果您能够最大化可移植性,就应该坚持使用这些算法。
If you change your code to the following, it will work as expected:
如果您将代码更改为以下代码,它将按预期工作:
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
#2
4
SHA-256 should be the parameter to getInstance()
SHA-256应该是getInstance()的参数
Link for the list of algorithms supported for message digest
链接到消息摘要支持的算法列表
#1
29
When in doubt over what algorithms you can use for a JCA service, your first port of call should be the JCA Standard Algorithm Name Documentation. The algorithms guaranteed to be supported by the MessageDigest service in a JCA-compliant JVM are:
当您对JCA服务使用的算法有疑问时,您的第一个调用端口应该是JCA标准算法名称文档。兼容jca的JVM中的MessageDigest服务保证支持的算法是:
MD2
- MD2
MD5
- MD5
SHA-1
- sha - 1
SHA-256
- sha - 256
SHA-384
- sha - 384
SHA-512
- sha - 512
It's common for providers to supply aliases for these algorithms, which is why it'd probably work with Bouncy Castle, but you should stick to these if you can to maximise portability.
提供者为这些算法提供别名是很常见的,这也是为什么它可能与Bouncy Castle一起工作的原因,但是如果您能够最大化可移植性,就应该坚持使用这些算法。
If you change your code to the following, it will work as expected:
如果您将代码更改为以下代码,它将按预期工作:
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
#2
4
SHA-256 should be the parameter to getInstance()
SHA-256应该是getInstance()的参数
Link for the list of algorithms supported for message digest
链接到消息摘要支持的算法列表