· 5 years ago · Jan 28, 2020, 05:30 AM
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
3131
3232
3333
3434
3535
3636
3737
3838
3939
4040
4141
4242
4343
4444
4545
4646
4747
4848
4949
5050
51import java.util.*;
52import java.io.BufferedReader;
53import java.io.InputStreamReader;
54import java.security.spec.KeySpec;
55import javax.crypto.Cipher;
56import javax.crypto.SecretKey;
57import javax.crypto.SecretKeyFactory;
58import javax.crypto.spec.DESedeKeySpec;
59//import sun.misc.BASE64Decoder;
60//import sun.misc.BASE64Encoder;
61import java.util.Base64;
62public class DES {
63private static final String UNICODE_FORMAT = "UTF8";
64public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
65private KeySpec myKeySpec;
66private SecretKeyFactory mySecretKeyFactory;
67private Cipher cipher;
68byte[] keyAsBytes;
69private String myEncryptionKey;
70private String myEncryptionScheme;
71SecretKey key;
72static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
73public DES() throws Exception {
74// TODO code application logic here
75myEncryptionKey="ThisIsSecretEncryptionKey";
76myEncryptionScheme=DESEDE_ENCRYPTION_SCHEME;
77keyAsBytes=myEncryptionKey.getBytes(UNICODE_FORMAT);
78myKeySpec=new DESedeKeySpec(keyAsBytes);
79mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
80cipher = Cipher.getInstance(myEncryptionScheme);
81key = mySecretKeyFactory.generateSecret(myKeySpec);
82}
83public String encrypt(String unencryptedString)
84{ String encryptedString = null;
85try {
86cipher.init(Cipher.ENCRYPT_MODE, key);
87byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
88byte[] encryptedText = cipher.doFinal(plainText);
89encryptedString = Base64.getEncoder().encodeToString(encryptedText);
90}
91catch (Exception e)
92{
93e.printStackTrace();
94}
95return encryptedString;
96}
97DES.java 06-02-2019 10:29
98Page 1 of 2
9951
10052
10153
10254
10355
10456
10557
10658
10759
10860
10961
11062
11163
11264
11365
11466
11567
11668
11769
11870
11971
12072
12173
12274
12375
12476
12577
12678
12779
12880
12981
13082
13183
13284
13385
13486
135public String decrypt(String encryptedString)
136{ String decryptedText=null;
137try {
138cipher.init(Cipher.DECRYPT_MODE, key);
139byte[] encryptedText = Base64.getDecoder().decode(encryptedString);
140byte[] plainText = cipher.doFinal(encryptedText);
141decryptedText=bytes2String(plainText); }
142catch (Exception e)
143{
144e.printStackTrace();
145}
146return decryptedText;
147}
148private static String bytes2String(byte[] bytes)
149{ StringBuffer stringBuffer = new StringBuffer();
150for (int i = 0; i <bytes.length;i++)
151{ stringBuffer.append((char) bytes[i]); }
152return stringBuffer.toString();
153}
154public static void main(String args []) throws Exception
155{
156DES myEncryptor= new DES();
157System.out.print("Enter the string: ");
158String stringToEncrypt = br.readLine();
159String encrypted = myEncryptor.encrypt(stringToEncrypt);
160String decrypted = myEncryptor.decrypt(encrypted);
161System.out.println("\nString To Encrypt: " +stringToEncrypt);
162System.out.println("\nEncrypted Value : " +encrypted);
163System.out.println("\nDecrypted Value : " +decrypted); System.out.println("");
164}
165}
166DES.java 06-02-2019 10:29
167Page 2 of 2