package com.repair.common.utils; import java.util.HashMap; import java.util.Map; public class EncryptionUtil { private static final int MAX_JSON_STRING = 40; /** * 将需要加密的字段分批加密 * * @param jsonString * @param publicKey * @return * @throws Exception */ public Map encryption(String jsonString, String publicKey) throws Exception { int length = jsonString.length(); int offset = 0; int i = 0; HashMap map = new HashMap<>(); // 判断字符串长度是否大于40,大于就截取前40位,分批加密 while (length - offset > 0) { if (length - offset > MAX_JSON_STRING) { String substring = jsonString.substring(offset, offset + MAX_JSON_STRING); String encrypt = RSAUtils.encrypt(substring, RSAUtils.getPublicKey(publicKey)); map.put(i, encrypt); } else { String substring = jsonString.substring(offset,length); String encrypt = RSAUtils.encrypt(substring, RSAUtils.getPublicKey(publicKey)); map.put(i, encrypt); } i++; offset = i * MAX_JSON_STRING; } return map; } }