EncryptionUtil.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.repair.common.utils;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class EncryptionUtil {
  5. private static final int MAX_JSON_STRING = 40;
  6. /**
  7. * 将需要加密的字段分批加密
  8. *
  9. * @param jsonString
  10. * @param publicKey
  11. * @return
  12. * @throws Exception
  13. */
  14. public Map<Object, Object> encryption(String jsonString, String publicKey) throws Exception {
  15. int length = jsonString.length();
  16. int offset = 0;
  17. int i = 0;
  18. HashMap<Object, Object> map = new HashMap<>();
  19. // 判断字符串长度是否大于40,大于就截取前40位,分批加密
  20. while (length - offset > 0) {
  21. if (length - offset > MAX_JSON_STRING) {
  22. String substring = jsonString.substring(offset, offset + MAX_JSON_STRING);
  23. String encrypt = RSAUtils.encrypt(substring, RSAUtils.getPublicKey(publicKey));
  24. map.put(i, encrypt);
  25. } else {
  26. String substring = jsonString.substring(offset,length);
  27. String encrypt = RSAUtils.encrypt(substring, RSAUtils.getPublicKey(publicKey));
  28. map.put(i, encrypt);
  29. }
  30. i++;
  31. offset = i * MAX_JSON_STRING;
  32. }
  33. return map;
  34. }
  35. }