30595c11b41ee37bb9a16d36009abc8aeaf55083.svn-base 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cn.com.goldenwater.dcproj.utils;
  2. import org.springframework.util.Base64Utils;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.SecretKey;
  6. import java.net.URLDecoder;
  7. import java.nio.charset.StandardCharsets;
  8. import java.security.SecureRandom;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * @author rf
  13. * @version 1.0
  14. * @date 2021-1-15 15:16
  15. */
  16. public class DecryptUtil {
  17. private final static String KEY = "}a?7_sa@KXE>NV%@";
  18. public static Map<String, String> decrypt(String accessId) throws Exception {
  19. final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  20. SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  21. random.setSeed(KEY.getBytes());
  22. keyGenerator.init(128, random);
  23. final SecretKey secretKey = keyGenerator.generateKey();
  24. final Cipher cipher = Cipher.getInstance("AES");
  25. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  26. final String decode = new String(cipher.doFinal(Base64Utils.decodeFromString(URLDecoder.decode(accessId, "UTF-8"))), StandardCharsets.UTF_8);
  27. final String[] infos = decode.split("\\|");
  28. String userLoginName = infos[0];
  29. String userGuid = infos[1];
  30. String userMobile = infos[2];
  31. String timestamp = infos[3];
  32. if (Long.parseLong(timestamp) - System.currentTimeMillis() > 30 * 1000)
  33. throw new Exception("已过期");
  34. HashMap<String, String> res = new HashMap<>();
  35. res.put("userLoginName", userLoginName);
  36. res.put("userGuid", userGuid);
  37. res.put("userMobile", userMobile);
  38. return res;
  39. }
  40. }