Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
156 | jmachado | 1 | package jomm.utils; |
2 | |||
3 | import javax.crypto.Cipher; |
||
1827 | jmachado | 4 | import javax.crypto.IllegalBlockSizeException; |
156 | jmachado | 5 | import javax.crypto.SecretKey; |
6 | import javax.crypto.spec.SecretKeySpec; |
||
7 | import java.io.UnsupportedEncodingException; |
||
8 | |||
9 | /** |
||
10 | * @author Jorge Machado |
||
11 | * @date 20/Mai/2008 |
||
12 | * @see jomm.utils |
||
13 | */ |
||
14 | public class DesUtils |
||
15 | { |
||
16 | static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher ecipher; |
||
17 | static 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher dcipher; |
||
18 | |||
19 | static 1.5.0/docs/api/javax/crypto/SecretKey.html">SecretKey key; |
||
20 | |||
21 | |||
22 | public static byte[] addParity(byte[] in) { |
||
23 | byte[] result = new byte[8]; |
||
24 | |||
25 | // Keeps track of the bit position in the result |
||
26 | int resultIx = 1; |
||
27 | |||
28 | // Used to keep track of the number of 1 bits in each 7-bit chunk |
||
29 | int bitCount = 0; |
||
30 | |||
31 | // Process each of the 56 bits |
||
32 | for (int i=0; i<56; i++) { |
||
33 | // Get the bit at bit position i |
||
34 | boolean bit = (in[6-i/8]&(1<<(i%8))) > 0; |
||
35 | |||
36 | // If set, set the corresponding bit in the result |
||
37 | if (bit) { |
||
38 | result[7-resultIx/8] |= (1<<(resultIx%8))&0xFF; |
||
39 | bitCount++; |
||
40 | } |
||
41 | |||
42 | // Set the parity bit after every 7 bits |
||
43 | if ((i+1) % 7 == 0) { |
||
44 | if (bitCount % 2 == 0) { |
||
45 | // Set low-order bit (parity bit) if bit count is even |
||
46 | result[7-resultIx/8] |= 1; |
||
47 | } |
||
48 | resultIx++; |
||
49 | bitCount = 0; |
||
50 | } |
||
51 | resultIx++; |
||
52 | } |
||
53 | return result; |
||
54 | } |
||
55 | |||
56 | // Get the 56-bit value |
||
57 | |||
58 | |||
59 | private static DesUtils desUtils; |
||
60 | |||
61 | private DesUtils() |
||
62 | { |
||
63 | try { |
||
64 | byte[] raw = new byte[]{0x01, 0x72, 0x43, 0x3E, 0x1C, 0x7A, 0x55}; |
||
65 | byte[] keyBytes = addParity(raw); |
||
66 | key = new 1.5.0/docs/api/javax/crypto/spec/SecretKeySpec.html">SecretKeySpec(keyBytes, "DES"); |
||
67 | ecipher = 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.getInstance("DES"); |
||
68 | dcipher = 1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.getInstance("DES"); |
||
69 | ecipher.init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.ENCRYPT_MODE, key); |
||
70 | dcipher.init(1.5.0/docs/api/javax/crypto/Cipher.html">Cipher.DECRYPT_MODE, key); |
||
71 | |||
72 | } catch (javax.crypto.1.5.0/docs/api/javax/crypto/NoSuchPaddingException.html">NoSuchPaddingException e) { |
||
73 | e.printStackTrace(); |
||
74 | } catch (java.security.1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) { |
||
75 | e.printStackTrace(); |
||
76 | } catch (java.security.5+0%2Fdocs%2Fapi+InvalidKeyException">InvalidKeyException e) { |
||
77 | e.printStackTrace(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | public static DesUtils getInstance() |
||
82 | { |
||
83 | if(desUtils == null) |
||
84 | desUtils = new DesUtils(); |
||
85 | return desUtils; |
||
86 | } |
||
87 | |||
1827 | jmachado | 88 | |
89 | /*public String encrypt(String str) { |
||
156 | jmachado | 90 | try { |
91 | // Encode the string into bytes using utf-8 |
||
92 | byte[] utf8 = str.getBytes("UTF8"); |
||
93 | |||
94 | // Encrypt |
||
95 | byte[] enc = ecipher.doFinal(utf8); |
||
96 | |||
97 | // Encode bytes to base64 to get a string |
||
98 | return new sun.misc.BASE64Encoder().encode(enc); |
||
99 | } catch (javax.crypto.BadPaddingException e) { |
||
100 | } catch (IllegalBlockSizeException e) { |
||
101 | } catch (UnsupportedEncodingException e) { |
||
102 | } catch (java.io.IOException e) { |
||
103 | } |
||
104 | return null; |
||
1827 | jmachado | 105 | }*/ |
106 | |||
107 | /** |
||
108 | * only for JAVA8 |
||
109 | * @param str |
||
110 | * @return |
||
111 | */ |
||
112 | public 1.5.0/docs/api/java/lang/String.html">String encrypt(1.5.0/docs/api/java/lang/String.html">String str) { |
||
113 | try { |
||
114 | // Encode the string into bytes using utf-8 |
||
115 | byte[] utf8 = str.getBytes("UTF8"); |
||
116 | |||
117 | // Encrypt |
||
118 | byte[] enc = ecipher.doFinal(utf8); |
||
119 | |||
120 | // Encode bytes to base64 to get a string |
||
121 | return java.util.Base64.getEncoder().encodeToString(enc); |
||
122 | } catch (javax.crypto.1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e) { |
||
123 | } catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e) { |
||
124 | } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) { |
||
125 | } catch (java.io.1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
126 | } |
||
127 | return null; |
||
156 | jmachado | 128 | } |
129 | |||
1827 | jmachado | 130 | /* public String decrypt(String str) { |
156 | jmachado | 131 | try { |
132 | // Decode base64 to get bytes |
||
133 | byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); |
||
134 | |||
135 | // Decrypt |
||
136 | byte[] utf8 = dcipher.doFinal(dec); |
||
137 | |||
138 | // Decode using utf-8 |
||
139 | return new String(utf8, "UTF8"); |
||
140 | } catch (javax.crypto.BadPaddingException e) { |
||
141 | } catch (IllegalBlockSizeException e) { |
||
142 | } catch (UnsupportedEncodingException e) { |
||
143 | } catch (java.io.IOException e) { |
||
144 | } |
||
145 | return null; |
||
1827 | jmachado | 146 | }*/ |
147 | |||
148 | /** |
||
149 | * only JAVA 8 |
||
150 | * @param str |
||
151 | * @return |
||
152 | */ |
||
153 | public 1.5.0/docs/api/java/lang/String.html">String decrypt(1.5.0/docs/api/java/lang/String.html">String str) { |
||
154 | try { |
||
155 | // Decode base64 to get bytes |
||
156 | byte[] dec = java.util.Base64.getDecoder().decode(str); |
||
157 | |||
158 | // Decrypt |
||
159 | byte[] utf8 = dcipher.doFinal(dec); |
||
160 | |||
161 | // Decode using utf-8 |
||
162 | return new 1.5.0/docs/api/java/lang/String.html">String(utf8, "UTF8"); |
||
163 | } catch (javax.crypto.1.5.0/docs/api/javax/crypto/BadPaddingException.html">BadPaddingException e) { |
||
164 | } catch (1.5.0/docs/api/javax/crypto/IllegalBlockSizeException.html">IllegalBlockSizeException e) { |
||
165 | } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) { |
||
166 | } catch (java.io.1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
167 | } |
||
168 | return null; |
||
156 | jmachado | 169 | } |
170 | |||
171 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) |
||
172 | { |
||
173 | 1.5.0/docs/api/java/lang/String.html">String code = DesUtils.getInstance().encrypt("jorge machado"); |
||
174 | 1.5.0/docs/api/java/lang/System.html">System.out.println(code); |
||
175 | 1.5.0/docs/api/java/lang/System.html">System.out.println(DesUtils.getInstance().decrypt(code)); |
||
176 | } |
||
177 | } |