· 8 years ago · Dec 07, 2017, 07:34 AM
1import java.awt.Container;
2import java.security.InvalidAlgorithmParameterException;
3import java.security.InvalidKeyException;
4import java.security.NoSuchAlgorithmException;
5import java.security.SecureRandom;
6import java.util.Base64;
7
8import javax.crypto.BadPaddingException;
9import javax.crypto.Cipher;
10import javax.crypto.IllegalBlockSizeException;
11import javax.crypto.KeyGenerator;
12import javax.crypto.NoSuchPaddingException;
13import javax.crypto.SecretKey;
14import javax.crypto.spec.IvParameterSpec;
15import java.awt.Dimension;
16import java.awt.EventQueue;
17import java.awt.Toolkit;
18
19import javax.swing.GroupLayout;
20import javax.swing.JComponent;
21import javax.swing.JFrame;
22import javax.swing.JLabel;
23import javax.swing.JPanel;
24import javax.swing.JTabbedPane;
25import java.util.Scanner;
26
27 public class AES256 extends JFrame{
28
29 /**
30 *
31 */
32 private static final long serialVersionUID = 1L;
33 private SecretKey aeskey = null;
34 String s = null;
35 String d = null;
36 String plaintext = null;
37
38 public AES256() throws NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException {
39 Scanner scan = new Scanner(System.in);
40 System.out.print("What information would you like to encrypt with AES 256 in CBC mode: ");
41 String plaintext = scan.nextLine()
42 + "";
43 KeyGenerator keygen = KeyGenerator.getInstance("AES") ; // Specifying algorithm key will be used for
44 keygen.init(128);
45 aeskey = keygen.generateKey() ;
46
47 byte[] encrypted = encrypt(plaintext, aeskey);
48 String decrypted = decrypt(encrypted, aeskey);
49 s= (Base64.getEncoder().encodeToString(encrypted));
50 d = decrypted;
51 System.out.println("The encrypted text: " + Base64.getEncoder().encodeToString(encrypted));
52 System.out.println("The decrypted text: " + decrypted);
53
54 initUI();
55 this.setTitle("AES256 Encryption");
56 //this.setSize(1920,1080);
57 Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
58 this.setMaximumSize(DimMax);
59
60 this.setExtendedState(JFrame.MAXIMIZED_BOTH);
61 }
62
63 // Generating Key
64
65 public static byte[] encrypt(String plaintext, SecretKey aeskey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
66 byte[] plainbytes = plaintext.getBytes();
67
68 // Generating IV.
69 int ivSize = 16;
70 byte[] iv = new byte[ivSize];
71 SecureRandom random = new SecureRandom();
72 random.nextBytes(iv);
73 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
74
75 // Encrypt.
76 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
77 cipher.init(Cipher.ENCRYPT_MODE, aeskey, ivParameterSpec);
78 byte[] encrypted = cipher.doFinal(plainbytes);
79
80 // Combine IV and encrypted part.
81 byte[] encryptedIVAndText = new byte[ivSize + encrypted.length];
82 System.arraycopy(iv, 0, encryptedIVAndText, 0, ivSize);
83 System.arraycopy(encrypted, 0, encryptedIVAndText, ivSize, encrypted.length);
84
85 return encryptedIVAndText;
86 }
87
88 public static String decrypt(byte[] encryptedIvTextBytes, SecretKey aeskey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
89 int ivSize = 16;
90
91 // Extract IV.
92 byte[] iv = new byte[ivSize];
93 System.arraycopy(encryptedIvTextBytes, 0, iv, 0, iv.length);
94 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
95
96 // Extract encrypted part.
97 int encryptedSize = encryptedIvTextBytes.length - ivSize;
98 byte[] encryptedBytes = new byte[encryptedSize];
99 System.arraycopy(encryptedIvTextBytes, ivSize, encryptedBytes, 0, encryptedSize);
100
101 // Decrypt.
102 Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
103 cipherDecrypt.init(Cipher.DECRYPT_MODE, aeskey, ivParameterSpec);
104 byte[] decrypted = cipherDecrypt.doFinal(encryptedBytes);
105
106 return new String(decrypted);
107 }
108 private void initUI() {
109 JTabbedPane tbp = new JTabbedPane();
110 tbp.addTab("Encrypted AES256", createPanel(s));
111 tbp.addTab("Decrypted AES256", createPanel(d));
112
113 createLayout(tbp);
114
115 setTitle("JTabbedPane");
116 setLocationRelativeTo(null);
117 setDefaultCloseOperation(EXIT_ON_CLOSE);
118 }
119
120 public JPanel createPanel(String text) {
121
122 JPanel panel = new JPanel();
123 JLabel lbl = new JLabel(text);
124 panel.add(lbl);
125
126 return panel;
127 }
128 private void createLayout(JComponent... arg) {
129
130 Container pane = getContentPane();
131 GroupLayout gl = new GroupLayout(pane);
132 pane.setLayout(gl);
133
134 gl.setAutoCreateContainerGaps(true);
135 gl.setAutoCreateGaps(true);
136
137 gl.setHorizontalGroup(gl.createSequentialGroup()
138 .addComponent(arg[0])
139 );
140
141 gl.setVerticalGroup(gl.createParallelGroup()
142 .addComponent(arg[0])
143 );
144
145 //pack();
146 }
147}