Saturday 9 February 2019

JAVA : How to generate password


//JAVA source code to generate random password
PasswordExample.java
package java8s;

import java.util.*;
public class PasswordExample {

     public static void main(String[] args) {
          
           int length = 10;
        System.out.println(silanPassword(length));
          

     }
    
     static char[] silanPassword(int len)
    {
        System.out.print("Your new password is : ");
 
       
        String uchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lchars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*_=+-/.?<>)";
 
 
        String values = uchars + lchars +
                        numbers + symbols;
 
        
        Random rd = new Random();
 
        char[] password = new char[len];
 
        for (int i = 0; i < len; i++)
        {
           
            password[i] =
              values.charAt(rd.nextInt(values.length()));
 
        }
        return password;
    }

}
Output:
Your new password is : mR!XPdfVmg

Note : Here we have used random() to generate the password. It may change every time due to use of random() method.

Thanks & Regards:'
Trilochan Tarai
SILAN Technology & java8s.com
0674-2361252

No comments:

Post a Comment