· 6 years ago · Sep 09, 2019, 05:20 AM
1package main;
2
3import java.security.spec.*;
4import javax.crypto.*;
5import javax.crypto.spec.*;
6
7class DESTest {
8 public static void main(String[] args) {
9 String test = "2";
10 try {
11 byte[] theKey = null;
12 byte[] theMsg = null;
13 byte[] theExp = null;
14 // 4 vectores de prueba para encriptar
15 System.out.println("4 vectores de prueba para encriptar");
16 for (int i = 0; i < 4; i++) {
17 // Vector 1:
18 if (i == 0) {
19 System.out.println("vector 1");
20 theKey = hexToBytes("0101010101010101");
21 theMsg = hexToBytes("8000000000000000");
22 theExp = hexToBytes("95F8A5E5DD31D900");
23 }
24 System.out.println("");
25 // Vector 2:
26 if (i == 1) {
27 System.out.println("vector 2");
28 theKey = hexToBytes("0101010101010101"); // "8bytekey"
29 theMsg = hexToBytes("4000000000000000"); // "message."
30 theExp = hexToBytes("DD7F121CA5015619");
31 }
32 System.out.print("");
33 // Vector 3:
34 if (i == 2) {
35 System.out.println("vector 3");
36 theKey = hexToBytes("0101010101010101"); // "8bytekey"
37 theMsg = hexToBytes("2000000000000000"); // "message."
38 theExp = hexToBytes("2E8653104F3834EA");
39 }
40 System.out.print("");
41 // Vector 4:
42 if (i == 3) {
43 System.out.println("vector 4");
44 theKey = hexToBytes("0101010101010101"); // "8bytekey"
45 theMsg = hexToBytes("1000000000000000"); // "message."
46 theExp = hexToBytes("4BD388FF6CD81D4F");
47 }
48
49 KeySpec ks = new DESKeySpec(theKey);
50 SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
51 SecretKey ky = kf.generateSecret(ks);
52 Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
53 cf.init(Cipher.ENCRYPT_MODE, ky);
54 byte[] theCph = cf.doFinal(theMsg);
55 System.out.println("Key : " + bytesToHex(theKey));
56 System.out.println("Message : " + bytesToHex(theMsg));
57 System.out.println("Cipher : " + bytesToHex(theCph));
58 System.out.println("Expected: " + bytesToHex(theExp));
59 System.out.println("\n");
60 }
61 System.out.println("\n");
62 // 4 vectores de prueba para desncriptar
63 System.out.println("4 vectores de prueba para desencriptar");
64 for (int i = 0; i < 4; i++) {
65 // Vector 1:
66 if (i == 0) {
67 System.out.println("vector 1");
68 theKey = hexToBytes("0101010101010101");
69 theExp = hexToBytes("0000000000000001");
70 theMsg = hexToBytes("166B40B44ABA4BD6");
71 }
72 // Vector 2:
73 if (i == 1) {
74 System.out.println("vector 2");
75 theKey = hexToBytes("0101010101010101"); // "8bytekey"
76 theExp = hexToBytes("0000000000000002");
77 theMsg = hexToBytes("06E7EA22CE92708F");
78 }
79 // Vector 3:
80 if (i == 2) {
81 System.out.println("vector 3");
82 theKey = hexToBytes("0101010101010101"); // "8bytekey"
83 theExp = hexToBytes("0000000000000004");
84 theMsg = hexToBytes("D2FD8867D50D2DFE");
85 }
86 // Vector 4:
87 if (i == 3) {
88 System.out.println("vector 4");
89 theKey = hexToBytes("0101010101010101"); // "8bytekey"
90 theExp = hexToBytes("0000000000000008");
91 theMsg = hexToBytes("CC083F1E6D9E85F6");
92 }
93
94 KeySpec ks = new DESKeySpec(theKey);
95 SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
96 SecretKey ky = kf.generateSecret(ks);
97 Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
98 cf.init(Cipher.DECRYPT_MODE, ky);
99 byte[] theCph = cf.doFinal(theMsg);
100 System.out.println("Key : " + bytesToHex(theKey));
101 System.out.println("Message : " + bytesToHex(theMsg));
102 System.out.println("Cipher : " + bytesToHex(theCph));
103 System.out.println("Expected: " + bytesToHex(theExp));
104 System.out.println("\n");
105 }
106
107 } catch (Exception e) {
108 e.printStackTrace();
109 return;
110 }
111 }
112
113 public static byte[] hexToBytes(String str) {
114 if (str == null) {
115 return null;
116 } else if (str.length() < 2) {
117 return null;
118 } else {
119 int len = str.length() / 2;
120 byte[] buffer = new byte[len];
121 for (int i = 0; i < len; i++) {
122 buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
123 }
124 return buffer;
125 }
126
127 }
128
129 public static String bytesToHex(byte[] data) {
130 if (data == null) {
131 return null;
132 } else {
133 int len = data.length;
134 String str = "";
135 for (int i = 0; i < len; i++) {
136 if ((data[i] & 0xFF) < 16)
137 str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
138 else
139 str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
140 }
141 return str.toUpperCase();
142 }
143 }
144}