· 6 years ago · Aug 14, 2019, 10:22 PM
1public static String decrypt(byte[] cipherText) throws Exception
2{
3
4 String encryptionKey = "6qp4Y?kLmaY8+Fsd";
5 byte[] key = encryptionKey.getBytes("UTF-8");
6 SecretKeySpec secretKey = new SecretKeySpec(key, "AES128");
7
8 byte[] IV = new byte[16];
9 SecureRandom random = new SecureRandom();
10 random.nextBytes(IV);
11
12 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
13 SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
14 IvParameterSpec ivSpec = new IvParameterSpec(IV);
15 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
16 byte[] decryptedText = cipher.doFinal(cipherText);
17 return new String(decryptedText);
18}
19
20
21public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception
22{
23 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
24 SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
25 IvParameterSpec ivSpec = new IvParameterSpec(IV);
26 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
27 byte[] cipherText = cipher.doFinal(plaintext);
28 return cipherText;
29}