View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.crypto;
22  
23  /**
24   * This class handles methods to crypt and decrypt messages with Blowfish (efficient: 4000/s both at 56 and 128 keysize).<br>
25   * <br>
26   * Usage:<br>
27   * <ul>
28   * <li>Create a Blowfish object: Blowfish key = new Blowfish();</li>
29   * <li>Create a key:
30   * <ul>
31   * <li>Generate: key.generateKey();<br>
32   * The method key.getSecretKeyInBytes() allow getting the key in Bytes.</li>
33   * <li>From an external source: key.setSecretKey(arrayOfBytes);</li>
34   * </ul></li>
35   * <li>To crypt a String in a Base64 format: String myStringCrypt = key.cryptToString(myString);</li>
36   * <li>To decrypt one string from Base64 format to the original String: String myStringDecrypt = key.decryptStringInString(myStringCrypte);</li>
37   * </ul>
38   *
39   *
40   * @author frederic bregier
41   */
42  public class Blowfish extends KeyObject {
43      /**
44       * This value could be between 32 and 448. But it seems it is blocked up to 128.
45       */
46      public final static int KEY_SIZE = 56; // [32..448]
47      public final static String ALGO = "Blowfish";
48      public final static String INSTANCE = "Blowfish";
49      public final static String EXTENSION = "blf";
50  
51  
52      /* (non-Javadoc)
53       * @see atlas.cryptage.KeyObject#getAlgorithm()
54       */
55      @Override
56      public String getAlgorithm() {
57          return "Blowfish";
58      }
59  
60      /* (non-Javadoc)
61       * @see atlas.cryptage.KeyObject#getInstance()
62       */
63      @Override
64      public String getInstance() {
65          return "Blowfish";
66      }
67  
68      /* (non-Javadoc)
69       * @see atlas.cryptage.KeyObject#getKeySize()
70       */
71      @Override
72      public int getKeySize() {
73          return KEY_SIZE;
74      }
75  
76      /**
77       * This method allows to test the correctness of this class
78       *
79       * @param args
80       * @throws Exception
81       */
82      public static void main(String[] args) throws Exception {
83          String plaintext = null;
84          if (args.length != 0) {
85              plaintext = args[0];
86          }
87          if (plaintext == null || plaintext.length() == 0) {
88              plaintext = "This is a try for a very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long String";
89          }
90          System.out.println("plaintext = " + plaintext);
91          Blowfish bf = new Blowfish();
92          // Generate a key
93          bf.generateKey();
94          // get the generated key
95          byte[] secretKey = bf.getSecretKeyInBytes();
96          // crypt one text
97          byte[] ciphertext = bf.crypt(plaintext);
98          // print the cipher
99          System.out.println(ciphertext.length);
100         System.out.println("ciphertext = " + bf.encodeHex(ciphertext));
101 
102         // Test the set Key
103         bf.setSecretKey(secretKey);
104         // decrypt the cipher
105         String plaintext2 = bf.decryptInString(ciphertext);
106         // print the result
107         System.out.println("plaintext2 = " + plaintext2);
108         if (!plaintext2.equals(plaintext))
109             System.out.println("Error: plaintext2 != plaintext");
110 
111         // same on String only
112         int nb = 100000;
113         long time1 = System.currentTimeMillis();
114         for (int i = 0; i < nb ; i++) {
115             String cipherString = bf.cryptToHex(plaintext);
116             //System.out.println("cipherString = " + cipherString);
117             String plaintext3 = bf.decryptHexInString(cipherString);
118             //System.out.println("plaintext3 = " + plaintext3);
119             if (!plaintext3.equals(plaintext))
120                 System.out.println("Error: plaintext3 != plaintext");
121         }
122         long time2 = System.currentTimeMillis();
123         System.out.println("Total time in ms: "+(time2-time1)+" or "+(nb*1000/(time2-time1))+" crypt or decrypt/s");
124     }
125 
126 }