· 7 years ago · Oct 28, 2018, 07:36 PM
1package aes;
2
3import javax.crypto.Cipher;
4import javax.crypto.spec.SecretKeySpec;
5
6public class AdvancedEncryptionStandard {
7 private byte[] key;
8
9 public AdvancedEncryptionStandard(byte[] key){
10 this.key = key;
11 }
12
13 public byte[] encrypt(byte[] plainText) throws Exception{
14 SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
15 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
16 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
17
18 return cipher.doFinal(plainText);
19 }
20
21 public byte[] decrypt(byte[] cipherText) throws Exception{
22 SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
23 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
24 cipher.init(Cipher.DECRYPT_MODE, secretKey);
25
26 return cipher.doFinal(cipherText);
27 }
28}