簡體   English   中英

使用Eclipse編譯程序時出現Java錯誤NoSuchAlgorithmException

[英]Java error NoSuchAlgorithmException while compiling program with Eclipse

我已經為登錄表單編寫了代碼,該代碼將密碼輸入中生成的哈希與保存的哈希進行比較。 問題是當我嘗試編譯它時,由於異常,它給了我以下錯誤:

線程“ AWT-EventQueue-0”中的異常java.lang.Error:未解決的編譯問題:未處理的異常類型NoSuchAlgorithmException。

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

這是源代碼:

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);
    }
`}

如果您需要更多信息,請與我聯系

謝謝

MessageDigest.getInstance("SHA-256")

方法拋出NoSuchAlgorithmException 您需要在該語句周圍添加try catch塊。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM