-
Notifications
You must be signed in to change notification settings - Fork 0
/
AES.java
57 lines (40 loc) · 1.44 KB
/
AES.java
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
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.binary.*;
class AES
{
public String Encrypt(String plainText, String key) throws
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException,
UnsupportedEncodingException
{
SecretKeySpec KeyMaker = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
// Instantiate the Encryptor
Cipher Encryptor = Cipher.getInstance("AES");
Encryptor.init(Cipher.ENCRYPT_MODE, KeyMaker);
byte[] EncBytes = Encryptor.doFinal(plainText.getBytes("UTF-8"));
return new Base64().encodeAsString(EncBytes);
}
public String Decrypt(String encryptedText, String key) throws
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException,
UnsupportedEncodingException
{
SecretKeySpec KeyMaker = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
// Instantiate the Encryptor
Cipher Encryptor = Cipher.getInstance("AES");
Encryptor.init(Cipher.DECRYPT_MODE, KeyMaker);
new Base64();
byte[] EncBytes = Base64.decodeBase64(encryptedText);
byte[] decryptedTextBytes = Encryptor.doFinal(EncBytes);
return new String(decryptedTextBytes);
}
}