· 5 years ago · Apr 02, 2020, 04:05 AM
1import javax.crypto.Cipher;
2import javax.crypto.SecretKey;
3import javax.crypto.SecretKeyFactory;
4import javax.crypto.spec.IvParameterSpec;
5import javax.crypto.spec.PBEKeySpec;
6import javax.crypto.spec.SecretKeySpec;
7import java.nio.ByteBuffer;
8import java.nio.charset.StandardCharsets;
9import java.security.Key;
10import java.security.NoSuchAlgorithmException;
11import java.security.spec.InvalidKeySpecException;
12import java.util.Arrays;
13
14public class Main {
15 private static final byte[] DATA = new byte[]{72, -71, 33, -116, 61, 25, -105, 61, 69, -34, 22, -96, 83, -41, 4, 100, 49, -120, -76, -32, -24, 105, -103, 57, -101, -101, 114, 39, 45, -48, -58, 106, -83, 72, -120, -98, 14, 111, -73, 38, -43, -29, -17, -64, -48, -21, -63, -14, 7, -65, -115, 61, -62, -121, 108, -2, 24, 84, -62, 117, 115, 52, -88, 18, 30, 115, 44, -113, -123, 88, 77, -122, 15, -13, 123, 85, -118, -12, -30, 7, 104, -75, -84, -57, -124, -113, -38, 84, 52, 6, -94, 67, 76, 59, 105, 82, 92, -65, -52, -26, -46, 45, 94, 47, 10, -14, -86, -12, 1, 111, -107, -119, -115, 32, -60, -92, 107, -2, 73, 109, -128, -107, -52, -7, -6, 126, 98, -71, -92, 12, -41, 83, -124, 14, -51, -15, 4, -3, -65, -36, 99, 63, 119, 64, 46, 21, 10, 30, -23, -10, -90, -36, -4, -106, -102, 84, -17, 58, 59, -76, -103, -28, -95, 4, 112, 18, 3, -78, 125, -79, 11, 120, -59, -64, -37, -47, 19, -21, 90, -9, -65, 109, 70, -83, -4, 34, 41, -109, 27, -20, 29, 60, 109, -117, 74, -112, -58, 76, 96, 9, -65, 86, 63, 62, 112, -88, 96, -35, 64, 57, 35, 89, -24, -40, 121, 106, -102, -103, -24, -73, 103, -110, 56, 97, -82, 55, -53, -100, 22, -68, 104, 8, 98, -120, -65, -30, 38, 114, -59, 30, 66, -119, 59, -93, 107, -50, 115, 40, 80, 77, -61, -102, -62, -110, -80, -85, 19, 123, -120, 70, -119, 11, 63, 30, 92, 73, 81, -19, -14, 122, -103, -108, 38, -116, -100, 50, -121, -7, -125, 61, -44, -38, -117, 16, 14, -101, 79, -96, 89, 12, 84, -36, 42, -21, -109, -7, 117, 64, 38, 18, -97, -58, 73, 2, 41, 70, -85, 75, 6, 123, 76, -66, 53, -41, 25, -14, -104, -19, 67, -28, -9, -111, 59, -109, 35, 57, 108, 100, 40, 116, -106, -128, 2, 109, -75, 3, 19, 87, -120, 59, -20, -15, 74, -40, 106, -3, -122, 19, -94, 53, -103, -60, -36, 2, 52, 31, 63, 17, -32, -61, -116, 5, 9, 117, -72, -28, -125, 99, -54, -126, 96, 21, 29, 38, 35, 90, -32, 89, 48, 108, 10, -52, -117, 2, -74, -122, -21, 119, 126, -110, -115, 57, -119, -53, 43, -128, 10, 97, 122, 126, -111, 103, 113, 90, 101, 44, 9, 5, 102, 88, -24, -108, -8, 42, 65, 46};
16 private static final byte[] IVS = new byte[]{-114, 123, -36, 36, 6, 2, 31, 116, -76, -125, -62, -61, -41, -121, 82, -106};
17
18 public static void main(String[] args) {
19 Main main=new Main();
20 main.generate("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray());
21
22 }
23 private static String[] str=new String[]{"Uh uh uh! You didn't say the magic word!"};
24 public void generate(char[] charset) {
25 char[] result = new char[charset.length];
26 int[] index = new int[charset.length];
27
28 // initialize the arrays.
29 Arrays.fill(result, 0, result.length, charset[0]);
30 Arrays.fill(index, 0, index.length, 0);
31
32 // loop over the output lengths.
33 for (int length = 1; length <= charset.length; length++) {
34 int updateIndex = 0;
35 do {
36 if (isValid(new String(result, 0, length))){
37 System.out.println(Arrays.toString(decodeMessage(new String(result, 0, length))));
38 System.out.println(new String(result, 0, length));
39 };
40
41 // update values that need to reset.
42 for (updateIndex = length - 1;
43 updateIndex != -1 && ++index[updateIndex] == charset.length;
44 result[updateIndex] = charset[0], index[updateIndex] = 0, updateIndex--)
45 ;
46
47 // update the character that is not resetting, if valid
48 if (updateIndex != -1) result[updateIndex] = charset[index[updateIndex]];
49 }
50 while (updateIndex != -1);
51 }
52 }
53 private static boolean isValid(String string) {
54 try {
55 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
56 PBEKeySpec pBEKeySpec = new PBEKeySpec(string.toCharArray(), "pinch_of_salt".getBytes(StandardCharsets.UTF_8), 65536, 128);
57 SecretKey secretKey = secretKeyFactory.generateSecret(pBEKeySpec);
58 SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
59 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
60 IvParameterSpec ivParameterSpec = new IvParameterSpec(IVS);
61 cipher.init(2, secretKeySpec, ivParameterSpec);
62 byte[] arrby = cipher.doFinal(DATA);
63 return true;
64 } catch (Exception exception) {
65 return false;
66 }
67 }
68 private static String[] decodeMessage(String string) {
69 try {
70 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
71 PBEKeySpec pBEKeySpec = new PBEKeySpec(string.toCharArray(), "pinch_of_salt".getBytes(StandardCharsets.UTF_8), 65536, 128);
72 SecretKey secretKey = secretKeyFactory.generateSecret(pBEKeySpec);
73 SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
74 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
75 IvParameterSpec ivParameterSpec = new IvParameterSpec(IVS);
76 cipher.init(2, secretKeySpec, ivParameterSpec);
77 byte[] arrby = cipher.doFinal(DATA);
78 return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(arrby)).toString().split("\n");
79 } catch (Exception exception) {
80 return new String[]{"Uh uh uh! You didn't say the magic word!"};
81 }
82 }
83}