Support Forums

Full Version: Password Generator [Version 2]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well it seems the other programmers in some other sections have been creating password generators in different languages, so I thought I would take a swing at it and make something simple out of it. I may improve it or work off some of your ideas.

Anyhow, its a simple GUI that has 3 textfields, which creates/writes to a Passwords.txt file. I may make it load in XML just for the fun of it later.

[Image: 346357.png]

http://uppit.com/v/845HLDTA

Virus scan if you wish, but its clean. Smile
Thanks.

Src
Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/*
* GUI.java
*
* Created on 6-Nov-2009, 9:29:37 PM
*
* @author Anthony Calandra
*/
public class PasswordGen extends javax.swing.JFrame {

    public PasswordGen() {
        initComponents();
    }

    public static void main(String[] args) {
        new PasswordGen().setVisible(true);
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Password Utility Panel");

        jLabel2.setText("Desired Username:");

        jLabel3.setText("Desired Password:");

        jLabel4.setText("Login for:");

        jTextField3.setText("example: Google sign-in.");

        jButton1.setText("Save to Text File");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Close");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setText("Clear Fields");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Password (Number) Randomizer");

        jLabel6.setText("Enter a password:");

        jButton4.setText("Randomize");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel7.setText("Randomized Password:");

        jTextField5.setEditable(false);

        jButton5.setText("Clear Randomizer");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Get Randomized Password");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton6))
                        .addGap(25, 25, 25))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(138, 138, 138)
                .addComponent(jLabel1)
                .addContainerGap(157, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(133, 133, 133)
                .addComponent(jLabel5)
                .addContainerGap(88, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(184, 184, 184)
                .addComponent(jButton3)
                .addContainerGap(203, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(15, 15, 15)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    Random randomGen = new Random();
    private String randomizeInt() {
        int toInt = Integer.parseInt(jTextField4.getText());
        int randomIndex = randomGen.nextInt(toInt);
        String randomString = Integer.toString(randomIndex);
        return randomString;
    }

    /* private char randomizeChar() {
    String randomString = jTextField4.getText();
    char toChar = (char) randomString.lastIndexOf(randomString.length());
    return (char) (randomGen.nextInt(toChar));
    } */

    private void createFile() {
        try {
            File f = new File("Passwords.txt");
            f.createNewFile();
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            bw.write("--- " + jTextField1.getText() + " Login information ---");
            bw.newLine();
            bw.write("Username: " + jTextField2.getText() + "");
            bw.newLine();
            bw.write("Password: " + jTextField3.getText() + "");
            bw.newLine();
            bw.newLine();
            bw.close();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
           createFile();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
}                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        System.exit(0);
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextField5.setText(randomizeInt());
    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextField5.setText("");
    }

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextField2.setText(jTextField5.getText());
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;

}

As you can see, I used NetBeans to create the GUI, since I can never be bothered to do it myself.. >.>
Yeah, we have been making a bunch of password generators Big Grin

But I have a question: Does it actually generate passwords? Because I see there is a text field for "Desired Password". Or is it just a utility that can save your user name and password in case you forget them?

Good job, though. It makes a nice, simple project.
Thanks for the comment, unfortunately I havnt got to the random generations yet, but I will add it very soon. So for now, its a utility.
Right. Thanks for the confirmation.
Done version 2 I guess, trying to figure out character randomization, and for now im using the Random API for randomizing the numbers. According to the results, the amount of numbers being randomized depend on the desired passwords input. So thats a bug fix needed.

Soon to add XML.
Nice job. It looks promising.

Maybe adjust the layout of the buttons and text fields a bit?
Sure thing, I will in V3. Smile
(11-07-2009, 12:26 PM)Project Evolution Wrote: [ -> ]Done version 2 I guess, trying to figure out character randomization, and for now im using the Random API for randomizing the numbers.

Think about it... what's a simple way of having numbers correspond to any element? If you guessed array, you're right.

You could set up an array of 26 characters, and then initialize them with a for loop. shouldn't take more than three lines of code.

Code:
char letters[26];
for(int i = 0; i < 26; i++) {
     letter[i] =(char) i + 97; //ascii key codes, 'a' is 97.
}

the iffy part here is the type-casting, I think java allows casting from an int to a char (I mean, why wouldn't they), but if not, I'm sure there's a method for turning an int into a char.

from there, you can get random characters by using pseudorandom elements in the array.

i.e...
Code:
char nextChar = letters[Random_generator.nextInt(25)]; //probably wrong syntax, but you know what I mean...
password.append(letter);
Uhm thanks? But im pretty sure I have the mental capacity to figure it out. ;)
Also, I have to figure out that Number randomization, I will get to it when I have time.
Once I go through this tutorial I'm walking myself through right now, hopefully I'll be able to make GUI's like this... idk D: