1 25 package org.ofbiz.base.crypto; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.security.NoSuchAlgorithmException ; 33 34 import javax.crypto.Cipher; 35 import javax.crypto.KeyGenerator; 36 import javax.crypto.SecretKey; 37 import javax.crypto.spec.SecretKeySpec; 38 39 46 public class BlowFishCrypt { 47 48 private SecretKeySpec secretKeySpec = null; 49 50 54 public BlowFishCrypt(SecretKeySpec secretKeySpec) { 55 this.secretKeySpec = secretKeySpec; 56 } 57 58 62 public BlowFishCrypt(byte[] key) { 63 try { 64 secretKeySpec = new SecretKeySpec(key, "Blowfish"); 65 } catch (Exception e) {} 66 } 67 68 72 public BlowFishCrypt(File keyFile) { 73 try { 74 FileInputStream is = new FileInputStream (keyFile); 75 ObjectInputStream os = new ObjectInputStream (is); 76 String keyString = (String ) os.readObject(); 77 78 is.close(); 79 80 byte[] keyBytes = keyString.getBytes(); 81 82 secretKeySpec = new SecretKeySpec(keyBytes, "Blowfish"); 83 } catch (Exception e) {} 84 } 85 86 90 public byte[] encrypt(String string) { 91 return encrypt(string.getBytes()); 92 } 93 94 98 public byte[] decrypt(String string) { 99 return decrypt(string.getBytes()); 100 } 101 102 106 public byte[] encrypt(byte[] bytes) { 107 byte[] resp = null; 108 109 try { 110 resp = crypt(bytes, Cipher.ENCRYPT_MODE); 111 } catch (Exception e) { 112 return null; 113 } 114 return resp; 115 } 116 117 121 public byte[] decrypt(byte[] bytes) { 122 byte[] resp = null; 123 124 try { 125 resp = crypt(bytes, Cipher.DECRYPT_MODE); 126 } catch (Exception e) { 127 return null; 128 } 129 return resp; 130 } 131 132 private byte[] crypt(byte[] bytes, int mode) throws Exception { 133 if (secretKeySpec == null) 134 throw new Exception ("SecretKey cannot be null."); 135 Cipher cipher = Cipher.getInstance("Blowfish"); 136 137 cipher.init(mode, secretKeySpec); 138 return cipher.doFinal(bytes); 139 } 140 141 public static byte[] generateKey() throws NoSuchAlgorithmException { 142 KeyGenerator keyGen = keyGen = KeyGenerator.getInstance("Blowfish"); 143 keyGen.init(448); 144 145 SecretKey secretKey = keyGen.generateKey(); 146 byte[] keyBytes = secretKey.getEncoded(); 147 148 return keyBytes; 149 } 150 151 public static boolean testKey(byte[] key) { 152 String testString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz"; 153 BlowFishCrypt c = new BlowFishCrypt(key); 154 byte[] encryptedBytes = c.encrypt(testString); 155 String encryptedMessage = new String (encryptedBytes); 156 157 byte[] decryptedBytes = c.decrypt(encryptedMessage); 158 String decryptedMessage = new String (decryptedBytes); 159 160 if (testString.equals(decryptedMessage)) { 161 return true; 162 } 163 164 return false; 165 } 166 167 public static void main(String args[]) throws Exception { 168 if (args[0] == null) { 169 args[0] = "ofbkey"; 170 } 171 172 byte[] key = generateKey(); 173 if (testKey(key)) { 174 FileOutputStream fos = new FileOutputStream (args[0]); 175 ObjectOutputStream os = new ObjectOutputStream (fos); 176 String keyString = new String (key); 177 os.writeObject(keyString); 178 fos.close(); 179 } 180 } 181 } 182 | Popular Tags |