· 7 years ago · Nov 18, 2018, 02:48 PM
1package sample;
2
3import javax.crypto.Cipher;
4import javax.crypto.spec.IvParameterSpec;
5import javax.crypto.spec.SecretKeySpec;
6import java.nio.ByteBuffer;
7import java.security.SecureRandom;
8import java.util.Arrays;
9import java.util.Random;
10
11public class AesCipher {
12
13// private static final String INIT_VECTOR = "ItIsOurBigSecret";
14// private static final String INIT_VECTOR = "dfgthyjukifredsr";
15
16 // зашифрование
17 static byte[] encrypt(byte[] secretKey, byte[] plainText, String mode) {
18
19 try {
20 if (!isKeyLengthValid(secretKey)) {
21 throw new Exception("Длина ключа должна быть 128, 192 или 256 бит!");
22 }
23
24// IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes());
25 SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
26
27 Cipher cipher = Cipher.getInstance("AES/" + mode + "/PKCS5Padding");
28
29 SecureRandom secureRandom = new SecureRandom();
30 byte[] iv = new byte[cipher.getBlockSize()];
31 secureRandom.nextBytes(iv);
32 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
33
34 if (mode.equals("ECB"))
35 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
36 else
37 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
38
39 byte[] c = cipher.doFinal(plainText);
40
41// return cipher.doFinal(plainText);
42 return ByteBuffer.wrap(new byte[iv.length + c.length]).put(iv).put(c).array();
43 } catch (Throwable cause) {
44 System.out.print(cause.getMessage());
45 }
46
47 return null;
48 }
49
50
51 // дешифрование
52 static byte[] decrypt(byte[] secretKey, byte[] cipherText, String mode) {
53
54 try {
55 if (!isKeyLengthValid(secretKey)) {
56 throw new Exception("Длина ключа должна быть 128, 192 или 256 бит!");
57 }
58
59// IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes());
60 SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
61
62 Cipher cipher = Cipher.getInstance("AES/" + mode + "/NoPadding");
63
64 byte[] iv = new byte[cipher.getBlockSize()];
65 System.arraycopy(cipherText, 0, iv, 0, iv.length);
66 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
67
68 if (mode.equals("ECB"))
69 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
70 else
71 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
72
73 byte[] cipherTextNew = new byte[cipherText.length - iv.length];
74 System.arraycopy(cipherText, 15, cipherTextNew, 0, cipherTextNew.length);
75
76 return cipher.doFinal(cipherTextNew);
77 } catch (Throwable cause) {
78 System.out.print(cause.getMessage());
79 Controller.ShowMessage(cause.getMessage());
80 }
81
82 return null;
83 }
84
85
86 // проверка длины ключа
87 private static boolean isKeyLengthValid(byte[] key) {
88 return key.length == 16 || key.length == 24 || key.length == 32;
89 }
90
91
92 // Ð³ÐµÐ½ÐµÑ€Ð°Ñ†Ð¸Ñ Ð¸Ð½Ð¸Ñ†Ð¸Ð°Ð»Ð¸Ð·Ð¸Ñ€ÑƒÑŽÑ‰ÐµÐ³Ð¾ вектора
93 private static String genInitVector() {
94
95 Random r = new Random();
96 StringBuilder builder = new StringBuilder();
97
98 for (int j = 0; j < 16; j++) {
99 char code = (char) (r.nextInt(94) + 33); // Ñимволы Ñ ÐºÐ¾Ð´Ð°Ð¼Ð¸ от 33 по 126
100 builder.append(Character.toString(code));
101 }
102
103 return builder.toString();
104 }
105
106}