· 9 years ago · Nov 01, 2016, 01:04 PM
1package ru.psqq;
2
3import javax.crypto.Cipher;
4import javax.crypto.spec.IvParameterSpec;
5import javax.crypto.spec.SecretKeySpec;
6import java.io.*;
7import java.security.Key;
8import java.security.MessageDigest;
9import java.util.Base64;
10
11public class Main {
12
13 private static final String ALGORITHM = "AES";
14 private static final String TRANSFORMATION = "AES";
15
16 private static final String KEY = "ZZHHYYTTUUHHGGRR";
17 private static final String IV = "AAACCCDDDYYUURRS";
18
19 public static void main(String[] args) {
20 try {
21 test_encrypt_decrypt();
22 } catch (Exception e) {
23 System.out.println("Error: " + e.getMessage());
24 System.exit(1);
25 }
26 }
27
28 public static void test_encrypt_decrypt() throws Exception {
29 // encrypt "in.txt" -> "out.txt"
30 String s = readFile("in.txt");
31 String res = encrypt("mykey", IV, s);
32 PrintWriter writer = new PrintWriter("out.txt", "UTF-8");
33 writer.print(res);
34 writer.close();
35 // decrypt "out.txt" -> "out2.txt"
36 s = readFile("out.txt");
37 res = decrypt("mykey", IV, s);
38 writer = new PrintWriter("out2.txt", "UTF-8");
39 writer.print(res);
40 writer.close();
41 }
42
43 public static String encrypt(String key, String iv, String msg) throws Exception {
44 byte[] bytesOfKey = key.getBytes("UTF-8");
45 MessageDigest md = MessageDigest.getInstance("MD5");
46 byte[] keyBytes = md.digest(bytesOfKey);
47
48 final byte[] ivBytes = iv.getBytes();
49
50 SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
51 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
52 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(ivBytes));
53
54 final byte[] resultBytes = cipher.doFinal(msg.getBytes());
55 return Base64.getMimeEncoder().encodeToString(resultBytes);
56 }
57
58 public static void test_decrypt() throws Exception {
59 String s = readFile("in.txt");
60 String res = decrypt(KEY, IV, s);
61 System.out.println("res:\n" + res);
62 }
63
64 public static String decrypt(String key, String iv, String encrypted) throws Exception {
65 byte[] bytesOfKey = key.getBytes("UTF-8");
66 MessageDigest md = MessageDigest.getInstance("MD5");
67 byte[] keyBytes = md.digest(bytesOfKey);
68
69 final byte[] ivBytes = iv.getBytes();
70
71 final byte[] encryptedBytes = Base64.getMimeDecoder().decode(encrypted);
72
73 SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
74 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
75 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivBytes));
76
77 final byte[] resultBytes = cipher.doFinal(encryptedBytes);
78 return new String(resultBytes);
79 }
80
81 public static String readFile(String filename) throws Exception {
82 BufferedReader br = new BufferedReader(new FileReader(filename));
83 try {
84 StringBuilder sb = new StringBuilder();
85 String line = br.readLine();
86 while (line != null) {
87 sb.append(line);
88 sb.append(System.lineSeparator());
89 line = br.readLine();
90 }
91 String everything = sb.toString();
92 return everything;
93 } finally {
94 br.close();
95 }
96 }
97
98 public static void test_read_file() throws Exception {
99 String s = readFile("in.txt");
100 System.out.println("in.txt:\n" + s);
101 }
102
103 public static void encrypt(String key, File inputFile, File outputFile) throws Exception {
104 doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
105 }
106
107 public static void decrypt(String key, File inputFile, File outputFile) throws Exception {
108 doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
109 }
110
111 static void doCrypto(int cipherMode, String key, File inputFile,
112 File outputFile) throws Exception {
113 Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
114 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
115 cipher.init(cipherMode, secretKey);
116
117 FileInputStream inputStream = new FileInputStream(inputFile);
118 byte[] inputBytes = new byte[(int) inputFile.length()];
119 inputStream.read(inputBytes);
120
121 byte[] outputBytes = cipher.doFinal(inputBytes);
122
123 FileOutputStream outputStream = new FileOutputStream(outputFile);
124 outputStream.write(outputBytes);
125
126 inputStream.close();
127 outputStream.close();
128 }
129
130 public static void test_doCrypto(String filename) throws Exception {
131 String key = "Mary has one cat";
132 File inputFile = new File("document.txt");
133 File encryptedFile = new File("document.encrypted");
134 File decryptedFile = new File("document.decrypted");
135
136 encrypt(key, inputFile, encryptedFile);
137 decrypt(key, encryptedFile, decryptedFile);
138 }
139
140 public static void test_write_to_file(String filename) throws Exception {
141 PrintWriter writer = new PrintWriter(filename, "UTF-8");
142 writer.println("The first line");
143 writer.println("The second line");
144 writer.close();
145 }
146}