Repository navigation
Expand file tree
/
Copy pathaesEncryption.java
More file actions
68 lines (53 loc) · 1.79 KB
/
Copy pathaesEncryption.java
File metadata and controls
68 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.util.Arrays;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.lang.Throwable;
import org.apache.commons.codec.binary.Base64;
public class aesEncryption {
private static final String CYPHER_MODE = "AES/CBC/PKCS5Padding";
public static byte[] encrypt(byte[] key, byte[] initVector, byte[] value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(CYPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
return cipher.doFinal(value);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] key, byte[] initVector, byte[] encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(CYPHER_MODE);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
return cipher.doFinal(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
try {
InputStream keyFS = new FileInputStream("encryption.key");
byte[] key = new byte[16];
keyFS.read(key);
keyFS.close();
byte[] b64content = Files.readAllBytes(Paths.get("encryptedFile"));
byte[] content = Base64.decodeBase64(b64content);
byte[] iv = Arrays.copyOfRange(content, 0, 16);
byte[] cyphertext = Arrays.copyOfRange(content, 16, content.length);
String plaintext = new String(decrypt(key, iv, cyphertext));
System.out.println(plaintext);
} catch (Throwable t) {
t.printStackTrace();
}
}
}