· 7 years ago · Jun 30, 2018, 03:08 AM
1public class TripleDESTest {
2
3public static void main(String[] args) {
4
5 String text = "kyle boon";
6
7 byte[] codedtext = new TripleDESTest().encrypt(text);
8 String decodedtext = new TripleDESTest().decrypt(codedtext);
9
10 System.out.println(codedtext);
11 System.out.println(decodedtext);
12}
13
14public byte[] encrypt(String message)
15{
16 try
17 {
18 final MessageDigest md = MessageDigest.getInstance("md5");
19 final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
20 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
21 for (int j = 0, k = 16; j < 8;)
22 {
23 keyBytes[k++] = keyBytes[j++];
24 }
25
26 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
27 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
28 final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
29 cipher.init(Cipher.ENCRYPT_MODE, key, iv);
30
31 final byte[] plainTextBytes = message.getBytes("utf-8");
32 final byte[] cipherText = cipher.doFinal(plainTextBytes);
33 final String encodedCipherText = new sun.misc.BASE64Encoder().encode(cipherText);
34
35 return cipherText;
36 }
37 catch (java.security.InvalidAlgorithmParameterException e) { System.out.println("Invalid Algorithm"); }
38 catch (javax.crypto.NoSuchPaddingException e) { System.out.println("No Such Padding"); }
39 catch (java.security.NoSuchAlgorithmException e) { System.out.println("No Such Algorithm"); }
40 catch (java.security.InvalidKeyException e) { System.out.println("Invalid Key"); }
41 catch (BadPaddingException e) { System.out.println("Invalid Key");}
42 catch (IllegalBlockSizeException e) { System.out.println("Invalid Key");}
43 catch (UnsupportedEncodingException e) { System.out.println("Invalid Key");}
44
45 return null;
46}
47
48public String decrypt(byte[] message)
49{
50 try
51 {
52 final MessageDigest md = MessageDigest.getInstance("md5");
53 final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
54 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
55 for (int j = 0, k = 16; j < 8;)
56 {
57 keyBytes[k++] = keyBytes[j++];
58 }
59
60 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
61 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
62 final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
63 decipher.init(Cipher.DECRYPT_MODE, key, iv);
64
65 //final byte[] encData = new sun.misc.BASE64Decoder().decodeBuffer(message);
66 final byte[] plainText = decipher.doFinal(message);
67
68 return plainText.toString();
69 }
70 catch (java.security.InvalidAlgorithmParameterException e) { System.out.println("Invalid Algorithm"); }
71 catch (javax.crypto.NoSuchPaddingException e) { System.out.println("No Such Padding"); }
72 catch (java.security.NoSuchAlgorithmException e) { System.out.println("No Such Algorithm"); }
73 catch (java.security.InvalidKeyException e) { System.out.println("Invalid Key"); }
74 catch (BadPaddingException e) { System.out.println("Invalid Key");}
75 catch (IllegalBlockSizeException e) { System.out.println("Invalid Key");}
76 catch (UnsupportedEncodingException e) { System.out.println("Invalid Key");} catch (IOException e) {
77 // TODO Auto-generated catch block
78 e.printStackTrace();
79 }
80
81 return null;
82}
83
84import java.security.MessageDigest;
85import java.util.Arrays;
86
87import javax.crypto.Cipher;
88import javax.crypto.SecretKey;
89import javax.crypto.spec.IvParameterSpec;
90import javax.crypto.spec.SecretKeySpec;
91
92public class TripleDESTest {
93
94 public static void main(String[] args) throws Exception {
95
96 String text = "kyle boon";
97
98 byte[] codedtext = new TripleDESTest().encrypt(text);
99 String decodedtext = new TripleDESTest().decrypt(codedtext);
100
101 System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array
102 System.out.println(decodedtext); // This correctly shows "kyle boon"
103 }
104
105 public byte[] encrypt(String message) throws Exception {
106 final MessageDigest md = MessageDigest.getInstance("md5");
107 final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
108 .getBytes("utf-8"));
109 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
110 for (int j = 0, k = 16; j < 8;) {
111 keyBytes[k++] = keyBytes[j++];
112 }
113
114 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
115 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
116 final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
117 cipher.init(Cipher.ENCRYPT_MODE, key, iv);
118
119 final byte[] plainTextBytes = message.getBytes("utf-8");
120 final byte[] cipherText = cipher.doFinal(plainTextBytes);
121 // final String encodedCipherText = new sun.misc.BASE64Encoder()
122 // .encode(cipherText);
123
124 return cipherText;
125 }
126
127 public String decrypt(byte[] message) throws Exception {
128 final MessageDigest md = MessageDigest.getInstance("md5");
129 final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
130 .getBytes("utf-8"));
131 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
132 for (int j = 0, k = 16; j < 8;) {
133 keyBytes[k++] = keyBytes[j++];
134 }
135
136 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
137 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
138 final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
139 decipher.init(Cipher.DECRYPT_MODE, key, iv);
140
141 // final byte[] encData = new
142 // sun.misc.BASE64Decoder().decodeBuffer(message);
143 final byte[] plainText = decipher.doFinal(message);
144
145 return new String(plainText, "UTF-8");
146 }
147}