skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 4, 2012

Password Generation in Java

Posted by Admin at 11:13 AM – 0 comments
 
The input given for generating the password is combined with the current system time and the random number generator is initialized.
The random number is got from the random number generator and the no. of random numbers depends on the password length given.
The character array with alpha numeric is initialized and the random number generated will be used as the index to get the characters from the array which frames the password.
import java.util.*;
public class passwdgeneration
{
  private static java.util.Random rd = new java.util.Random();
     private static char charBase[] = {'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'};
     public static void main(String args[])
 {
  String str = args[0];
  int inPassLength = Integer.parseInt(args[1]);
  byte[] buffer = new byte[inPassLength];
  int curCount = 0;
  long testTmp=(System.currentTimeMillis()+str.hashCode());
             rd.setSeed(testTmp);
  for( int count = 0; curCount<inPassLength; ++count) 
  {
                 int thisRandom = rd.nextInt(31);
                 buffer[curCount++]=(byte)charBase[thisRandom];
             }
             String stPass = new String(buffer);
         System.out.println("Password "+stPass);
  }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger