· 9 years ago · Dec 05, 2016, 02:04 PM
1import java.security.SecureRandom;
2import java.security.spec.AlgorithmParameterSpec;
3import java.util.Arrays;
4import javax.crypto.Cipher;
5import javax.crypto.SecretKey;
6import javax.crypto.spec.IvParameterSpec;
7import javax.crypto.spec.SecretKeySpec;
8import sun.misc.BASE64Decoder;
9
10public class AESModel {
11
12 public static void main(String[] args) throws Exception {
13 doAES();
14 doAES1();
15 }
16
17 /**
18 * Шифрование AES.
19 */
20 private static void doAES() throws Exception {
21 String ct = "Y5JAfKCpb5mjw0ShdOEWhXAlQNdbxOBOqnqHt7kskPuswrw5LYG9C8Yab3d4m2SR";
22 String initVector = "9m/faAg9yXIQPiGlZE/0CA==";
23 String key = "r+H5ZvgM4cWJDOyyK5uXsw==";
24
25 BASE64Decoder decoder = new BASE64Decoder();
26
27 byte[] ctBytes = decoder.decodeBuffer(ct);
28 byte[] initVectorBytes = decoder.decodeBuffer(initVector);
29 byte[] keyBytes = decoder.decodeBuffer(key);
30
31 SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
32 Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
33 cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(initVectorBytes));
34
35 byte[] openTextBytes = cipher.doFinal(ctBytes);
36 String openText = new String(openTextBytes);
37 System.out.println(openText);
38 }
39
40 /**
41 * Перебор AES.
42 */
43 private static void doAES1() throws Exception {
44 //final String ct = "Jg7QI1RrVRYNjREM2+KUnifcnQgFhv0gGQ41yquM1+0HAPdK+s/FfUAo";
45 final String ct = "bB0jXIKsr4F5Ir8LtvM8sWTBF5KHsn5zBkA3yW9zgg==";
46 //final String initVector = "b2Y+/uy/M6dtShDAGQaYzg==";
47 final String initVector = "jm/QEdqKgJ5WbE8gia9sjA==";
48
49 // нужно найти 7-й, 8-й байты
50 //final int i0 = 6;
51 //final int j0 = 7;
52 //
53 // нужно найти 5-й, 6-й байты
54 final int i0 = 4;
55 final int j0 = 5;
56
57 //final byte[] keyBytes = {-127, -82, -63, 75, 37, -34, 0, 0, -60, 94, 42, 101, 16, -65, 100, 101};
58 final byte[] keyBytes = {-80, -9, 83, 31, 0, 0, 61, -41, -33, 39, 46, 20, -48, -92, -44, 51};
59
60 BASE64Decoder decoder = new BASE64Decoder();
61
62 byte[] ctBytes = decoder.decodeBuffer(ct);
63 byte[] initVectorBytes = decoder.decodeBuffer(initVector);
64 AlgorithmParameterSpec iv = new IvParameterSpec(initVectorBytes);
65
66 Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
67 for (int i = -128; i < 128; i++) {
68 for (int j = -128; j < 128; j++) {
69 //keyBytes[i0] = (byte) i;
70 //keyBytes[j0] = (byte) j;
71 keyBytes[i0] = (byte) i;
72 keyBytes[j0] = (byte) j;
73
74 SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
75 cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
76
77 byte[] openTextBytes = cipher.doFinal(ctBytes);
78 String openText = new String(openTextBytes);
79 long count = openText.chars().filter(Character::isLetter).count();
80
81 // в Ñлучае Ð½Ð°Ñ…Ð¾Ð¶Ð´ÐµÐ½Ð¸Ñ Ð½ÐµÑкольких вариантов уменьшить коÑффициент
82 // или выбрать оÑмыÑленный текÑÑ‚ ÑамоÑтоÑтельно
83 if (count > openText.length() / 1.5) {
84 System.out.println(openText);
85 System.out.println(i0 + ": " + i);
86 System.out.println(j0 + ": " + j);
87 }
88 }
89 }
90 }
91}