调用MessageDigest.getInstance(“ SHA256”)时发生异常 [英] Exception when calling MessageDigest.getInstance("SHA256")

查看:1599
本文介绍了调用MessageDigest.getInstance(“ SHA256”)时发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Android上运行良好的代码。当我将其移植到具有JRE 1.6的Windows 64位计算机上时,代码不起作用。

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")

我得到以下异常:


java.security.NoSuchAlgorithmException:SHA256 MessageDigest在星期日不可用.security.jca.GetInstance.getInstance(未知源)
在java.security.Security.getImpl(未知源)在
java.security.MessageDigest.getInstance(未知源)

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)

我在互联网上发现有人声称可以将SHA256与Sun JRE附带的标准加密提供程序一起使用,而有人说我需要使用另一个提供程序例如,Bouncy Castle。

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?

推荐答案

对于可用于JCA服务的算法有疑问时,您的第一个端口应该是JCA 标准算法名称文档 MessageDigest 保证支持算法符合JCA的JVM中的服务是:

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

  • MD2
  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

提供者通常会为这些算法提供别名,这就是为什么它可能与Bouncy Castle一起工作的原因,但是如果可以最大限度地提高可移植性,则应坚持使用这些别名。

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");

这篇关于调用MessageDigest.getInstance(“ SHA256”)时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆