· 7 years ago · Aug 09, 2018, 09:34 AM
1private static byte[] encrypt(byte[] plainData) {
2 byte[] encryptedData = new byte[0];
3 try {
4 byte[] cle = (new String(PASSWORD)).getBytes();
5 SecretKey key = new SecretKeySpec(cle, ALGO);
6 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
7 cipher.init(Cipher.ENCRYPT_MODE, key);
8 encryptedData = cipher.doFinal(plainData);
9 } catch (Exception ex) {
10 //Log Error
11 }
12 return encryptedData;
13 }
14
15 private static void decrypt(InputStream in, OutputStream out) {
16 try {
17 // Cipher INIT
18 byte[] cle = (new String(PASSWORD)).getBytes();
19 SecretKey key = new SecretKeySpec(cle, ALGO);
20 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
21 cipher.init(Cipher.DECRYPT_MODE, key);
22 // Decrypt
23 CipherInputStream cis = new CipherInputStream(in, cipher);
24 byte[] block = new byte[8];
25 int i;
26 while ((i = cis.read(block)) != -1) {
27 out.write(block, 0, i);
28 }
29 out.close();
30 } catch (IOException ex) {
31 //Log Error
32 }
33 }