使用Eclipse编译程序时出现Java错误NoSuchAlgorithmException

时间:2018-05-03 18:05:07

标签: java eclipse forms login windowbuilder

我为登录表单编写了一个代码,用于将密码输入中生成的哈希值与保存的哈希值进行比较。问题是,当我尝试编译它时,由于例外情况,它会给我以下错误:

  

线程中的异常“AWT-EventQueue-0”java.lang.Error:未解决的编译问题:       未处理的异常类型NoSuchAlgorithmException。

我目前正在使用Eclipse for Java EE 4.7.3a和Java SE - 1.8。

这是源代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.security.MessageDigest;

public class Login {

private JFrame frame;
private JPasswordField password;
private JTextField username;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Login window = new Login();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Login() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 338);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblUsername.setBounds(52, 78, 105, 36);
    frame.getContentPane().add(lblUsername);

    password = new JPasswordField();
    password.setBounds(208, 158, 144, 26);
    frame.getContentPane().add(password);

    username = new JTextField();
    username.setBounds(206, 83, 146, 26);
    frame.getContentPane().add(username);
    username.setColumns(10);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblPassword.setBounds(52, 149, 105, 41);
    frame.getContentPane().add(lblPassword);

    JLabel lblLogin = new JLabel("LOGIN");
    lblLogin.setFont(new Font("Yu Gothic Medium", Font.PLAIN, 35));
    lblLogin.setForeground(Color.RED);
    lblLogin.setBounds(147, -15, 117, 112);
    frame.getContentPane().add(lblLogin);

    JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String uname = username.getText();
            String pwd = password.getText();
            String sh = "SAVEDSHA256HASH";
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(pwd.getBytes());
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            String ch = sb.toString();
            if(uname.equals("username") && ch.equals(sh)) {
                JOptionPane.showMessageDialog(frame, "Access Granted");
            } else {
                JOptionPane.showMessageDialog(frame, "Invalid username or password");
            }
            }
        });
    btnLogin.setBounds(147, 221, 117, 29);
    frame.getContentPane().add(btnLogin);
    }
`}

如果您需要进一步的信息,请与我联系

由于

1 个答案:

答案 0 :(得分:1)

MessageDigest.getInstance("SHA-256")

方法抛出NoSuchAlgorithmException。您需要在该语句周围添加try catch块。

                try {
                    md = MessageDigest.getInstance("SHA-256");
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }