efa5a51146803f8b0ca87fbf400f608dbb0c8b56.svn-base 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.chinacreator.process.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKeyFactory;
  4. import javax.crypto.spec.DESKeySpec;
  5. import java.security.Key;
  6. public class DesUtil {
  7. private static final String SECRET_KEY_TYPE = "DES";
  8. private static final String ECB_MOB = "DES/ECB/PKCS5Padding";
  9. private static final String CHAESET_NAME = "UTF-8";
  10. private static Key getKey(String password) throws Exception{
  11. byte[] DESkey = password.getBytes(CHAESET_NAME);
  12. DESKeySpec keySpec = new DESKeySpec(DESkey);
  13. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_TYPE);
  14. return keyFactory.generateSecret(keySpec);
  15. }
  16. public static String encode(String data, String password) throws Exception {
  17. Cipher enCipher = Cipher.getInstance(ECB_MOB);
  18. Key key = getKey(password);
  19. enCipher.init(Cipher.ENCRYPT_MODE, key);
  20. byte[] pasByte = enCipher.doFinal(data.getBytes(CHAESET_NAME));
  21. return Base64.encodeBase64String(pasByte);
  22. }
  23. public static String decode(String data, String password) throws Exception {
  24. Cipher deCipher = Cipher.getInstance(ECB_MOB);
  25. Key key = getKey(password);
  26. deCipher.init(Cipher.DECRYPT_MODE, key);
  27. byte[] pasByte = deCipher.doFinal(Base64.decodeBase64(data.getBytes(CHAESET_NAME)));
  28. return new String(pasByte, CHAESET_NAME);
  29. }
  30. }