· 7 years ago · Jun 22, 2018, 09:52 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package aes;
7
8import java.io.BufferedReader;
9import java.io.IOException;
10import java.io.InputStreamReader;
11import java.security.InvalidKeyException;
12import java.security.NoSuchAlgorithmException;
13import java.security.SecureRandom;
14import javax.crypto.BadPaddingException;
15import javax.crypto.Cipher;
16import javax.crypto.IllegalBlockSizeException;
17import javax.crypto.KeyGenerator;
18import javax.crypto.NoSuchPaddingException;
19import javax.crypto.SecretKey;
20import javax.crypto.spec.IvParameterSpec;
21import javax.crypto.spec.SecretKeySpec;
22
23public class AES {
24
25 public static byte[] getTextFromUser() {
26 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
27 String text = "";
28
29 try {
30 text = buffer.readLine();
31 } catch (IOException e) {
32 }
33
34 return text.getBytes();
35 }
36
37 public static SecretKey getSecretKey() throws NoSuchAlgorithmException {
38 SecureRandom rand = new SecureRandom();
39 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
40 keyGen.init(128, rand);
41 return keyGen.generateKey();
42 }
43
44 public static byte[] encrypt(String method, byte[] text, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
45 Cipher aes = Cipher.getInstance(method);
46 aes.init(Cipher.ENCRYPT_MODE, secretKey);
47
48 long startTime = System.nanoTime();
49
50 byte[] encryptedText = aes.doFinal(text);
51
52 double endTime = (double) (System.nanoTime() - startTime) / 1000000000.0;
53 System.out.println(method + " encryption time: " + endTime + "s");
54
55 return encryptedText;
56 }
57
58 public static byte[] decrypt(String method, byte[] encryptedText, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchPaddingException, NoSuchPaddingException, NoSuchPaddingException, NoSuchPaddingException, NoSuchPaddingException, IllegalBlockSizeException, IllegalBlockSizeException, BadPaddingException, IllegalBlockSizeException, IllegalBlockSizeException, IllegalBlockSizeException, IllegalBlockSizeException, NoSuchPaddingException, NoSuchPaddingException, InvalidKeyException {
59 Cipher aes = Cipher.getInstance(method);
60 aes.init(Cipher.ENCRYPT_MODE, secretKey);
61
62 long startTime = System.nanoTime();
63 byte[] decryptedText = aes.doFinal(encryptedText);
64
65 double endTime = (double) (System.nanoTime() - startTime) / 1000000000.0;
66 System.out.println(method + " decryption time: " + endTime + "s");
67
68 return decryptedText;
69 }
70
71 public static void runAES(String method, byte[] text, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, BadPaddingException, InvalidKeyException, InvalidKeyException, InvalidKeyException, InvalidKeyException {
72 byte[] encryptedText = encrypt("AES/ECB/PKCS5Padding", text, secretKey);
73
74 decrypt("AES/ECB/PKCS5Padding", encryptedText, secretKey);
75 }
76
77 /**
78 * @param args the command line arguments
79 */
80 public static void main(String[] args) throws Exception {
81 byte[] text = getTextFromUser();
82 SecretKey secretKey = getSecretKey();
83
84 runAES("AES/ECB/PKCS5Padding", text, secretKey);
85 runAES("AES/CTR/NoPadding", text, secretKey);
86 }
87}