项目开发中,需要针对请求参数加密 解密操作 可以使用下列工具类 oap.security.enabled=true oap.security.enableIgnoreAnnotation=true oap.security.secretKey=MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOH4vp2PiV45ar4DFsvACGRxvpvqafKMyduCRRZ7CRKnH0W0oXmRKjsyTde//XZ5IOKxFXrRrDWlPV4PFHZH8wVh0iAyvrtZXeaDi7d9MoumBN77gK2Ji1/Oiezt5BCaIMIN/WM9bxaMLxQ2BtVwQQHFdasjpz1PKwq3J4oVDe4BAklnLPJbVJRpXiyUTUGZiIRQlkiEM2xsUyGvd1hl03pC8PUq5WGuSNNnL1RYb8LGlZSqFfgnpnX1TfQaS3zSDgQrfLDfqKAeqUdvGWIS2Tfn/BXsAqDRdWjvMR4wJjZIIrT6VKUyWgwmQxWyJ//c4vK9zBAsGv/CR8hxClowK7AgMBAAECggEAGQwaPmZMuS3Y+gWS8FTZytBxbOp6twYbuZggXnBlv8+5MZV9I1eI32zNDHO0DSRlqzWP4uHRG6ipqcHxA+IEQGKM09e3P91Kwl2f0fo2LrFaE6Ixz0eI8lnWLVbL+K5X7sdEa53LPdR2tspnDh4Ny0ETbBqJDqzy8IvmqrfTZtPWX7nrChIms7izz1dVocU19OmtxSestNGT0CIfmtGZVkJlsNjhaaFiRRD4whv0skVVoQ5MkTkFRwxap/QDi4KLUKLcIYCEetYZvMatk91vhsqIA2Sq+fr+BCXf3XOVs2BQERygC4KWXQC872ymmBWiCw3Eqrt3bWe7/zx9YYqBQQKBgQDbanFAwh+MBlP9+c6VqJilMee1LmI9fu6eFXx5gnU1OeQeklIlTA6m520d/CKIKbUVfSkm3yYowqy5+wSOawKAMlUAqgU3BfEbGo886szmPNSukqrEwb/xeOwOEfyegyGjSkzQYteEw3FtgHwKSQWeYfVQovwylNJXgQrlc9xF8QKBgQCl0fGJBk3S9K0B2v1/71/5zoEWWqTDFKm575UOQ4qz8OaWYO3d9lkcCUcNiQNIRTBb60r6lBieEKWfig7+/lPDaD1QMGbrP73nCi/SOYOishGHdwhrra1ML2GkVqn9DkZAgRFYjvGijt+0U6ggT4XQqAZ04Lc04JQ0HUwaNzs3awKBgFHavs9a3Cv/vFvWUrRQRknyeS+FU07/yIMfHuWxvooRw8GH8q9JVXn8gh+ajUNg09Zlb/G5c+Hm5ksnkwaSc0dh/4YU54ckZ1MmMGOYcMI/1VQfESf/49xwsRd6rFRyNR1WvZtMFHzJ8MnaEexAW6E/iCYB97XjASl10ZvSAk3xAoGBAI50NFqRbd5jkRXc+pckIIHNZJy9tnpZkA5KwvUEFW7M8GwUTYW4k4lAdygrowQ9XHQ7SajH/fYdOww6YPOzrmZbcQEo8+H6WrzXxYmHG410CRclrbhD9nNS6UFzqonZ74SO3UYFV5VdxNV4YSGQ79b34sQaIYptvSSgXrbO6DKNAoGADsPfjDjLGB+rBHIdviBXr6Cc3BP/jOzuQZQ85xUrsuMbAfjilxDSpZE5LhiPVv/cD2VklOr0Nksu7BQAoh1Sz91uUjDmrYayjRZ4PlDx98nYFfoko/rQzLY070SIjykHbDopHuMTh2VvlbOBzlXqDBzKRoUMCbKUFRBs1dBzY18= oap.security.public.secretKey=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjh+L6dj4leOWq+AxbLwAhkcb6b6mnyjMnbgkUWewkSpx9FtKF5kSo7Mk3Xv/12eSDisRV60aw1pT1eDxR2R/MFYdIgMr67WV3mg4u3fTKLpgTe+4CtiYtfzons7eQQmiDCDf1jPW8WjC8UNgbVcEEBxXWrI6c9TysKtyeKFQ3uAQJJZyzyW1SUaV4slE1BmYiEUJZIhDNsbFMhr3dYZdN6QvD1KuVhrkjTZy9UWG/CxpWUqhX4J6Z19U30Gkt80g4EK3yw36igHqlHbxliEtk35/wV7AKg0XVo7zEeMCY2SCK0+lSlMloMJkMVsif/3OLyvcwQLBr/wkfIcQpaMCuwIDAQABp
配置
@ConfigurationProperties(
prefix = "oap.security"
)
public class SecurityProperties {
private boolean enabled;
private boolean enableIgnoreAnnotation;
private Class<? extends Annotation> annotationClass = IgnoreRequestSecurity.class;
private String secretKey;
public SecurityProperties() {
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setEnableIgnoreAnnotation(boolean enableIgnoreAnnotation) {
this.enableIgnoreAnnotation = enableIgnoreAnnotation;
}
public boolean isEnableIgnoreAnnotation() {
return this.enableIgnoreAnnotation;
}
public Class<? extends Annotation> getAnnotationClass() {
return this.annotationClass;
}
public void setAnnotationClass(Class<? extends Annotation> annotationClass) {
this.annotationClass = annotationClass;
}
public String getSecretKey() {
return this.secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}
public class RsaDeEnServiceImpl implements RsaDeEnService {
private final LogUtil log = LogUtil.of(RsaDeEnServiceImpl.class,null);
@Autowired
private SecurityProperties secretProperties;
@Value("${oap.security.public.secretKey}")
private String publicKey;
private static final String AES = "AES";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static Base64.Encoder base64Encoder = Base64.getEncoder();
private static Base64.Decoder base64Decoder = Base64.getDecoder();
/**
* 加密
* @param content
* @return
*/
@Override
public RsaResultVo encryptContext(String content) throws Exception{
RsaResultVo result=new RsaResultVo();
String IV = "OapManagement" + UUID.randomUUID().toString();
log.info("加密", "IV: " + IV);
int v = (int)(Math.random() * 9 + 1) * 100000;
String IKEY = System.currentTimeMillis() + String.valueOf(v);
log.info("加密", "IKEY: " + IKEY);
String ikey = SecureUtil.md5(IKEY).substring(0,32);
String iv = SecureUtil.md5(IV).substring(0,16);
//1.先用内容+IKEY和IV 进行AES256 加密操作
SecretKey secretKey = new SecretKeySpec(ikey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(1, secretKey, new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
byte[] byteEncode = content.getBytes(StandardCharsets.UTF_8);
byte[] byteAES = cipher.doFinal(byteEncode);
String s = base64Encoder.encodeToString(byteAES);
SecurityContent param=new SecurityContent();
param.setContent(s);
String s2 = JSON.toJSONString(param);
result.setContext(s2);
//2.再把IKEY和IV 进行RSA 加密
Map<String,Object> signatureKey=new HashMap<>();
signatureKey.put("IKEY",ikey);
signatureKey.put("IV",iv);
String s1 = JSON.toJSONString(signatureKey);
byte[] decoded = Base64.getDecoder().decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipherRsa = Cipher.getInstance("RSA");
cipherRsa.init(1, pubKey);
result.setSignatureKey(Base64.getEncoder().encodeToString(cipherRsa.doFinal(s1.getBytes(StandardCharsets.UTF_8))));
return result;
}
/**
* 解密
* @param content
* @param signatureKey
* @return
*/
@Override
public String decryptContext(String content,String signatureKey) throws Exception{
//1.先获取signatureKey 通过RSA 解密 得到IKEY和IV
byte[] inputByte = Base64.getDecoder().decode(signatureKey);
byte[] decoded = Base64.getDecoder().decode(this.secretProperties.getSecretKey());
RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, priKey);
String s = new String(cipher.doFinal(inputByte));
JSONObject jsonObject = JSONObject.parseObject(s);
String iKey = jsonObject.getString("IKEY");
log.info("解密", "IKEY: " + iKey);
String vi = jsonObject.getString("IV");
log.info("解密", "IV: " + vi);
String decryptBody =JSON.toJSONString(content);
log.info("解密", "body of before decrypt: " + decryptBody);
SecurityContent content2 = (SecurityContent)JSON.parseObject(content, SecurityContent.class);
if (content != null && !StringUtils.isEmpty(content2.getContent())) {
String json = AesUtil256.decode(iKey, content2.getContent(), vi);
log.info("解密", "body of after decrypt:" + json);
return json;
} else {
return null;
}
}
7606

被折叠的 条评论
为什么被折叠?



