1 package com.explosion.utilities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.security.MessageDigest;
24
25 import org.apache.log4j.LogManager;
26 import org.apache.log4j.Logger;
27
28 import xjava.security.Cipher;
29 import xjava.security.SecretKey;
30 import cryptix.provider.key.DESKeyGenerator;
31 import cryptix.provider.key.RawSecretKey;
32 import cryptix.util.core.Hex;
33
34 /***
35 * @author Stephen Cowx
36 * @version
37 */
38
39 public class CryptoUtils
40 {
41 private static Logger log = LogManager.getLogger(CryptoUtils.class);
42 private final static byte[] salt = { 5, 19, 8, 24, 3, 10, 8, 21, 17, 5, 1};
43
44 /***
45 * This method returns a String which is a BCD representation of the text
46 * which has been hashed using an SHA encryption algorithm. This is a
47 * non-reversible operation
48 */
49 public static String getSHAEncoded(String text) throws Exception
50 {
51 if (text == null || text.length() < 1) return "";
52
53
54 MessageDigest md = MessageDigest.getInstance("SHA");
55 byte[] intermediateHash = md.digest(text.getBytes());
56
57
58
59 md.update(intermediateHash);
60 return new String(ByteUtils.getCharsFromBCD(md.digest(salt), false, false));
61 }
62
63 /***
64 * This method encrypts the string given to it.
65 */
66 public static String encrypt(String valueToEncrypt, byte[] privateKeyBytes) throws Exception
67 {
68
69 java.security.Security.addProvider(new cryptix.provider.Cryptix());
70
71
72 RawSecretKey key = new RawSecretKey("DES", privateKeyBytes);
73
74
75 Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding", "Cryptix");
76 encrypt.initEncrypt(key);
77
78 return Hex.toString(encrypt.doFinal(valueToEncrypt.getBytes()));
79 }
80
81 /***
82 * This method decrypts a string given to it.
83 */
84 public static String decrypt(String valueToDecrypt, byte[] privateKeyBytes) throws Exception
85 {
86
87 java.security.Security.addProvider(new cryptix.provider.Cryptix());
88
89
90 RawSecretKey key = new RawSecretKey("DES", privateKeyBytes);
91
92
93 Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding", "Cryptix");
94 decrypt.initDecrypt(key);
95 String decryptedText = new String(decrypt.doFinal(Hex.fromString(valueToDecrypt)));
96
97 return decryptedText;
98 }
99
100 /***
101 * This method changes a Hex String private key into an array of bytes.
102 */
103 public static byte[] resolveToKey(String keyHexVersion) throws Exception
104 {
105 return Hex.fromString(keyHexVersion);
106 }
107
108 /***
109 * This method constructs a PrivateKey and returns it as a hex string
110 */
111 public static String generatePrivateKey() throws Exception
112 {
113 byte[] bytes = generatePrivateKeyBytes();
114 return Hex.toString(bytes);
115 }
116
117 /***
118 * This method constructs a PrivateKey
119 */
120 private static byte[] generatePrivateKeyBytes() throws Exception
121 {
122 DESKeyGenerator generator = new DESKeyGenerator();
123 SecretKey key = generator.generateKey();
124 return key.getEncoded();
125 }
126
127 public static void main(String[] args)
128 {
129 try
130 {
131 String txt = "Hi I went to my grandmothers one day.";
132 String key = generatePrivateKey();
133 byte[] keyBytes = CryptoUtils.resolveToKey(key);
134
135 String encrypted = CryptoUtils.encrypt(txt, keyBytes);
136 String decrypted = CryptoUtils.decrypt(encrypted, keyBytes);
137
138 log.debug("txt :" + txt);
139 log.debug("key :" + key);
140 log.debug("encrypted :" + encrypted);
141 log.debug("decrypted :" + decrypted);
142
143 log.debug(generatePrivateKey());
144 } catch (Exception e)
145 {
146 e.printStackTrace();
147 }
148 }
149 }