· 7 years ago · Apr 04, 2018, 12:48 PM
1public String encrypt(String data) throws Exception {
2
3 byte[] encAes = null;
4
5
6 SecretKey key = new SecretKeySpec(pass, 0, pass.length, "AES");
7 Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
8 c.init(Cipher.ENCRYPT_MODE, key);
9
10 encAes = c.doFinal(data.getBytes());
11
12
13 return enc.encodeToString(encAes);
14
15 }
16
17 /**
18 * DesifrovánÃ, vstup je kódován BASE64! Výstup kódován BASE64!
19 *
20 * @param data
21 * @return
22 * @throws Exception
23 */
24 public String decrypt(String data) throws Exception {
25
26 byte[] decAes = null;
27 // --------------- inicializace, desifrovani AES-128, mod ECB, padding
28 // PKCS5Padding---
29
30 SecretKey key = new SecretKeySpec(pass, 0, pass.length, "AES");
31 Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
32 c.init(Cipher.DECRYPT_MODE, key);
33
34 // --------------------- vytvorit instanci tÅ™Ãdy Cipher, inicializovat a použÃt ---
35 decAes = c.doFinal(data.getBytes());
36 // -------------------------------------------------------------------
37 return new String(decAes);
38 }