评论

java敏感信息解密demo

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;


import org.apache.commons.lang3.StringUtils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import org.bouncycastle.util.encoders.Base64;

import java.security.AlgorithmParameters;

import java.security.Security;

import java.util.Arrays;

public class Demo{

public static JSONObject getDecryptMsg(String encryptedData, String iv, String sessionKey) {

byte[] dataByte = Base64.decode(encryptedData);

byte[] keyByte = Base64.decode(sessionKey);

byte[] ivByte = Base64.decode(iv);

try {

int base = 16;

if (keyByte.length % base != 0) {

int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);

byte[] temp = new byte[groups * base];

Arrays.fill(temp, (byte) 0);

System.arraycopy(keyByte, 0, temp, 0, keyByte.length);

keyByte = temp;

}

Security.addProvider(new BouncyCastleProvider());

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");

SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");

AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");

parameters.init(new IvParameterSpec(ivByte));

cipher.init(Cipher.DECRYPT_MODE, spec, parameters);

byte[] resultByte = cipher.doFinal(dataByte);

if (null != resultByte && resultByte.length > 0) {

String result = new String(resultByte, "UTF-8");

return JSONObject.parseObject(result);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void main(String[] args) {

String content=JSON.toJSONString(getDecryptMsg("", 

"", ""));

System.out.println(content);

}

}



小游戏技术交流QQ群:621710684

最后一次编辑于  01-17  
点赞 0
收藏
评论