java MessageDigest NoSuchAlgorithmException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16133881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:00:45  来源:igfitidea点击:

MessageDigest NoSuchAlgorithmException

java

提问by user2304850

I want to use MessageDigestto get a MD5 hash, but I get an error.

我想用来MessageDigest获取 MD5 哈希值,但出现错误。

import java.security.MessageDigest;

public class dn {
  public static void main(String[] args) {
    MessageDigest md = MessageDigest.getInstance("MD5");
  }
}

Error:

错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type NoSuchAlgorithmException
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type NoSuchAlgorithmException

Error is referring to
NoSuchAlgorithmException - if a MessageDigestSpi implementation for the specified algorithm is not available from the specified provider.
found on this site http://docs.oracle.com/javase/6/docs/api/java/security/MessageDigest.htmlunder getInstance

错误是指
NoSuchAlgorithmException - 如果指定算法的 MessageDigestSpi 实现无法从指定提供程序获得。
在这个站点上找到http://docs.oracle.com/javase/6/docs/api/java/security/MessageDigest.htmlgetInstance 下

I have reinstalled the latest java jdk1.7.0_21 and a different version of eclipse, but the error persists. Everything else runs fine on eclipse.

我已经重新安装了最新的 java jdk1.7.0_21 和不同版本的 eclipse,但错误仍然存​​在。其他一切都在 eclipse 上运行良好。

I don't know what else could i do.

我不知道我还能做什么。

回答by JB Nizet

The error message is clear : the code doesn't compile(Unresolved compilation problem) because you're not handling the checked exception NoSuchAlgorithmExceptionthat can be thrown by MessageDigest.getInstance().

该错误信息是明确的:代码不能编译未解决的问题,编译),因为你不处理检查的异常NoSuchAlgorithmException,可以通过抛出MessageDigest.getInstance()

Either add this exception to the throws clause of the main method, or catch it:

将此异常添加到 main 方法的 throws 子句,或捕获它:

public static void main(String[] args) throws NoSuchAlgorithmException {
    ...
}

or

public static void main(String[] args) {
    try {
        ...
    }
    catch (NoSuchAlgorithmException e) {
        System.err.println("I'm sorry, but MD5 is not a valid message digest algorithm");
    }
}

Note that this is a compilationerror. You chose to launch your program despite the presence of compilation errors (visible in the "Problems" view of Eclipse), and despite the fact that Eclipse warned you about that before launching the program. So you tried executing code which doesn't compile, which you shouldn't do.

请注意,这是一个编译错误。尽管存在编译错误(在 Eclipse 的“问题”视图中可见),并且尽管 Eclipse 在启动程序之前警告过您,但您还是选择了启动程序。所以你尝试执行不能编译的代码,这是你不应该做的。

EDIT: fixed the typo in the code in NoSuchAlgorithmException

编辑:修复了 NoSuchAlgorithmException 中代码中的错字

回答by Miguel Prz

You should handle the exception:

您应该处理异常:

try {

   MessageDigest md = MessageDigest.getInstance("MD5");
   //...

} catch(NoSuchAlgorithmException x) {
  // do proper exception handling
}

回答by Anirudha

In addition to other answers here

除了这里的其他答案

Certain algorithms would not be available with some JVM

某些算法不适用于某些 JVM

To make it truly portable application you should do this

要使其成为真正可移植的应用程序,您应该这样做

public boolean isMDAvailable(String s)
{
    boolean success=true;
    try{MessageDigest.GetInstance(s);}
    catch(NoSuchAlgorithmException x)
    {
         success=false;
    }
    return success;
}

Now you can get any available MD algorithm with this method

现在您可以使用此方法获得任何可用的 MD 算法

public MessageDigest getAvailableMessageDigest()
{
    if(isMDAvailable("MD5")==true)return MessageDigest.GetInstance("MD5");
    else if(isMDAvailable("MD2")==true)return MessageDigest.GetInstance("MD2");
    else if(isMDAvailable("SHA-512")==true)return MessageDigest.GetInstance("SHA-512");
    else return null;
}