package ca.canuckcoding.utils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /*import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;*/ public class DESEncrypter { Cipher ecipher; Cipher dcipher; public DESEncrypter(String key) { try { DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secret = keyFactory.generateSecret(keySpec); ecipher = Cipher.getInstance("DES"); dcipher = Cipher.getInstance("DES"); ecipher.init(Cipher.ENCRYPT_MODE, secret); dcipher.init(Cipher.DECRYPT_MODE, secret); } catch (NoSuchPaddingException e) { } catch (InvalidKeySpecException e) { } catch (IOException e) { } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } } public String encrypt(String str) { try { // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes("UTF8"); // Encrypt byte[] enc = ecipher.doFinal(utf8); // Encode bytes to base64 to get a string return new Base64().encode(enc); } catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return null; } public String decrypt(String str) { try { // Decode base64 to get bytes byte[] dec = new Base64().decode(str); // Decrypt byte[] utf8 = dcipher.doFinal(dec); // Decode using utf-8 return new String(utf8, "UTF8"); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return null; } }