⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authentication.java

📁 sifi-0.1.6.tar.gz 出自http://www.ifi.unizh.ch/ikm/SINUS/firewall/ 一个linux的防火墙工具。
💻 JAVA
字号:
/* ----------------------------------------------------------------------   The SINUS Firewall -- a TCP/IP packet filter for Linux   Written within the SINUS project at the University of Zurich,   SWITCH, Telekurs Payserv AG, ETH Zurich.   originally based on the sf Firewall Software (C) 1996 by Robert   Muchsel and Roland Schmid.   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   SINUS Firewall resources:   SINUS Homepage: http://www.ifi.unizh.ch/ikm/SINUS/   Firewall Homepage: http://www.ifi.unizh.ch/ikm/SINUS/firewall.html   Frequently asked questions: http://www.ifi.unizh.ch/ikm/SINUS/sf_faq.html   Mailing list for comments, questions, bug reports: firewall@ifi.unizh.ch   ----------------------------------------------------------------------  */package sfclasses;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import java.security.*;import com.sun.java.swing.*;public class Authentication extends JDialog implements ActionListener {  /**   * Display dialog for authentication on a firewall.	 * The user has to enter a login, his password and the actual keynumber.	 * The MD5 key is calculated and authentication is performed on	 * the firewall.	 * @param parent Parent frame	 * @param hostname Hostname to authenticate on a firewall    */	public Authentication(Frame parent, String hostname) {		super(parent, "Authentication Required", true);		// create Authentification Dialog		//setResizable(false);		GridBagLayout gbl = new GridBagLayout();		getContentPane().setLayout(gbl);		getContentPane().setSize(400, 300);		Panel p1 = new Panel();		Font title = new Font("Helvetica", Font.BOLD, 12);		p1.setFont(title);		p1.add(new Label("Enter data for authentication on "+ hostname));		JLabel l1 = new JLabel("Login:");		JLabel l2 = new JLabel("Password:");		JLabel l3 = new JLabel("Key Number:");		login = new JTextField(MAX_LOGIN_LENGTH);		password = new JPasswordField(20);		keynum = new JTextField(5);				Panel p2 = new Panel();		GridLayout gl2 = new GridLayout(3, 2);		p2.setLayout(gl2);				p2.add(l1);		p2.add(login);		p2.add(l2);		p2.add(password);		p2.add(l3);		p2.add(keynum);				JButton okButton = new JButton("OK");		okButton.addActionListener(this);		JButton cancelButton = new JButton("Cancel");		cancelButton.addActionListener(this);		Panel p3 = new Panel();		p3.add(okButton);		p3.add(cancelButton);		GridBagConstraints gbc = new GridBagConstraints();		gbc.gridx = 1;		gbc.gridy = 1;		gbc.fill = GridBagConstraints.NONE;		gbc.anchor = GridBagConstraints.CENTER;		gbl.setConstraints(p1, gbc);		gbc.gridy = 3;		gbl.setConstraints(p3, gbc);		gbc.gridy = 2;		gbc.insets = new Insets(10, 50, 10, 50);		gbc.weightx = 0.0;		gbl.setConstraints(p2, gbc);		getContentPane().add(p1);		getContentPane().add(p2);		getContentPane().add(p3);		pack();		}	/**	 * Display the authentication dialog	 */	public void display() {		setVisible(true);		return;	}	/**	 * Get the calculated key needed for authentication.	 * @return The key to pass to the firewall	 */	public String getKey() {		return key;	}	/**	 * Get the login for authentication.	 * @return The login to pass to the firewall	 */	public String getLogin() {		return login.getText();	}	/**	 * Actionlistener	 */	public void actionPerformed(ActionEvent ae) {			String what = ae.getActionCommand();		if (what == "Cancel") {			dispose();			login = null;			return;		}		if (what == "OK") {		key = calcKey(password.getText(), keynum.getText());			if (key.length() == 0) {				UserDialog.ErrorBox("Invalid Key Number");				return;			}			dispose();			return;		}	}		/**	 * Calculates the actual key based on the password. This is done	 * by calculating several MD5 hashes. Each time, a hash has been	 * calculated, the resulting byte array is converted into a string	 * representing the array in hex. This String is the basis for	 * further MD5 calculations. Calculation is performed keynum times.	 * @param password Password as basis for the calculation	 * @param keynum Number of key to generate	 * @return Key for authentication on the firewall	 * @see toHexString	 */	public static String calcKey(String password, String keynum) {		byte[] input;		byte[] output;		int i;		MessageDigest msgdig;		String pw = new String(password);		String key = new String();		input = pw.getBytes();		try {			i = Integer.parseInt(keynum);		} catch(NumberFormatException e) {		System.out.println("Error Number Conversion");		return key;		}			try {			msgdig = MessageDigest.getInstance("MD5");		} catch(NoSuchAlgorithmException e) {		System.out.println("No Algorithm Found");		return key;		}		while (i > 0) {			msgdig.update(input);			output = msgdig.digest();			key = toHexString(output);			input = key.getBytes();			i--;		}		return key;	} // calcKey	/**	 * Converts a 16 byte unsigned integer to a hex string.	 * That hex string is used to perform further MD5 hashes.	 * @param digest Byte array containing the message digest	 * @return A String representing the byte array in hex format	 */	public static String toHexString(byte[] digest) {		int i;		String tmpstr;		String hexdigest;		hexdigest = "";		for (i=0; i<16; i++) {			tmpstr = (Integer.toHexString(digest[i] & 0xff));			if ((digest[i] & 0xf0) != 0)				hexdigest += tmpstr;			else				hexdigest += ("0"+tmpstr);		}		return hexdigest;	} // toHexString	// protected data	protected static final int MAX_LOGIN_LENGTH = 20; // max. login size	// private data	private Frame parent;	private String key; 					// generated key to be returned	private JTextField login;	private JPasswordField password;	private JTextField keynum;		} // class Authentication

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -