· 6 years ago · May 15, 2019, 03:34 PM
1import java.security.NoSuchAlgorithmException;
2import java.security.SecureRandom;
3import java.util.Base64;
4
5import javax.crypto.Cipher;
6import javax.crypto.KeyGenerator;
7import javax.crypto.SecretKey;
8import javax.crypto.spec.IvParameterSpec;
9import javax.crypto.spec.SecretKeySpec;
10
11public class AES {
12
13 static String plainText = "This is a plain text which need to be encrypted by AES Algorithm with CBC Mode";
14
15 public void execute(String text) {
16
17 KeyGenerator keyGenerator;
18 try {
19
20 // Generate Key
21 keyGenerator = KeyGenerator.getInstance("AES");
22 keyGenerator.init(256);
23 SecretKey key = keyGenerator.generateKey();
24
25 // Generating IV.
26 byte[] IV = new byte[16];
27 SecureRandom random = new SecureRandom();
28 random.nextBytes(IV);
29
30 //test encryption speed
31 byte[] cipherText = AES.encrypt(text.getBytes(),key, IV);
32 String EncryptedString = Base64.getEncoder().encodeToString(cipherText);
33
34 //test decryption speed
35 String DecryptedText = AES.decrypt(cipherText,key, IV);
36
37 } catch (NoSuchAlgorithmException e) {
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 } catch (Exception e) {
41 // TODO Auto-generated catch block
42 e.printStackTrace();
43 }
44
45 }
46
47 public static byte[] encrypt (byte[] plaintext,SecretKey key,byte[] IV ) throws Exception
48 {
49 //Get Cipher Instance
50 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
51
52 //Create SecretKeySpec
53 SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
54
55 //Create IvParameterSpec
56 IvParameterSpec ivSpec = new IvParameterSpec(IV);
57
58 //Initialize Cipher for ENCRYPT_MODE
59 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
60
61 //Perform Encryption
62 byte[] cipherText = cipher.doFinal(plaintext);
63
64 return cipherText;
65 }
66
67 public static String decrypt (byte[] cipherText, SecretKey key,byte[] IV) throws Exception
68 {
69 //Get Cipher Instance
70 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
71
72 //Create SecretKeySpec
73 SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
74
75 //Create IvParameterSpec
76 IvParameterSpec ivSpec = new IvParameterSpec(IV);
77
78 //Initialize Cipher for DECRYPT_MODE
79 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
80
81 //Perform Decryption
82 byte[] decryptedText = cipher.doFinal(cipherText);
83
84 return new String(decryptedText);
85 }
86
87}
88
89import java.awt.EventQueue;
90
91import javax.swing.JFrame;
92import javax.crypto.KeyGenerator;
93import javax.crypto.SecretKey;
94import javax.swing.JButton;
95import java.awt.event.ActionListener;
96import java.security.NoSuchAlgorithmException;
97import java.security.SecureRandom;
98import java.util.Base64;
99import java.awt.event.ActionEvent;
100import javax.swing.JTextField;
101import javax.swing.JLabel;
102import java.awt.Font;
103import javax.swing.ImageIcon;
104import java.awt.Color;
105import java.awt.Toolkit;
106import javax.swing.border.BevelBorder;
107import java.awt.ComponentOrientation;
108import javax.swing.UIManager;
109import java.awt.Component;
110
111public class Interfata {
112
113 private JFrame frmProiectSecuritate;
114 private JTextField original;
115 private JTextField criptat;
116 private JTextField decriptat;
117
118 /**
119 * Launch the application.
120 */
121 public static void main(String[] args) {
122 EventQueue.invokeLater(new Runnable() {
123 public void run() {
124 try {
125 Interfata window = new Interfata();
126 window.frmProiectSecuritate.setVisible(true);
127 } catch (Exception e) {
128 e.printStackTrace();
129 }
130 }
131 });
132 }
133
134 /**
135 * Create the application.
136 */
137 public Interfata() {
138 initialize();
139 }
140
141 /**
142 * Initialize the contents of the frame.
143 */
144 private void initialize() {
145 frmProiectSecuritate = new JFrame();
146 frmProiectSecuritate.setBackground(Color.LIGHT_GRAY);
147 frmProiectSecuritate.setTitle("Proiect - Securitate si Criptografie");
148 frmProiectSecuritate.setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\George\\Eclipse\\ProiectCripto\\Aroche-Delta-Key-Session.png"));
149 frmProiectSecuritate.setBounds(100, 100, 450, 300);
150 frmProiectSecuritate.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
151 frmProiectSecuritate.getContentPane().setLayout(null);
152
153 JButton btnAesAlgorythm = new JButton("AES Algorythm");
154 btnAesAlgorythm.setAlignmentX(Component.CENTER_ALIGNMENT);
155 btnAesAlgorythm.setBorder(UIManager.getBorder("Button.border"));
156 btnAesAlgorythm.setBackground(new Color(153, 204, 102));
157 btnAesAlgorythm.setFont(new Font("Sitka Small", Font.PLAIN, 11));
158 btnAesAlgorythm.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent arg0) {
160 KeyGenerator keyGenerator;
161 String text = original.getText();
162 try {
163 keyGenerator = KeyGenerator.getInstance("AES");
164 keyGenerator.init(256);
165 SecretKey key = keyGenerator.generateKey();
166
167 // Generating IV.
168 byte[] IV = new byte[16];
169 SecureRandom random = new SecureRandom();
170 random.nextBytes(IV);
171
172 //test encryption speed
173 byte[] cipherText = AES.encrypt(text.getBytes(),key, IV);
174 String EncryptedString = Base64.getEncoder().encodeToString(cipherText);
175 criptat.setText(EncryptedString);
176
177 //test decryption speed
178 String DecryptedText = AES.decrypt(cipherText,key, IV);
179 decriptat.setText(DecryptedText);
180 } catch (NoSuchAlgorithmException e) {
181 // TODO Auto-generated catch block
182 e.printStackTrace();
183 }catch (Exception e) {
184 // TODO Auto-generated catch block
185 e.printStackTrace();
186 }
187 }
188 });
189 btnAesAlgorythm.setBounds(10, 76, 167, 23);
190 frmProiectSecuritate.getContentPane().add(btnAesAlgorythm);
191
192 JButton btnRsaAlgorythm = new JButton("RSA Algorythm");
193 btnRsaAlgorythm.setBackground(new Color(153, 204, 102));
194 btnRsaAlgorythm.setFont(new Font("Sitka Small", Font.PLAIN, 11));
195 btnRsaAlgorythm.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent arg0) {
197 RSA rsa = new RSA();
198 String teststring;
199
200 // encrypt
201 byte[] encrypted = rsa.encrypt(original.getText().getBytes());
202 String EncryptedString = Base64.getEncoder().encodeToString(encrypted);
203 criptat.setText(EncryptedString);
204 // decrypt
205 byte[] decrypted = rsa.decrypt(encrypted);
206 String DecryptedString = Base64.getEncoder().encodeToString(decrypted);
207 decriptat.setText(DecryptedString);
208 }
209 });
210 btnRsaAlgorythm.setBounds(10, 110, 167, 23);
211 frmProiectSecuritate.getContentPane().add(btnRsaAlgorythm);
212
213 JButton btnVigenereAlgorythm = new JButton("Vigenere Algorythm");
214 btnVigenereAlgorythm.setBackground(new Color(153, 204, 102));
215 btnVigenereAlgorythm.setFont(new Font("Sitka Small", Font.PLAIN, 11));
216 btnVigenereAlgorythm.addActionListener(new ActionListener() {
217 public void actionPerformed(ActionEvent e) {
218 String key = "CIPHER";
219
220 String message = original.getText();
221
222 criptat.setText(Vigenere.vcriptare(message, key));
223
224 decriptat.setText(Vigenere.vdecriptare(Vigenere.vcriptare(message, key), key));
225 }
226 });
227 btnVigenereAlgorythm.setBounds(10, 144, 167, 23);
228 frmProiectSecuritate.getContentPane().add(btnVigenereAlgorythm);
229
230 original = new JTextField();
231 original.setBackground(new Color(255, 255, 102));
232 original.setSelectedTextColor(new Color(204, 255, 204));
233 original.setBounds(135, 8, 289, 20);
234 frmProiectSecuritate.getContentPane().add(original);
235 original.setColumns(10);
236
237 criptat = new JTextField();
238 criptat.setBackground(new Color(255, 255, 102));
239 criptat.setSelectedTextColor(new Color(204, 255, 204));
240 criptat.setBounds(135, 184, 289, 20);
241 frmProiectSecuritate.getContentPane().add(criptat);
242 criptat.setColumns(10);
243
244 decriptat = new JTextField();
245 decriptat.setBackground(new Color(255, 255, 102));
246 decriptat.setSelectedTextColor(new Color(204, 255, 204));
247 decriptat.setBounds(135, 215, 289, 20);
248 frmProiectSecuritate.getContentPane().add(decriptat);
249 decriptat.setColumns(10);
250
251 JLabel lblIntroducetiTextul = new JLabel("Introduce\u021Bi textul:");
252 lblIntroducetiTextul.setFont(new Font("Bahnschrift", Font.PLAIN, 11));
253 lblIntroducetiTextul.setBounds(10, 11, 115, 14);
254 frmProiectSecuritate.getContentPane().add(lblIntroducetiTextul);
255
256 JLabel lblTextulCriptat = new JLabel("Textul criptat:");
257 lblTextulCriptat.setFont(new Font("Bahnschrift", Font.PLAIN, 11));
258 lblTextulCriptat.setBounds(10, 187, 108, 14);
259 frmProiectSecuritate.getContentPane().add(lblTextulCriptat);
260
261 JLabel lblNewLabel = new JLabel("Textul decriptat:");
262 lblNewLabel.setFont(new Font("Bahnschrift", Font.PLAIN, 11));
263 lblNewLabel.setBounds(10, 218, 119, 14);
264 frmProiectSecuritate.getContentPane().add(lblNewLabel);
265
266 JLabel lblAlegetiUnulDin = new JLabel("Alege\u021Bi unul din cei 3 algoritmi:");
267 lblAlegetiUnulDin.setFont(new Font("Candara", Font.BOLD, 15));
268 lblAlegetiUnulDin.setBounds(10, 39, 382, 14);
269 frmProiectSecuritate.getContentPane().add(lblAlegetiUnulDin);
270
271 JLabel lblNewLabel_1 = new JLabel("New label");
272 lblNewLabel_1.setFont(new Font("Berlin Sans FB", Font.PLAIN, 15));
273 lblNewLabel_1.setIcon(new ImageIcon("D:\\George\\Eclipse\\ProiectCripto\\pisica.jpg"));
274 lblNewLabel_1.setBounds(0, 0, 434, 261);
275 frmProiectSecuritate.getContentPane().add(lblNewLabel_1);
276 }
277}
278
279import java.io.DataInputStream;
280import java.io.IOException;
281import java.math.BigInteger;
282import java.util.Random;
283
284public class RSA {
285 private BigInteger p;
286 private BigInteger q;
287 private BigInteger N;
288 private BigInteger phi;
289 private BigInteger e;
290 private BigInteger d;
291 private int bitlength = 1024;
292 private Random r;
293
294 public RSA() {
295 r = new Random();
296 p = BigInteger.probablePrime(bitlength, r);
297 q = BigInteger.probablePrime(bitlength, r);
298 N = p.multiply(q);
299 phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
300 e = BigInteger.probablePrime(bitlength / 2, r);
301 while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) {
302 e.add(BigInteger.ONE);
303 }
304 d = e.modInverse(phi);
305 }
306
307 public RSA(BigInteger e, BigInteger d, BigInteger N) {
308 this.e = e;
309 this.d = d;
310 this.N = N;
311 }
312
313 @SuppressWarnings("deprecation")
314 public static void main(String[] args) throws IOException {
315 }
316
317 private static String bytesToString(byte[] encrypted) {
318 String test = "";
319 for (byte b : encrypted) {
320 test += Byte.toString(b);
321 }
322 return test;
323 }
324
325 // Encrypt message
326 public byte[] encrypt(byte[] message) {
327 return (new BigInteger(message)).modPow(e, N).toByteArray();
328 }
329
330 // Decrypt message
331 public byte[] decrypt(byte[] message) {
332 return (new BigInteger(message)).modPow(d, N).toByteArray();
333 }
334}
335
336public class Vigenere {
337
338 public static String vcriptare(String text, final String key)
339
340 {
341
342 String res = "";
343 text = text.toUpperCase();
344
345 for (int i = 0, j = 0; i < text.length(); i++)
346
347 {
348
349 char c = text.charAt(i);
350
351 if (c < 'A' || c > 'Z')
352
353 continue;
354
355 res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
356
357 j = ++j % key.length();
358
359 }
360
361 return res;
362
363 }
364
365
366
367 public static String vdecriptare(String text, final String key)
368
369 {
370
371 String res = "";
372
373 text = text.toUpperCase();
374
375 for (int i = 0, j = 0; i < text.length(); i++)
376
377 {
378
379 char c = text.charAt(i);
380
381 if (c < 'A' || c > 'Z')
382
383 continue;
384
385 res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
386
387 j = ++j % key.length();
388
389 }
390
391 return res;
392
393 }
394 public static void main(String[] args) {
395 // TODO Auto-generated method stub
396 String key = "CIPHER";
397
398 String message = "AWJHVVPWLTEJVMGPRXXQVLRVTMROMGGZ";
399
400 System.out.println("Mesajul: " + message);
401
402 System.out.println("Mesajul decriptat: " + vdecriptare(message, key));
403 }
404
405}