bd328d602f53b5ca6ca4d182cb2aed78dd50259b.svn-base 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 EnDecHelper2 {
  7. private static final String ECB_MOB = "DES/ECB/PKCS5Padding";
  8. private static final String CHAESET_NAME = "UTF-8";
  9. private static final String SECRET_KEY_TYPE = "DES";
  10. public static String channel = "kechuang";
  11. public static String pwd = "!QAW2djxns";
  12. public static String encode(String data) throws Exception {
  13. Cipher enCipher = Cipher.getInstance(ECB_MOB);
  14. Key key = getKey(pwd);
  15. enCipher.init(Cipher.ENCRYPT_MODE, key);
  16. byte[] pasByte = enCipher.doFinal(data.getBytes(CHAESET_NAME));
  17. return Base64Util.getEncoder().encodeToString(pasByte);
  18. }
  19. //for vip sms
  20. public static String encodeByPwd(String data,String pwd) throws Exception {
  21. Cipher enCipher = Cipher.getInstance(ECB_MOB);
  22. Key key = getKey(pwd);
  23. enCipher.init(Cipher.ENCRYPT_MODE, key);
  24. byte[] pasByte = enCipher.doFinal(data.getBytes(CHAESET_NAME));
  25. return Base64Util.getEncoder().encodeToString(pasByte);
  26. }
  27. private static Key getKey(String password) throws Exception {
  28. byte[] DESkey = password.getBytes(CHAESET_NAME);
  29. DESKeySpec keySpec = new DESKeySpec(DESkey);
  30. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_TYPE);
  31. return keyFactory.generateSecret(keySpec);
  32. }
  33. public static String decode(String data) {
  34. try {
  35. Cipher deCipher = Cipher.getInstance(ECB_MOB);
  36. Key key = getKey(pwd);
  37. deCipher.init(Cipher.DECRYPT_MODE, key);
  38. byte[] pasByte = deCipher.doFinal(Base64Util.getDecoder().decode(data.getBytes(CHAESET_NAME)));
  39. return new String(pasByte, CHAESET_NAME);
  40. } catch (Exception e) {
  41. System.out.println("decode异常: {}"+ e.getMessage());
  42. return "";
  43. }
  44. }
  45. public static String decodeWithPwd(String data, String password) {
  46. try {
  47. Cipher deCipher = Cipher.getInstance(ECB_MOB);
  48. Key key = getKey(password);
  49. deCipher.init(Cipher.DECRYPT_MODE, key);
  50. byte[] pasByte = deCipher.doFinal(Base64Util.getDecoder().decode(data.getBytes(CHAESET_NAME)));
  51. return new String(pasByte, CHAESET_NAME);
  52. } catch (Exception e) {
  53. System.out.println("decode异常: {}"+ e.getMessage());
  54. return "";
  55. }
  56. }
  57. public static void main(String[] args) {
  58. try {
  59. System.out.println(EnDecHelper2.encodeByPwd("18673197465","!QAW2djxns"));
  60. } catch (Exception e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64. }
  65. }