· 6 years ago · Jun 22, 2019, 12:02 AM
1import javax.security.auth.login.LoginException;
2import java.security.*;
3import javax.crypto.*;
4import java.io.*;
5
6public class Main {
7 public static void main(String[] args) throws LoginException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
8 String s = "Hello world";
9
10 Cipher cipher = Cipher.getInstance("AES");
11
12 KeyGenerator kgen = KeyGenerator.getInstance("AES");
13 kgen.init(128);
14 SecretKey key = kgen.generateKey();
15
16 cipher.init(Cipher.ENCRYPT_MODE, key);
17
18 byte[] bytes = cipher.doFinal(s.getBytes());
19 System.out.print("\n");
20 for(byte b : bytes) {
21 System.out.print(b);
22 }
23 Cipher decryptCipher = Cipher.getInstance("AES");
24 decryptCipher.init(Cipher.DECRYPT_MODE, key);
25 byte[] decriptedBytes = decryptCipher.doFinal(bytes);
26 System.out.print("\n");
27 for(byte b : decriptedBytes) {
28 System.out.print(b);
29 }
30 }
31}