· 7 years ago · Mar 15, 2018, 03:28 PM
1 private String createPuzzle() throws Exception {
2 //Generate the required Byte arrays
3 byte[] prefix = get16ZeroBytes();
4 byte[] puzzleNumber = getPuzzleNumber();
5 byte[] bytekey = getRandom8Bytes();
6 byte[] desKey = getDESByteArray();
7
8 //Write all arrays to a ByteArrayOutputStream so they can be appended.
9 ByteArrayOutputStream oStream = new ByteArrayOutputStream();
10 oStream.write(prefix);
11 oStream.write(puzzleNumber);
12 oStream.write(bytekey);
13
14 //Create the puzzle byte array via the ByteArrayOutputStream
15 byte[] puzzle = oStream.toByteArray();
16
17// Check the size is correct
18// System.out.println("");
19// System.out.println("Length is " + String.valueOf(puzzle.length) + " bytes.");
20
21// Debug Info
22// printByteArray(prefix);
23// printByteArray(puzzleNumber);
24// printByteArray(bytekey);
25// printByteArray(getDESByteArray());
26
27
28 //Change byte arrays to strings so that we can encrypt.
29 StringBuilder stringBuilder = new StringBuilder();
30 String prefixString = Base64.getEncoder().encodeToString(prefix);
31 String puzzleNumberString = Base64.getEncoder().encodeToString(puzzleNumber);
32 String byteKeyString = Base64.getEncoder().encodeToString(bytekey);
33 stringBuilder.append(prefixString);
34 stringBuilder.append(puzzleNumberString);
35 stringBuilder.append(byteKeyString);
36
37 //Define plaintext and Key
38 String plaintext = stringBuilder.toString();
39 SecretKey DES_KEY = CryptoLib.createKey(desKey);
40
41 //Encrypt plaintext with key
42 DES desEncryptor = new DES();
43 String encryptedPuzzle = desEncryptor.encrypt(plaintext, DES_KEY);
44
45// System.out.println("ENCRYPTED PUZZLE: " + encryptedPuzzle);
46
47 return encryptedPuzzle;
48 }
49
50
51public static void main(String[] args) throws Exception {
52
53 PuzzleMaker pm = new PuzzleMaker();
54
55 ArrayList<String> puzzleArrayList = new ArrayList<>();
56 for(int i = 0; i < 4096; i++){
57 String newPuzzle = pm.createPuzzle();
58 puzzleArrayList.add(newPuzzle);
59 System.out.println(String.valueOf(i) + ":" + newPuzzle);
60 }
61
62
63
64 }