-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES.java
More file actions
57 lines (44 loc) · 1.37 KB
/
AES.java
File metadata and controls
57 lines (44 loc) · 1.37 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
/*H****************************************************************
* FILENAME : AES.java
*
* DESCRIPTION :
* Encrypts and decrpyts text using AES cipher
*
* Copyright 2019, Jacob Wilkins. All rights reserved.
*
* AUTHOR : Jacob Wilkins START DATE : 6 Jun 19
*
*H*/
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import java.util.Base64;
public class AES {
public static String AES_encrypt(String text, String key) {
try {
SecretKeySpec sks = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] enc = cipher.doFinal(text.getBytes());
return Base64.getEncoder().encodeToString(enc);
} catch (Exception e) {
System.out.println("Error encrypting AES.\nExiting...");
e.printStackTrace();
System.exit(0);
}
return null;
}
public static String AES_decrypt(String text, String key) {
try {
SecretKeySpec sks = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
byte[] dec = cipher.doFinal(Base64.getDecoder().decode(text));
return new String(dec);
} catch (Exception e) {
System.out.println("Error decrypting AES.\nExiting...");
e.printStackTrace();
System.exit(0);
}
return null;
}
}