1 25 package org.ofbiz.base.crypto; 26 27 import java.security.NoSuchAlgorithmException ; 28 import java.security.InvalidKeyException ; 29 import java.security.InvalidAlgorithmParameterException ; 30 import java.security.spec.InvalidKeySpecException ; 31 import javax.crypto.Cipher; 32 import javax.crypto.IllegalBlockSizeException; 33 import javax.crypto.BadPaddingException; 34 import javax.crypto.SecretKey; 35 import javax.crypto.NoSuchPaddingException; 36 import javax.crypto.KeyGenerator; 37 import javax.crypto.SecretKeyFactory; 38 import javax.crypto.spec.IvParameterSpec; 39 import javax.crypto.spec.DESedeKeySpec; 40 41 import org.ofbiz.base.util.GeneralException; 42 43 50 public class DesCrypt { 51 52 public static final String module = DesCrypt.class.getName(); 53 54 public static SecretKey generateKey() throws NoSuchAlgorithmException { 55 KeyGenerator keyGen = KeyGenerator.getInstance("DESede"); 56 57 return keyGen.generateKey(); 59 } 60 61 public static byte[] encrypt(SecretKey key, byte[] bytes) throws GeneralException { 62 Cipher cipher = DesCrypt.getCipher(key, Cipher.ENCRYPT_MODE); 63 byte[] encBytes = null; 64 try { 65 encBytes = cipher.doFinal(bytes); 66 } catch (IllegalStateException e) { 67 throw new GeneralException(e); 68 } catch (IllegalBlockSizeException e) { 69 throw new GeneralException(e); 70 } catch (BadPaddingException e) { 71 throw new GeneralException(e); 72 } 73 return encBytes; 74 } 75 76 public static byte[] decrypt(SecretKey key, byte[] bytes) throws GeneralException { 77 Cipher cipher = DesCrypt.getCipher(key, Cipher.DECRYPT_MODE); 78 byte[] decBytes = null; 79 try { 80 decBytes = cipher.doFinal(bytes); 81 } catch (IllegalStateException e) { 82 throw new GeneralException(e); 83 } catch (IllegalBlockSizeException e) { 84 throw new GeneralException(e); 85 } catch (BadPaddingException e) { 86 throw new GeneralException(e); 87 } 88 return decBytes; 89 } 90 91 public static SecretKey getDesKey(byte[] rawKey) throws GeneralException { 92 SecretKeyFactory skf = null; 93 try { 94 skf = SecretKeyFactory.getInstance("DESede"); 95 } catch (NoSuchAlgorithmException e) { 96 throw new GeneralException(e); 97 } 98 99 if (rawKey.length > 0) { 101 DESedeKeySpec desedeSpec1 = null; 102 try { 103 desedeSpec1 = new DESedeKeySpec(rawKey); 104 } catch (InvalidKeyException e) { 105 throw new GeneralException(e); 106 } 107 108 SecretKey key = null; 110 try { 111 key = skf.generateSecret(desedeSpec1); 112 } catch (InvalidKeySpecException e) { 113 throw new GeneralException(e); 114 } 115 return key; 116 } else { 117 throw new GeneralException("Not a valid DESede key!"); 118 } 119 } 120 121 protected static Cipher getCipher(SecretKey key, int mode) throws GeneralException { 123 byte[] zeros = { 0, 0, 0, 0, 0, 0, 0, 0 }; 124 IvParameterSpec iv = new IvParameterSpec(zeros); 125 126 Cipher encCipher = null; 128 try { 129 encCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 130 } catch (NoSuchAlgorithmException e) { 131 throw new GeneralException(e); 132 } catch (NoSuchPaddingException e) { 133 throw new GeneralException(e); 134 } 135 try { 136 encCipher.init(mode, key, iv); 137 } catch (InvalidKeyException e) { 138 throw new GeneralException(e); 139 } catch (InvalidAlgorithmParameterException e) { 140 throw new GeneralException(e); 141 } 142 return encCipher; 143 } 144 } 145 | Popular Tags |