aes cbc java_微信小程序AES-CBC加解密 java版本

首先需要引入依赖 bcprov-jdk15on

maven版本:

org.bouncycastle

bcprov-jdk15on

1.66

gradle版本:

implementation("org.bouncycastle:bcprov-jdk15on:1.66")

解密工具类:

import com.alibaba.fastjson.JSON;

import org.apache.commons.codec.binary.Base64;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import java.security.*;

import java.util.HashMap;

import java.util.Map;

public class AesCBC {

public static boolean initialized =false;

/**

* AES解密

* @param content 密文

* @return

* @throws InvalidAlgorithmParameterException

* @throws NoSuchProviderException

*/

public static byte[]decrypt(byte[] content,byte[] keyByte,byte[] ivByte)throws InvalidAlgorithmParameterException {

initialize();

try {

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

Key sKeySpec =new SecretKeySpec(keyByte,"AES");

cipher.init(Cipher.DECRYPT_MODE,sKeySpec,generateIV(ivByte));// 初始化

byte[]result =cipher.doFinal(content);

return result;

}catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}catch (NoSuchPaddingException e) {

e.printStackTrace();

}catch (InvalidKeyException e) {

e.printStackTrace();

}catch (IllegalBlockSizeException e) {

e.printStackTrace();

}catch (BadPaddingException e) {

e.printStackTrace();

}catch (NoSuchProviderException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

/**

* 微信小程序用户信息解密

* @param encryptedData

* @param sessionKey

* @param iv

* @return

*/

public static String decrypt(String encryptedData,String sessionKey,String iv){

try {

byte[]resultByte =decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(sessionKey),Base64.decodeBase64(iv));

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

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

return info;

}

}catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void initialize(){

if (initialized)return;

Security.addProvider(new BouncyCastleProvider());

initialized =true;

}

//生成iv

public static AlgorithmParameters generateIV(byte[] iv)throws Exception{

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

params.init(new IvParameterSpec(iv));

return params;

}

public static void main(String[] args) {

String encryptedData,sessionKey,iv,appId;

encryptedData =加密字符串内容;

sessionKey ="小程序sessionkey";

iv ="小程序返回加密字符串同级的iv";

appId ="小程序appid";

String info =decrypt(encryptedData,sessionKey,iv);

System.out.println(info);

}

}

解密结果:{"phoneNumber":"18605137011","purePhoneNumber":"18605137011","countryCode":"86","watermark":{"timestamp":1599096570,"appid":"wx0198259b4c28f5a9"}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值