· 7 years ago · Oct 27, 2018, 01:48 PM
1import javax.crypto.Cipher;
2import javax.crypto.spec.SecretKeySpec;
3
4public class Main {
5 public static void main(String[] args) throws Exception {
6 byte[] plain = "abcdefghijklmnopabcdefghijklmnop".getBytes();
7 byte[] key = "klucz--128-bitow".getBytes();
8 String plainString= new String(plain,"UTF-8");
9 String encryptedString;
10 String decryptedStrng;
11 SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
12 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
13 System.out.println(plainString);
14
15
16 // szyfrowanie
17 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
18 byte encrypted[] = cipher.doFinal(plain);
19 encryptedString=new String (encrypted,"UTF-8");
20 System.out.println(encryptedString);
21
22 // deszyfrowanie
23 cipher.init(Cipher.DECRYPT_MODE, secretKey);
24 byte decrypted[] = cipher.doFinal(encrypted);
25 decryptedStrng=new String (decrypted,"UTF-8");
26 System.out.println(decryptedStrng);
27
28 }
29}