/zdal-datasource/src/main/java/com/alipay/zdal/datasource/resource/security/SecureIdentityLoginModule.java

https://github.com/xie-summer/zdal · Java · 156 lines · 122 code · 19 blank · 15 comment · 12 complexity · 4d96da37605520952da12a6516a18030 MD5 · raw file

  1. /**
  2. * Alipay.com Inc.
  3. * Copyright (c) 2004-2012 All Rights Reserved.
  4. */
  5. package com.alipay.zdal.datasource.resource.security;
  6. import java.math.BigInteger;
  7. import java.security.InvalidKeyException;
  8. import java.security.NoSuchAlgorithmException;
  9. import javax.crypto.BadPaddingException;
  10. import javax.crypto.Cipher;
  11. import javax.crypto.IllegalBlockSizeException;
  12. import javax.crypto.NoSuchPaddingException;
  13. import javax.crypto.spec.SecretKeySpec;
  14. /**
  15. * ¼ÓÃÜÄ£¿é
  16. *
  17. * @author liangjie.li
  18. * @version $Id: SecureIdentityLoginModule.java, v 0.1 2012-8-8 ÏÂÎç5:12:38 liangjie.li Exp $
  19. */
  20. public class SecureIdentityLoginModule {
  21. // dev key
  22. private static byte[] ENC_KEY_BYTES = "jaas is the way".getBytes();
  23. // prod key
  24. private static byte[] ENC_KEY_BYTES_PROD = "gQzLk5tTcGYlQ47GG29xQxfbHIURCheJ".getBytes();
  25. public static String encode(String secret) throws NoSuchPaddingException,
  26. NoSuchAlgorithmException, InvalidKeyException,
  27. BadPaddingException, IllegalBlockSizeException {
  28. return SecureIdentityLoginModule.encode(null, secret);
  29. }
  30. public static String encode(String encKey, String secret) throws InvalidKeyException,
  31. NoSuchAlgorithmException,
  32. NoSuchPaddingException,
  33. IllegalBlockSizeException,
  34. BadPaddingException {
  35. byte[] kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES_PROD;
  36. if (isNotBlank(encKey)) {
  37. kbytes = encKey.getBytes();
  38. }
  39. // ĬÈϲÉÓÃprod key¼ÓÃÜÓë½âÃÜ,ÏßÏ»·¾³»áÒì³£;
  40. try {
  41. return initEncode(kbytes, secret);
  42. } catch (InvalidKeyException e) {
  43. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  44. } catch (NoSuchAlgorithmException e) {
  45. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  46. } catch (NoSuchPaddingException e) {
  47. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  48. } catch (IllegalBlockSizeException e) {
  49. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  50. } catch (BadPaddingException e) {
  51. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  52. }
  53. return initEncode(kbytes, secret);
  54. }
  55. static final String initEncode(byte[] kbytes, String secret) throws NoSuchAlgorithmException,
  56. NoSuchPaddingException,
  57. InvalidKeyException,
  58. IllegalBlockSizeException,
  59. BadPaddingException {
  60. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  61. Cipher cipher = Cipher.getInstance("Blowfish");
  62. cipher.init(Cipher.ENCRYPT_MODE, key);
  63. byte[] encoding = cipher.doFinal(secret.getBytes());
  64. BigInteger n = new BigInteger(encoding);
  65. return n.toString(16);
  66. }
  67. public static char[] decode(String secret) throws NoSuchPaddingException,
  68. NoSuchAlgorithmException, InvalidKeyException,
  69. BadPaddingException, IllegalBlockSizeException {
  70. return SecureIdentityLoginModule.decode(null, secret).toCharArray();
  71. }
  72. public static String decode(String encKey, String secret) throws NoSuchPaddingException,
  73. NoSuchAlgorithmException,
  74. InvalidKeyException,
  75. BadPaddingException,
  76. IllegalBlockSizeException {
  77. byte[] kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES_PROD;
  78. if (isNotBlank(encKey)) {
  79. kbytes = encKey.getBytes();
  80. }
  81. try {
  82. return iniDecode(kbytes, secret);
  83. } catch (InvalidKeyException e) {
  84. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  85. } catch (BadPaddingException e) {
  86. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  87. } catch (IllegalBlockSizeException e) {
  88. kbytes = SecureIdentityLoginModule.ENC_KEY_BYTES;
  89. }
  90. return iniDecode(kbytes, secret);
  91. }
  92. static final String iniDecode(byte[] kbytes, String secret) throws NoSuchPaddingException,
  93. NoSuchAlgorithmException,
  94. InvalidKeyException,
  95. BadPaddingException,
  96. IllegalBlockSizeException {
  97. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  98. BigInteger n = new BigInteger(secret, 16);
  99. byte[] encoding = n.toByteArray();
  100. // SECURITY-344: fix leading zeros
  101. if (encoding.length % 8 != 0) {
  102. int length = encoding.length;
  103. int newLength = ((length / 8) + 1) * 8;
  104. int pad = newLength - length; //number of leading zeros
  105. byte[] old = encoding;
  106. encoding = new byte[newLength];
  107. for (int i = old.length - 1; i >= 0; i--) {
  108. encoding[i + pad] = old[i];
  109. }
  110. }
  111. Cipher cipher = Cipher.getInstance("Blowfish");
  112. cipher.init(Cipher.DECRYPT_MODE, key);
  113. byte[] decode = cipher.doFinal(encoding);
  114. return new String(decode);
  115. }
  116. static final boolean isNotBlank(String str) {
  117. return !isBlank(str);
  118. }
  119. static final boolean isBlank(String str) {
  120. int strLen = 0;
  121. if (str == null || (strLen = str.length()) == 0) {
  122. return true;
  123. }
  124. for (int i = 0; i < strLen; i++) {
  125. if ((Character.isWhitespace(str.charAt(i)) == false)) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException,
  132. NoSuchAlgorithmException, BadPaddingException,
  133. IllegalBlockSizeException {
  134. // System.out.println(encode("ali88"));
  135. System.out.println(decode("-19c84bf1dcbecee0917eaefd81d23fbf"));
  136. }
  137. }