· 7 years ago · May 26, 2018, 12:06 AM
1import javax.crypto.*;
2import java.util.Scanner;
3
4public class TEST {
5
6 public static void main(String[] args) {
7
8 try {
9
10 Scanner scan = new Scanner(System.in);
11
12 System.out.print("Enter the password or message: ");
13
14 String input = scan.nextLine();
15
16 KeyGenerator keyGen = KeyGenerator.getInstance("DES");
17 SecretKey desKey = keyGen.generateKey();
18
19 Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
20
21 desCipher.init(Cipher.ENCRYPT_MODE, desKey);
22
23 byte[] clearText = input.getBytes();
24 byte[] cipherText = desCipher.doFinal(clearText);
25
26 desCipher.init(Cipher.DECRYPT_MODE, desKey);
27
28 byte[] clearText1 = desCipher.doFinal(cipherText);
29
30 System.out.println("\n\nCLEAR TEXT: \n");
31
32 for(int i = 0; i < clearText.length; i++) {
33
34 System.out.print(clearText[i]);
35
36 }
37
38 System.out.println("\n\nCIPHER TEXT: \n");
39
40 for(int j = 0; j < cipherText.length; j++) {
41
42 System.out.print(cipherText[j]);
43
44 }
45
46 System.out.println("\n\nDECRYPTED TEXT: \n");
47
48 for(int k = 0; k < clearText1.length; k++) {
49
50 System.out.print(clearText1[k]);
51
52 }
53
54 System.out.println();
55
56 } catch (Exception ex) {
57
58 ex.printStackTrace();
59
60 }
61
62 }
63
64}