Saturday, October 31, 2015

DiceWare in Java

Java Implementation of the same algorithm you can also see here as a C++ solution.  The input file can be used from any dictionary as long as it is in the format of "index word"  tab delimited file, where the index is a 5 digit value in the range of [1-6] for each digit.

import java.io.*;         // Needed for the File and IOException
import java.util.Random;
/**
 * This application was designed after an elev year old girl who appeared in the news starting her        * business  generating passwords for $2.  http://www.welivesecurity.com/2015/10/29/11-year-old-sets-cryptographically-secure-password-business/
* Dictionary used in this example was generated from - http://world.std.com/~reinhold/diceware.wordlist.asc
 *
 * @author Zoltan Szabo
 * @version 1.0.0
 */

public class DiceWare
{
public static void main(String[] args)throws IOException,InterruptedException{
    // Open the file.
      File file = new File("t:\\dict.txt");
      RandomAccessFile inputFile = new RandomAccessFile(file,"r");

    int index,value;
    final int NUM_WORDS = 6, DIE_SIDES=6;
    String temp="",word="",password="";
    Random rand=new Random();
    //generate random ASCII special character to add to the end of the password
    char addChar = (char)(rand.nextInt(15) + 33); 
    String[] myTuples=null;
    for (int i = 0; i < NUM_WORDS; i++){
        do{
            value = (rand.nextInt(DIE_SIDES)+1)*(int)Math.pow(10, 4)
                  + (rand.nextInt(DIE_SIDES)+ 1)*(int)Math.pow(10, 3)
                  + (rand.nextInt(DIE_SIDES)+ 1)*(int)Math.pow(10, 2)
                  + (rand.nextInt(DIE_SIDES)+ 1)*(int)Math.pow(10, 1)
                  + (rand.nextInt(DIE_SIDES)) + 1;
            temp=inputFile.readLine();
            if(temp!=null) myTuples=temp.split("\t");
            index = new Integer(myTuples[0]).intValue();
            if(index!=-1)word = myTuples[1];
            if (index == value){
                break;                 //stop looking if the generated value is found
            }//end of if value found
           
        } while (word!= null);//end of while reading the file
        inputFile.seek(0);    //reset the file to its beginning for the new search
        password+=word;
        if (i == 4){password += addChar; break;}
        password += '/';
        Thread.sleep(1000);                         //needs to sleep for a second to catch up with file operations
    }//end of for loop of generating 5 random words

    System.out.println("Your generated password is: " +password );
    inputFile.close();
}
}

No comments:

Post a Comment