· 7 years ago · Jun 22, 2018, 10:12 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.InvalidAlgorithmParameterException;
12import java.security.InvalidKeyException;
13import java.security.NoSuchAlgorithmException;
14import java.security.SecureRandom;
15import javax.crypto.BadPaddingException;
16import javax.crypto.Cipher;
17import javax.crypto.IllegalBlockSizeException;
18import javax.crypto.KeyGenerator;
19import javax.crypto.NoSuchPaddingException;
20import javax.crypto.SecretKey;
21import javax.crypto.spec.IvParameterSpec;
22
23/**
24 *
25 * @author tpodkowski
26 */
27public class AES {
28
29 public static byte[] getTextFromUser() {
30 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
31 String text = "";
32
33 try {
34 text = buffer.readLine();
35 } catch (IOException e) {
36 }
37
38 return text.getBytes();
39 }
40
41 public static SecretKey getSecretKey() throws NoSuchAlgorithmException {
42 SecureRandom rand = new SecureRandom();
43 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
44 keyGen.init(128, rand);
45 return keyGen.generateKey();
46 }
47
48 public static byte[] encrypt(String method, byte[] text, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
49 Cipher aes = Cipher.getInstance(method);
50 aes.init(Cipher.ENCRYPT_MODE, secretKey, method == "AES/CTR/NoPadding" ? new IvParameterSpec(new byte[16]) : null);
51
52 long startTime = System.nanoTime();
53
54 byte[] encryptedText = aes.doFinal(text);
55
56 double endTime = (double) (System.nanoTime() - startTime) / 1000000000.0;
57 System.out.println(method + " encryption time: " + endTime + "s");
58
59 return encryptedText;
60 }
61
62 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, InvalidAlgorithmParameterException {
63 Cipher aes = Cipher.getInstance(method);
64
65 aes.init(Cipher.DECRYPT_MODE, secretKey, method == "AES/CTR/NoPadding" ? new IvParameterSpec(new byte[16]) : null);
66
67 long startTime = System.nanoTime();
68 byte[] decryptedText = aes.doFinal(encryptedText);
69
70 double endTime = (double) (System.nanoTime() - startTime) / 1000000000.0;
71 System.out.println(method + " decryption time: " + endTime + "s");
72
73 return decryptedText;
74 }
75
76 public static void runAES(String method, byte[] text, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, BadPaddingException, InvalidKeyException, InvalidKeyException, InvalidKeyException, InvalidKeyException, InvalidAlgorithmParameterException {
77 byte[] encryptedText = encrypt(method, text, secretKey);
78 byte[] decryptedText = decrypt(method, encryptedText, secretKey);
79
80 System.out.println("Encrypted text " + method + ": " + encryptedText);
81 System.out.println("Decrypted text " + method + ": " + new String(decryptedText));
82 }
83
84 /**
85 * @param args the command line arguments
86 */
87 public static void main(String[] args) throws Exception {
88 byte[] text = getTextFromUser();
89 SecretKey secretKey = getSecretKey();
90
91 runAES("AES/ECB/PKCS5Padding", text, secretKey);
92 runAES("AES/CTR/NoPadding", text, secretKey);
93 }
94}