· 7 years ago · May 20, 2018, 09:06 PM
1import javax.crypto.Cipher;
2import javax.crypto.SecretKey;
3import javax.crypto.spec.IvParameterSpec;
4import javax.crypto.spec.SecretKeySpec;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileOutputStream;
8
9public class Test {
10 private SecretKey secretKey;
11 private IvParameterSpec ivParameterSpec;
12
13 private String key = "ThisIsMyGreatKey";
14 private byte[] ivKey = "ABCDEFGHabcdefgh".getBytes();
15
16 public static void main(String[] args) {
17 try {
18 new Test().run();
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
22 }
23
24 private void run() {
25 ivParameterSpec = new IvParameterSpec(ivKey);
26 secretKey = new SecretKeySpec(key.getBytes(), "AES");
27 encryptOrDecryptFile(Cipher.ENCRYPT_MODE,
28 new File("src/cactus.jpg"), new File("src/cactus-encrypted.jpg"));
29 }
30
31 private void encryptOrDecryptFile(int mode, File inputFile, File outputFile) {
32 try {
33 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
34 cipher.init(mode, secretKey, ivParameterSpec);
35
36 // Read input
37 byte[] input = new byte[(int) inputFile.length()];
38 FileInputStream inputStream = new FileInputStream(inputFile);
39 inputStream.read(input);
40
41 // Encrypt and write to output
42 byte[] output = cipher.doFinal(input);
43 FileOutputStream outputStream = new FileOutputStream(outputFile);
44 outputStream.write(output);
45
46 inputStream.close();
47 outputStream.close();
48 } catch (Exception e) {
49 e.printStackTrace();
50 }
51 }
52}
53
54<input type="file" id="file-input" onchange="handleFile(this)">
55<button onclick="useEncryptionForFile()" id="encrypt-file">Encrypt File</button>
56<button onclick="useDecryptionForFile()" id="decrypt-file">Decrypt File</button>
57<textarea id="output"></textarea>
58<img id="example">
59
60<script>
61 let key = "ThisIsMyGreatKey";
62 let iv = "ABCDEFGHabcdefgh";
63 let useEncryption, useDecryption;
64
65
66 function encrypt(text) {
67 let encrypted = CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(key), {
68 iv: CryptoJS.enc.Utf8.parse(iv),
69 mode: CryptoJS.mode.CBC,
70 padding: CryptoJS.pad.Pkcs7
71 });
72
73 return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
74 }
75
76 function decrypt(encrypted) {
77 let decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Utf8.parse(key), {
78 iv: CryptoJS.enc.Utf8.parse(iv),
79 mode: CryptoJS.mode.CBC,
80 padding: CryptoJS.pad.Pkcs7
81 });
82
83 // Should be Latin1 for files?
84 return decrypted.toString(CryptoJS.enc.Utf8);
85 }
86
87 let input = document.getElementById("file-input");
88 let output = document.getElementById("output");
89 let example = document.getElementById("example");
90
91 function handleFile(element) {
92 if (element.files && element.files[0]) {
93 let file = element.files[0];
94 if (useDecryption) {
95 decryptFile(file);
96 } else {
97 encryptFile(file);
98 }
99 }
100 }
101
102 function encryptFile(file) {
103 let reader = new FileReader();
104
105 reader.onload = function (e) {
106 let encrypted = CryptoJS.AES.encrypt(e.target.result, CryptoJS.enc.Utf8.parse(key), {
107 iv: CryptoJS.enc.Utf8.parse(iv),
108 mode: CryptoJS.mode.CBC,
109 padding: CryptoJS.pad.Pkcs7
110 });
111
112 output.textContent = encrypted;
113
114 let a = document.createElement("a");
115 a.setAttribute('href', 'data:application/octet-stream,' + encrypted);
116 a.setAttribute('download', file.name + '.encrypted');
117 a.click();
118 };
119
120 reader.readAsDataURL(file);
121 }
122
123 function decryptFile(file) {
124 let reader = new FileReader();
125 reader.onload = function (e) {
126 let decrypted = CryptoJS.AES.decrypt(e.target.result, CryptoJS.enc.Utf8.parse(key), {
127 iv: CryptoJS.enc.Utf8.parse(iv),
128 mode: CryptoJS.mode.CBC,
129 padding: CryptoJS.pad.Pkcs7
130 }).toString(CryptoJS.enc.Latin1);
131
132 output.textContent = decrypted;
133
134 if (!/^data:/.test(decrypted)) {
135 alert("Failed to decrypt file");
136 return false;
137 }
138
139 let a = document.createElement("a");
140 a.setAttribute('href', decrypted);
141 a.setAttribute('download', file.name.replace('.encrypted', ''));
142 a.click();
143 };
144
145 reader.readAsText(file);
146 }
147
148 function useEncryptionForFile() {
149 document.getElementById("encrypt-file").style.backgroundColor = "#757575";
150 document.getElementById("decrypt-file").style.backgroundColor = "#FFFFFF";
151 useEncryption = true;
152 useDecryption = false;
153 }
154
155 function useDecryptionForFile() {
156 document.getElementById("encrypt-file").style.backgroundColor = "#FFFFFF";
157 document.getElementById("decrypt-file").style.backgroundColor = "#757575";
158 useDecryption = true;
159 useEncryption = false;
160 }
161</script>