package com.nova.sankuai.domain.api.rongduoduo;
|
|
import org.springframework.stereotype.Component;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.util.Base64;
|
|
@Component
|
public class SecurityCore {
|
|
private static SecurityCore instance = null;
|
|
|
private SecurityCore() {
|
|
}
|
|
public static SecurityCore getInstance() {
|
if (instance == null)
|
instance = new SecurityCore();
|
return instance;
|
}
|
|
/*
|
* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
|
*/
|
public static String webSafeBase64StringEncoding(byte[] sSrc,boolean padded) throws Exception {
|
String encodeString= Base64.getEncoder().encodeToString(sSrc);// 此处使用BASE64做转码。
|
|
//websafe base64
|
encodeString=encodeString.replace("+","-");
|
encodeString=encodeString.replace("/","_");
|
|
//nopadding base64
|
if (!padded) {
|
if (encodeString.endsWith("=")) {
|
encodeString = encodeString.substring(0, encodeString.length() - 1);
|
if (encodeString.endsWith("=")) {
|
encodeString = encodeString.substring(0, encodeString.length() - 1);
|
}
|
}
|
}
|
return encodeString;
|
}
|
|
public static byte[] webSafeBase64StringDecoding(String sSrc) throws Exception {
|
//websafe base64
|
sSrc=sSrc.replace("-","+");
|
sSrc=sSrc.replace("_","/");
|
|
return Base64.getDecoder().decode(sSrc);
|
}
|
|
public static String base64StringEncoding(byte[] sSrc,boolean padded) throws Exception {
|
String encodeString=Base64.getEncoder().encodeToString(sSrc);// 此处使用BASE64做转码。
|
|
//nopadding base64
|
if (!padded) {
|
if (encodeString.endsWith("=")) {
|
encodeString = encodeString.substring(0, encodeString.length() - 1);
|
if (encodeString.endsWith("=")) {
|
encodeString = encodeString.substring(0, encodeString.length() - 1);
|
}
|
}
|
}
|
return encodeString;
|
}
|
|
public static byte[] base64StringDecoding(String sSrc) throws Exception {
|
return Base64.getDecoder().decode(sSrc);
|
}
|
|
public byte[] AES128CBCStringEncoding(String encData ,String secretKey,String vector) throws Exception {
|
if(secretKey == null) {
|
return null;
|
}
|
if(secretKey.length() != 16) {
|
return null;
|
}
|
if (vector != null && vector.length() != 16) {
|
return null;
|
}
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
byte[] raw = secretKey.getBytes();
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
|
|
return encrypted;
|
}
|
|
public String AES128CBCStringDecoding(byte[] sSrc,String key,String ivs){
|
try {
|
if(key == null) {
|
return null;
|
}
|
if(key.length() != 16) {
|
return null;
|
}
|
if (ivs != null && ivs.length() != 16) {
|
return null;
|
}
|
byte[] raw = key.getBytes("ASCII");
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
byte[] original = cipher.doFinal(sSrc);
|
String originalString = new String(original, "utf-8");
|
return originalString;
|
} catch (Exception ex) {
|
return null;
|
}
|
}
|
|
|
// 加密
|
public String encrypt(String sSrc){
|
try {
|
String encodeString=base64StringEncoding(AES128CBCStringEncoding(sSrc,"dxjf18129979469s","dxjf18129979469s"),false);
|
|
return encodeString;
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
return null;
|
}
|
}
|
|
// 解密
|
public String decrypt(String sSrc){
|
try {
|
String decodeString=AES128CBCStringDecoding(base64StringDecoding(sSrc),"dxjf18129979469s","dxjf18129979469s");
|
return decodeString;
|
} catch (Exception ex) {
|
return null;
|
}
|
}
|
}
|