· 6 years ago · Apr 02, 2019, 05:28 AM
1public class Main
2{
3 public static void main(String[] args)
4 {
5 String plainText = "Testing encryption algorithms";
6
7 System.out.println("Text for encryption: " + plainText);
8
9 //DES
10
11 Encrypted des = Crypto.encrypt(plainText, Crypto.ALGORITHM_DES);
12
13 if(des != null)
14 {
15 System.out.println(des.toString());
16
17 String decrypted = Crypto.decrypt(des.getEncryptedText(), des.getKey(), des.getEncryptionMethod());
18
19 System.out.println("DES: Decrypted text: " + decrypted);
20 }
21
22 //3DES
23
24 Encrypted des3 = Crypto.encrypt(plainText, Crypto.ALGORITHM_3DES);
25
26 if(des3 != null)
27 {
28 System.out.println(des3.toString());
29
30 String decrypted = Crypto.decrypt(des3.getEncryptedText(), des3.getKey(), des3.getEncryptionMethod());
31
32 System.out.println("3DES: Decrypted text: " + decrypted);
33 }
34
35 //AES
36
37 Encrypted aes = Crypto.encrypt(plainText, Crypto.ALGORITHM_AES);
38
39 if(aes != null)
40 {
41 System.out.println(aes.toString());
42
43 String decrypted = Crypto.decrypt(aes.getEncryptedText(), aes.getKey(), aes.getEncryptionMethod());
44
45 System.out.println("AES: Decrypted text: " + decrypted);
46 }
47 }
48}
49
50import javax.crypto.*;
51import javax.crypto.spec.SecretKeySpec;
52import java.nio.charset.StandardCharsets;
53import java.security.InvalidKeyException;
54import java.security.NoSuchAlgorithmException;
55
56public class Crypto
57{
58 public static final String ALGORITHM_DES = "DES";
59 public static final String ALGORITHM_3DES = "DESede";
60 public static final String ALGORITHM_AES = "AES";
61
62 public static Encrypted encrypt(String text, String encryption)
63 {
64 try
65 {
66 //Generate key
67 KeyGenerator keyGenerator = KeyGenerator.getInstance(encryption);
68 SecretKey desKey = keyGenerator.generateKey();
69
70 //Initialize cipher
71 Cipher cipher = Cipher.getInstance(encryption);
72 cipher.init(Cipher.ENCRYPT_MODE, desKey);
73
74 //Convert text to byte array
75 byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
76
77 //Encrypt
78 byte[] encryptedBytes = cipher.doFinal(bytes);
79
80 return new Encrypted(encryptedBytes, desKey.getEncoded(), encryption);
81 }
82 catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e)
83 {
84 e.printStackTrace();
85 }
86
87 return null;
88 }
89
90 public static String decrypt(byte[] encryptedText, byte[] key, String encryption)
91 {
92 try
93 {
94 //Convert key
95 SecretKey desKey = new SecretKeySpec(key, 0, key.length, encryption);
96
97 //Initialize cipher
98 Cipher cipher = Cipher.getInstance(encryption);
99 cipher.init(Cipher.DECRYPT_MODE, desKey);
100
101 //Decrypt
102 byte[] decryptedBytes = cipher.doFinal(encryptedText);
103
104 return new String(decryptedBytes);
105 }
106 catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e)
107 {
108 e.printStackTrace();
109 }
110
111 return null;
112 }
113}
114
115import java.nio.charset.StandardCharsets;
116
117public class Encrypted
118{
119 private byte[] encryptedText;
120 private byte[] key;
121 private String encryptionMethod;
122
123 public Encrypted(byte[] encryptedText, byte[] key, String encryptionMethod)
124 {
125 this.encryptedText = encryptedText;
126 this.key = key;
127 this.encryptionMethod = encryptionMethod;
128 }
129
130 public byte[] getEncryptedText()
131 {
132 return encryptedText;
133 }
134
135 public byte[] getKey()
136 {
137 return key;
138 }
139
140 public String getEncryptionMethod()
141 {
142 return encryptionMethod;
143 }
144
145 @Override
146 public String toString()
147 {
148 return encryptionMethod + ": " + "Encrypted Text: " + new String(encryptedText, StandardCharsets.UTF_8) + " Generated Key: " + new String(key, StandardCharsets.UTF_8);
149 }
150}