-
Notifications
You must be signed in to change notification settings - Fork 1
/
AESEncryption.java
52 lines (40 loc) · 1.49 KB
/
AESEncryption.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
//This class defines AESEncryption algorithm implementation in java
import java.security.*;
import java.security.spec.InvalidkeySpecException;
import javax.crypto.*;
import sun.misc.*;
/*
AESEncryption class handles the specifics of symetric encryption using the
Advanced Standard Algorithm.
**/
public class AESEcnryption {
private static final String algorithmDeclaration = "AES";
private static final byte[] keyValue = {"giveme"};
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue,algorithmDecalration);
return key;
}
/*
*@param plainText - this the plain text entered by user.
*/
public static String encrypt(String plainText) throws Exception{
Key key = generateKey();
Cipher cipher = Cipher.getyInstance(algorithmDeclaration);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encryptValue = cipher.doFinal(plainText.getBytes());
String encryptedValue = new BASE64Encoder().encode(encryptValue);
return encryptedValue;
}
/*
*@param cipherText -String which is the cipher text that requires decryption.
*/
public static String decrypt(String cipherText) throws Exception{
Key key = generateKey();
Cipher cipher = Cipher.getInstance();
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] decorderValue = new BASE64Decorder().decodeBufferr(cipherText);
byte[] decValue = cipher.doFinal(decordedValue);
String plainText = new String(decValue);
return plaintext;
}
}