· 7 years ago · Nov 05, 2018, 07:00 PM
1package com.bristol.usm.sso;
2
3/*
4 * Created on Oct 28, 2008
5 *
6 * To change the template for this generated file go to
7 * Window>Preferences>Java>Code Generation>Code and Comments
8 */
9
10
11import java.security.Key;
12import java.security.Provider;
13import java.security.Security;
14import java.util.Iterator;
15
16import javax.crypto.Cipher;
17import javax.crypto.KeyGenerator;
18import javax.crypto.SecretKey;
19import javax.crypto.SecretKeyFactory;
20import javax.crypto.spec.DESedeKeySpec;
21
22import org.apache.xerces.impl.dv.util.Base64;
23
24/**
25 * @author hardiv
26 *
27 * This class can be used to generate key and encrypt/decrypt text base on DESede (triple DES)
28 */
29public class Cryptographer {
30 private String provider = "IBMJCE"; //default provider
31 private static final String algorithm = "DESede";
32 private static final String encryptType = "/ECB/PKCS5Padding";
33
34 /**
35 * constructor
36 *
37 */
38 public Cryptographer() {
39 // nothing
40 }
41
42 /**
43 * constructor to set new provider
44 *
45 * @param provider
46 */
47 public Cryptographer(String provider) {
48 this.provider = provider;
49 }
50
51
52 /**
53 * encrypt the string with the key
54 *
55 * @param source
56 * @param key
57 */
58 public String encrypt(String source, String key) {
59 try {
60 // Get our secret key
61 Key thekey = getDESedeKey(key);
62
63 // Create the cipher
64 Cipher cipher =
65 Cipher.getInstance(algorithm + encryptType, provider);
66
67 // Initialize the cipher for encryption
68 cipher.init(Cipher.ENCRYPT_MODE, thekey);
69
70 // Our cleartext as bytes
71 byte[] cleartext = source.getBytes();
72
73 // Encrypt the cleartext
74 byte[] ciphertext = cipher.doFinal(cleartext);
75
76 // Return a String representation of the cipher text
77 return getString(ciphertext);
78 } catch (Exception e) {
79 // handle the error log here
80 e.printStackTrace();
81 return null;
82 }
83 }
84
85 /**
86 * decrypt the data with the key
87 *
88 * @param data
89 * @param key
90 */
91 public String decrypt(String data, String key) {
92 try {
93 // Get our secret key
94 Key thekey = getDESedeKey(key);
95
96 // Create the cipher
97 Cipher cipher = Cipher.getInstance(algorithm + encryptType, provider);
98
99 // Initialize the same cipher for decryption
100 cipher.init(Cipher.DECRYPT_MODE, thekey);
101
102 // Encrypt the cleartext
103 byte[] ciphertext = getBytes(data);
104
105 // Decrypt the ciphertext
106 byte[] cleartext = cipher.doFinal(ciphertext);
107
108 // Return the clear text
109 return new String(cleartext);
110 } catch (Exception e) {
111 // handle the error log here
112 e.printStackTrace();
113 return null;
114 }
115 }
116
117 /**
118 * method to display all providers info in the JRE
119 *
120 */
121 public void showProviders() {
122 try {
123 Provider[] providers = Security.getProviders();
124 for (int i = 0; i < providers.length; i++) {
125 System.out.println(
126 "Provider: "
127 + providers[i].getName()
128 + ", "
129 + providers[i].getInfo());
130 for (Iterator itr = providers[i].keySet().iterator();itr.hasNext();) {
131 String key = (String) itr.next();
132 String value = (String) providers[i].get(key);
133 System.out.println("\t" + key + " = " + value);
134 }
135
136 }
137 } catch (Exception e) {
138 // handle the error log here
139 e.printStackTrace();
140 }
141
142 }
143
144 /**
145 * base64 endocded the string from byte[]
146 *
147 * @param data
148 * @return
149 * @throws Exception
150 */
151 private String getString(byte[] data) throws Exception {
152 return new String(Base64.encode(data));
153
154 }
155
156 /**
157 * base64 decoded the string to byte[]
158 *
159 * @param data
160 * @return
161 * @throws Exception
162 */
163 private byte[] getBytes(String data) throws Exception {
164 return Base64.decode(data);
165 }
166
167 /**
168 * recreate the key to the DESedeKey
169 *
170 * @param key
171 * @return
172 */
173 private Key getDESedeKey(String key) {
174 try {
175 byte[] bytes = getBytes(key);
176 DESedeKeySpec pass = new DESedeKeySpec(bytes);
177 SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
178 SecretKey s = skf.generateSecret(pass);
179 return s;
180 } catch (Exception e) {
181 e.printStackTrace();
182 }
183 return null;
184 }
185
186 public static void main (String args[])throws Exception {
187 Cryptographer test = new Cryptographer();
188 String data = "ZcgYeBMwX7ebSd9xTOAwa7055ivhInk9Y49qBDX4DRJC5s5/nMwyKKOLqbrQqRJVCPRQTbCEIBP8"+
189 "ahZ2IzjaMcpkPvmtnXKtPZkVP3CtfACIjBd6eM2NLddwRF9IQbuVn9czBg+SuOJj3mosqgTnytb6"+
190 "6k2LPtlwvsrloPw9a9p1i414FP9k/LLp/CYX/dzxHq9HW38mChu3GYsD/+P1RDkC7Qotkpt+nuYM"+
191 "KXsq7da53vpqlhQH/GxJzhtR1UpMPAVeEw/ro0flUgclzL3j9cztNcJpTiVbLghNLT8JQJvM7TXC"+
192 "aU4lW52Mpl0k1knMTqtKqKbZ4vZdEj4brA1Yc3IxTw6Z+qiSi07YOu9KWJtIGX6nmZp3hLg/PFDe"+
193 "uZ5GXnIOCFA+0r10fMPTDLBDetukDWsUUxBsob5ehlIm72R6WXfYsr8J3VuKTbQgJZqXQ8oS0e6e"+
194 "/x2jimbshCCWA99E7dVhDOlQDmrV6A0swn0j44QcYz3VdbNLA4krjQLzFx0VW2jA9Ak=";
195 String key = "fCprkfiuzsFhT7xRMf1DhXwqa5H4rs7B";
196 //System.out.println(test.decrypt(data, key));
197
198
199 }
200}