Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: password

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default password

    i want to write a code to generat passwords but, a random generator method inside it not changing its value, can i get a solution to this d analternative way to generate password using matric number of the form s60202003

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package key;
     
    /**
     *
     * @author HARLEY
     */
    import java.io.*;
    import java.util.*;
    public class Main {
        public static void main(String[] args) throws IOException {
            File inputFile = new File("cc.txt");
     
            File outputFile = new File("cico.txt");
     
            BufferedReader in = new BufferedReader(new FileReader(inputFile));
     
            BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
     
            String line;
            int jack = 60 ;
            int adme = 1;
            while ((line = in.readLine())!= null) {
     
                //int num = line.hashCode();
                //int namei = Integer.parseInt(line.substring(7));
                Random rand = new Random(100);
                int numt = rand.nextInt(50);
     
                if (line.length()>3) {
     
     
                    adme += 1;
                    Formatter formato = new Formatter();
                int sys = Integer.parseInt(line.substring(7));
                sys = 64 +sys;
               // namei = jack + namei;
                //int js = line.substring(6);
               char form = (char) (sys);
               String hnome = (formato.format("%c",sys).toString());
               String nam = hnome.toLowerCase();
     
                //String suname = name.substring(2);
                //char col = line.charAt(3);
                //int num = line.hashCode();
                //int nnum = Math.abs(num); 
                /**
                Formatter formato = new Formatter();
                String hnome = (formato.format("%d",jack).toString());
                String hname = hnome.substring(0,2);
                String hname2 = hnome.substring(3,5);
                int nonam = line.length();
                String namea = line.substring(0,1);
                out.write(hname+hnome);
                */
               System.out.println(nam+adme+numt);
              out.write("");
              out.newLine();
                }
                   }
            in.close();
            out.close();
        }
    }


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: password

    Hi 5723,

    Welcome to the Java Programming Forums.

    Please refer to the below thread. You can get an idea on it.
    http://www.javaprogrammingforums.com...-password.html


    Cheers,
    Truffy

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: password

    Hey 5723.

    What is cc.txt used for?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: password

    i used the cc.txt as my input file, the password is meant to be generated for matric numbers which is saved in cc.txt and the output be printted to cico.txt

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: password

    Can you give me an example of some 'matric' numbers?

    Also, what kind of output are you looking for? Can you give me an example generated password..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: password

    Not sure if this is correct. Let me know..

    Example input (cc.txt):

    s60202403
    d60202501
    f60202702
    g60202904
    h60202005
    Example output (cico.txt);

    c215
    a315
    b415
    d515
    e615
    import java.io.*;
    import java.util.*;
     
    public class Main {
     
        public static String newLine = System.getProperty("line.separator");
     
        public static void main(String[] args) throws IOException {
     
            File inputFile = new File("cc.txt");
            File outputFile = new File("cico.txt");
     
            BufferedReader in = new BufferedReader(new FileReader(inputFile));
            BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
     
            String line;
            int jack = 60;
            int adme = 1;
     
            while ((line = in.readLine()) != null) {
     
                // int num = line.hashCode();
                // int namei = Integer.parseInt(line.substring(7));
                Random rand = new Random(100);
                int numt = rand.nextInt(50);
     
                if (line.length() > 3) {
     
                    adme += 1;
                    Formatter formato = new Formatter();
                    int sys = Integer.parseInt(line.substring(7));
                    sys = 64 + sys;
                    // namei = jack + namei;
                    // int js = line.substring(6);
                    char form = (char) (sys);
                    String hnome = (formato.format("%c", sys).toString());
                    String nam = hnome.toLowerCase();
     
                    // String suname = name.substring(2);
                    // char col = line.charAt(3);
                    // int num = line.hashCode();
                    // int nnum = Math.abs(num);
                    /**
                     * Formatter formato = new Formatter(); String hnome =
                     * (formato.format("%d",jack).toString()); String hname =
                     * hnome.substring(0,2); String hname2 = hnome.substring(3,5);
                     * int nonam = line.length(); String namea =
                     * line.substring(0,1); out.write(hname+hnome);
                     */
                    String pass = nam + adme + numt;
                    System.out.println(pass);
     
                    out.write(pass);
                    out.write(newLine);
                }
            }
            in.close();
            out.close();
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. The Following User Says Thank You to JavaPF For This Useful Post:

    5723 (July 1st, 2009)

  8. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: password

    all matric numbers start with s and its length is 9, but i want a more random password generator, thanks.

  9. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: password

    I'm having trouble understanding what exactly you are after, that might just be me because I'm simple. However I would really appreciate it if you could exaplain the problem clearly, post any sample input, explain the process to taken to get to the output. And post any output examples (disered output that is) so that I can have a good look for you.

    Thanks,
    Chris

  10. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: password

    I'm currently working on a little password generation class with built in password strength checker. I will post it here once it is done. Probably tomorrow.

    // Json

  11. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Lightbulb Re: password

    I believe I've finished my password generator and here we go.

    To test it out
        public static void main(final String... arguments) {
            final char[] password = Password.generate(8);
     
            System.out.println("Password: " + String.valueOf(password));
            System.out.println("Strength: " + Password.strength(String.valueOf(password)) + "%");
        }

    This should print out the password and the strength of it.


    Password.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    /**
     * Password generator created for the members of Java Programming Forums. Feel free
     * to use this in your own applications. Please give credit where credit is due.
     *
     * @author Daniel Johansson
     * @since 9 Jul 2009
     */
    public class Password {
     
        private static final String ALPHA_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
     
        private static final String NUMBERS = "0123456789";
     
        private static final String SYMBOLS = "$%&*@#!_:~";
     
        /**
         * Since this is a utility class there is no need to instantiate it. All statics baby!
         */
        protected Password() {
            throw new UnsupportedOperationException();
        }
     
        /**
         * Generates a random password of the specified length using uppercase letters, lowercase letters, digits and symbols.
         *
         * @param length the length of the password.
         * @return character array representing the password
         */
        public static char[] generate(final int length) {
            final String allowedCharaters = ALPHA_CHARACTERS + NUMBERS + SYMBOLS;
            final Random random = new Random();
            final char[] password = new char[length];
     
            for (int i = 0; i < length; ++i) {
                password[i] = allowedCharaters.charAt(random.nextInt(allowedCharaters.length() - 1));
            }
     
            return password;
        }
     
        /**
         * Calculates the strength of the password based on certain factors such as.
         * <ul>
         * <li>Length</li
         * <li>Number of upper case letters</li>
         * <li>Number of lower case letters</li>
         * <li>Number of digits</li>
         * <li>Number of digits or symbols in the middle</li>
         * <li>Any repeating characters</li>
         * <li>Sequences</li>
         *
         * It also adds bonuses if the password is 8 characters or more and it has a certain combination of lowercase,
         * uppercase, digits and symbols.
         *
         * @param password the password to check the strength of
         * @return the strength which is a value between zero and one hundred (0 - 100).
         */
        public static int strength(final String password) {
            int strength = 0;
     
            // Length
            strength += (password.length() * 4);
     
            // Upper case letters
            final int upperCase = matches(password, "[A-Z]");
     
            if (upperCase > 0) {
                strength += ((password.length() - upperCase) * 2);
            }
     
            // Lower case letters
            final int lowerCase = matches(password, "[a-z]");
     
            if (lowerCase > 0) {
                strength += ((password.length() - lowerCase) * 2);
            }
     
            // Numbers
            final int numbers = matches(password, "[0-9]");
     
            if (numbers > 0) {
                strength += (numbers * 4);
            }
     
            if (numbers == password.length()) {
                strength -= numbers;
            }
     
            // Sepcial characters
            final int specialCharacters = matches(password, "[:,!,@,#,$,%,^,&,*,?,_,~]");
     
            if (specialCharacters > 0) {
                strength += (specialCharacters * 6);
            }
     
            // Letters only
            final int letters = matches(password, "[a-z|A-Z]");
     
            if (letters == password.length()) {
                strength -= letters;
            }
     
            // Middle numbers
            int middles = 0;
     
            for (int j = 0; j < NUMBERS.length(); ++j) {
                if ((password.indexOf(NUMBERS.charAt(j)) > 0) && (password.indexOf(NUMBERS.charAt(j)) < password.length() - 1)) {
                    ++middles;
                }
            }
     
            // Middle symbols
            for (int j = 0; j < SYMBOLS.length(); ++j) {
                if ((password.indexOf(SYMBOLS.charAt(j)) > 0) && (password.indexOf(SYMBOLS.charAt(j)) < password.length())) {
                    ++middles;
                }
            }
     
            strength += (middles * 2);
     
            // Repeat charaters
            final List<Character> done = new ArrayList<Character>();
            int repeats = 0;
     
            for (int i = 0; i < password.length(); ++i) {
                int matches = 0;
     
                for (int j = 0; j < password.length(); ++j) {
                    if (password.toLowerCase().charAt(i) == password.toLowerCase().charAt(j) && !done.contains(password.toLowerCase().charAt(i))) {
                        ++matches;
                    }
                }
     
                if (matches > 1) {
                    strength -= (matches * (matches - 1));
                    repeats += (matches * (matches - 1));
                    done.add(password.toLowerCase().charAt(i));
                }
            }
     
            // Consequtive upper case letters
            final int consequtiveUpperCaseLetters = matches(password, "[A-Z]{2}");
            strength -= (consequtiveUpperCaseLetters * 2);
     
            // Consequtive lower case letters
            final int consequtiveLowerCaseLetters = matches(password, "[a-z]{2}");
            strength -= (consequtiveLowerCaseLetters * 2);
     
            // Consequtive numbers
            final int consequtiveNumbers = matches(password, "[0-9]{2}");
            strength -= (consequtiveNumbers * 2);
     
            // Sequential characters
            for (int i = 0; i < password.length(); ++i) {
                final int index = ALPHA_CHARACTERS.indexOf(String.valueOf(password.charAt(i)).toLowerCase());
     
                if (index != -1 && index < ALPHA_CHARACTERS.length() - 3) {
                    final String forwardSequence = ALPHA_CHARACTERS.substring(index, index + 3);
                    final String reverseSequence = new StringBuffer(forwardSequence).reverse().toString();
     
                    if (password.indexOf(forwardSequence) != -1 || password.indexOf(reverseSequence) != -1) {
                        strength -= 3;
                    }
                }
            }
     
            // Sequential numbers
            for (int i = 0; i < password.length(); ++i) {
                final int index = NUMBERS.indexOf(String.valueOf(password.charAt(i)).toLowerCase());
     
                if (index != -1 && index < NUMBERS.length() - 3) {
                    final String forwardSequence = NUMBERS.substring(index, index + 3);
                    final String reverseSequence = new StringBuffer(forwardSequence).reverse().toString();
     
                    if (password.indexOf(forwardSequence) != -1 || password.indexOf(reverseSequence) != -1) {
                        strength -= 3;
                    }
                }
            }
     
            // Bonuses
            int bonus = 0;
     
            if (password.length() >= 8) {
                ++bonus;
     
                if (((double) upperCase / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
     
                if (((double) lowerCase / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
     
                if (((double) numbers / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
                if (((double) specialCharacters / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
            }
     
            strength += (bonus * 2);
     
            if (strength < 0) {
                strength = 0;
            } else if (strength > 100) {
                strength = 100;
            }
     
            return strength;
        }
     
        private static int matches(final String string, final String regexPattern) {
            int matches = 0;
            final Pattern pattern = Pattern.compile(regexPattern);
            final Matcher matcher = pattern.matcher(string);
     
            while (matcher.find()) {
                ++matches;
            }
     
            return matches;
        }
     
    }

    Above is the actual password class, it contains some weird and freaky stuff to check the strength but the actual generate method is very simple. If you have any questions let me know and I shall try to answer them.

    // Json

Similar Threads

  1. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM
  2. Java password generator program to generate 8 random integers
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 16th, 2009, 07:49 AM