vrijdag 21 februari 2014

Creating Random Strings for password or encryption key

This program can be used to generate random strings. 

If you need a new safe password, or an encryption key.

Example output of the code:

Password_0  4FW5C(C5OH2YVNRYK!1G=L!2C)E9C1X=48^I)@%SCI#-1R!NJ=3=COWY&S))HSZF
Password_1  0VA+%(3HP#EANUZ=+1CF^!KY#J)0S1^Z=TV%V&VDYN+4)L4WOMM#%F0RMUJLW0YW
Password_2  FAK^H@D#TZ+SSL+R@37UZA@=K#D-HT(72PT5D!LOC7Y83G5M%TDL4Q+!X&Q!Z0NM
Password_3  4V9G4DB_#YNA9CC@2BHR!JM-H6D8B8Z3B_Q)LW@CNC-BJR350MR&EP=G(0S4R7XY
Password_4  X-31O#@#CU6#BITQF%F4OX6-NN6D+@0PU_(9!)-+&8EM7MY5C%X2&D58(OAEOR^Y
Password_5  N(VIG02=)5W(&P78UAL#8YW-+A^HS@RGW+0+U7G_35YB=^C(RFE1U@#+Y%045@3O
Password_6  96)2^XY((HP&EXNSKL%UWNNG!@1O0T)DHTWTF7DURLP8YC#^WV##&GF4(Y+0V@YZ
Password_7  ^3GIB7N34)&HI)H5X%N)!X_#CJ0%=9A-=8R)+&#&HV@4GO(+SXW^FF8((5AKACJJ
Password_8  #_(0T=&V7DNMYV)0@X#H6U6(T4_VX1Y@UK36SU#3Q(JFRJ+--L0EPJROY&NN+DAO
Password_9  0=VT)%EXI=83GN9R-I)PGG&S5@LFM2VWA3%Z(I8%MT1^ZVP_)7=+#BCP34VDD1&I


---------------------------------------------------------------------------------------

package nl.yokoz.string.random;

import java.util.Random;

public class MaxRandomString {

    static private final String charsBase;
    private static Random randomizer;
    private static int charsBaseLength;

    static {
        charsBase = "0123456789-_=^%#@!&()+ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        randomizer = new Random();
        charsBaseLength = charsBase.length();
    }

    public static String get(int desiredLength) {
        StringBuilder result = new StringBuilder(desiredLength);
        for (int i = 0; i < desiredLength; i++) {
            result.append(charsBase.charAt(randomizer.nextInt(charsBaseLength)));
        }
        return result.toString();
    }

    public static void main(String[] args) {
        // generate 10 random passwords........
        getPasswords(10, 64, false);
    }

    private static void getPasswords(int nPasswordsWanted, 

                                                    int passWordLength, 
                                                    boolean bShowValueOnly) {
        String passwordNr = "";
        for (int i = 0; i < nPasswordsWanted; i++) {
            if (!bShowValueOnly) {
                passwordNr = "Password_" + i + "  ";
            }
            System.out.println(passwordNr + get(passWordLength));
        }
    }

}