· 7 years ago · Jan 13, 2019, 02:36 AM
1Manual implementation of 3DES (academic)
2KeyGenerator kgen = KeyGenerator.getInstance("DES");
3 SecretKey sk_1 = kgen.generateKey();
4 SecretKey sk_2 = kgen.generateKey();
5 byte[] raw_1 = sk_1.getEncoded();
6 byte[] raw_2 = sk_2.getEncoded();
7
8 spec_1 = new SecretKeySpec(raw_1, "DES"); //key 1
9 spec_2 = new SecretKeySpec(raw_2, "DES"); //key 2
10
11 cipher = Cipher.getInstance("DES"); //standard mode is ECB which is block-by-block w/PKCS5Padding
12 cipher2 = Cipher.getInstance("DES");
13
14
15 protected byte[] get3DESEncryption(byte[] plaintext) throws Exception{
16 byte[] output = new byte[plaintext.length];
17 System.out.println("output len init: " + output.length);
18 cipher.init(Cipher.ENCRYPT_MODE, spec_1);
19 cipher2.init(Cipher.DECRYPT_MODE, spec_2);
20
21 //first encryption round, key 1 used
22 output = cipher.doFinal(plaintext);
23 //second "encryption" round, key 2 used but decrypt run
24 output = cipher2.doFinal(output);
25 //third encryption round, key 1 used
26 output = cipher.doFinal(output);
27
28 //return ciphertext
29 return output;
30 }