Exception when calling MessageDigest.getInstance(“SHA256”)

放肆的年华 提交于 2019-12-03 09:36:14

问题


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.

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.

I would prefer not to use a different provider. Is it possible to make it working?


回答1:


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:

  • MD2
  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • 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.

If you change your code to the following, it will work as expected:

final MessageDigest digest = MessageDigest.getInstance("SHA-256");



回答2:


SHA-256 should be the parameter to getInstance()

Link for the list of algorithms supported for message digest



来源:https://stackoverflow.com/questions/12642742/exception-when-calling-messagedigest-getinstancesha256

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!