A blog for technology, SEO tips, website development and open source programming.

PasswordGenerator with Java

0 725

This post indicates how you can generate passwords with JAVA swing. Nowadays we need more secure passwords with numbers, letters and special characters. The end result of this post is shown below.

passwordgenerator

Steps:

private JFrame frame;
private JButton generateButton;
private JTextField passwordField;
private JSpinner passwordLengthSpinner;
private JLabel passwordLabel;
private JLabel spinnerLabel;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel mainPanel;
private String password = "";//the string that the password will be generated
private int rndNumber;
private JCheckBox specialSignCheckBox;


//the characters array for password generation
private String charPool[] = {
"a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z", "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "0", "1",
"2", "3", "4", "5", "6", "7", "8", "9",
"°", "^", "!", "§", "$", "%", "&", "/",
"(", ")", "=", "?", "`", "´", "{", "}",
"[", "]", "*", "-", "+", ",", "<", ">",
"|", "_", ";", "#", "µ", "~", ".", ":",
"'"
};

public class GenerateButtonListener implements ActionListener {

public void actionPerformed(ActionEvent ev) {
//Code that generates according to JSpinner Setting
int spinnerValue = Integer.parseInt(passwordLengthSpinner.getValue().toString());
passwordField.setText(getPassword(spinnerValue));
}
}
public String getPassword(int spinnerValue) {
password = “”;
for (int i = 0; i < spinnerValue; i++) {
if (!specialSignCheckBox.isSelected()) {
rndNumber = (int) (Math.random() * 72);
password = password.concat(charPool[rndNumber]);
} else {
rndNumber = (int) (Math.random() * 105);
password = password.concat(charPool[rndNumber]);
}
}
return password;
}

public static void main(String[] args) {
new PasswordGenerator().setupGui();
}

public void setupGui() {
frame = new JFrame(“Password Generator”);
frame.setBackground(Color.BLUE);

//Panel two
panelTwo = new JPanel();
panelTwo.setBackground(Color.WHITE);
passwordLabel = new JLabel(“Password”);
passwordField = new JTextField(22);
specialSignCheckBox = new JCheckBox(“Add Special Signs to enforce password (/,=?`´}[]*-+<>|_;#µ~.:’)”);//the special characters which make the password stronger
passwordField.setEditable(false);
panelTwo.add(passwordLabel);
panelTwo.add(passwordField);
panelTwo.add(specialSignCheckBox);
//Panel two

//Main Panel
mainPanel = new JPanel();
mainPanel.add(panelOne);
mainPanel.add(panelTwo);
//Main Panel

//Frame Config
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.pack();
//Frame Config
}
}

Download the JAR file PasswordGenerator

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More