View Javadoc

1   package com.explosion.utilities;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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          // first calculate the password digest...
54          MessageDigest md = MessageDigest.getInstance("SHA");
55          byte[] intermediateHash = md.digest(text.getBytes());
56  
57          // then combine the digest with the salt, and calculate the digest of
58          // that...
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          /* Add the provider */
69          java.security.Security.addProvider(new cryptix.provider.Cryptix());
70  
71          /* Create a raw secret key */
72          RawSecretKey key = new RawSecretKey("DES", privateKeyBytes);
73  
74          /* Encrypt */
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          /* Add the provider */
87          java.security.Security.addProvider(new cryptix.provider.Cryptix());
88  
89          /* Create a raw secret key */
90          RawSecretKey key = new RawSecretKey("DES", privateKeyBytes);
91  
92          /* Decrypt */
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 }