概述
今天在写微信的接口的时候,需要解密微信的敏感信息。根据微信的文档可以知道,微信使用的是AES解密。正好之前是没有接触过这种解密。所以就记录一下开发过程。
什么AES解密?
AES解密是一种对称密钥加密方法。所谓的对称就是加密和解密的时候的密钥是相同的,也有非对称加密比如RSA加密。具体的原理我们就不说了。(其实我也不太懂),我们目前先知道如何让使用就行了。
AES算法模式是把明文分成很多个区块然后分别进行加密。解密同理
AES固定每个区块的128位,而密钥则是128位192位和256位。
密钥的生成算法下面的方法也有写。本文是固定128位。如果有需要可以修改。
微信敏感信息解密
微信使用的是算法是 AES-128-CBC 算法。
AES=加密方式
128=密码位数
CBC=AES的加密模式 (CBC就是带有IV初始变量模式)
我们直接看代码吧。
public static String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String KEY_ALGORITHM = "AES";
private static final String UTF8 = "UTF-8";
private static final String SECURERANDOM_MODE = "SHA1PRNG";
/**
* 带有初始变量的解密(微信用)
*
* @param content 密文
* @param skey 密钥
* @param ivParameter 初始向量
* @return
* @throws Exception
*/
public static String weixinDecrypt(String content, String skey, String ivParameter) {
try {
BASE64Decoder decoder = new BASE64Decoder();
// 根据微信文档要求需要把 密文、密钥、iv 使用BASE64进行解码
byte[] keyByte = new BASE64Decoder().decodeBuffer(skey);
byte[] contentByte = decoder.decodeBuffer(content);
byte[] ivByte = decoder.decodeBuffer(ivParameter);
// 生成密码
SecretKeySpec keySpec = new SecretKeySpec(keyByte, KEY_ALGORITHM);
// 生成IvParameterSpec
IvParameterSpec iv = new IvParameterSpec(ivByte);
// 初始化解密 指定模式 AES/CBC/PKCS5Padding
Cipher cipher = Cipher.getInstance(CBC_CIPHER_ALGORITHM);
// 指定解密模式 传入密码 iv
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
// 解密
byte[] result = cipher.doFinal(contentByte);
return new String(result, UTF8);
} catch (Exception ex) {
log.error("【解密错误】{}", ex.getMessage());
return null;
}
}
如果有需要的同学可以直接拷贝。
下面顺便奉上整个AES加密解密的工具类
package com.xmhmyh.util;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* @Author: buding
* @DateTime: 2020/3/4 4:39 下午
*/
@Slf4j
public class AESUtils {
public static String CBC_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
public static String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String KEY_ALGORITHM = "AES";
private static final String UTF8 = "UTF-8";
private static final String SECURERANDOM_MODE = "SHA1PRNG";
/**
* 带有初始变量的解密(微信用)
*
* @param content 密文
* @param skey 密钥
* @param ivParameter 初始向量
* @return
* @throws Exception
*/
public static String weixinDecrypt(String content, String skey, String ivParameter) {
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] keyByte = new BASE64Decoder().decodeBuffer(skey);
byte[] contentByte = decoder.decodeBuffer(content);
byte[] ivByte = decoder.decodeBuffer(ivParameter);
// 生成密码
SecretKeySpec keySpec = new SecretKeySpec(keyByte, KEY_ALGORITHM);
// 生成IvParameterSpec
IvParameterSpec iv = new IvParameterSpec(ivByte);
// 初始化解密 指定模式 AES/CBC/PKCS5Padding
Cipher cipher = Cipher.getInstance(CBC_CIPHER_ALGORITHM);
// 指定解密模式 传入密码 iv
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
// 解密
byte[] result = cipher.doFinal(contentByte);
return new String(result, UTF8);
} catch (Exception ex) {
log.error("【解密错误】{}", ex.getMessage());
return null;
}
}
/**
* AES 加密操作
*
* @param content 待加密内容
* @param key 加密密钥
* @return 返回Base64转码后的加密数据
*/
public static String encrypt(String content, String key) {
try {
// 密文转换 byte[]
byte[] byteContent = content.getBytes(UTF8);
// 创建密码器
Cipher cipher = Cipher.getInstance(ECB_CIPHER_ALGORITHM);
// 初始化为加密模式 并传入密码 密码器
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
// 加密
byte[] result = cipher.doFinal(byteContent);
//通过Base64转码返回
return new BASE64Encoder().encode(result);
} catch (Exception ex) {
log.error("【加密错误】{}", ex.getMessage());
return null;
}
}
/**
* AES 解密操作
*
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
try {
// 创建密码器
Cipher cipher = Cipher.getInstance(ECB_CIPHER_ALGORITHM);
// 初始化为解密模式 并传入密码 密码器
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
// 执行解密操作
byte[] result = cipher.doFinal(new BASE64Decoder().decodeBuffer(content));
// 转换成 string
return new String(result, UTF8);
} catch (Exception ex) {
log.error("【解密错误】{}", ex.getMessage());
return null;
}
}
/**
* 生成加密秘钥
*
* @return
*/
private static SecretKeySpec getSecretKey(final String password) throws NoSuchAlgorithmException {
//生成指定算法密钥的生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
//================重点========================
// 指定随机数算法 防止win linux 生成的随机数不一致
SecureRandom secureRandom = SecureRandom.getInstance(SECURERANDOM_MODE);
secureRandom.setSeed(password.getBytes());
keyGenerator.init(128, secureRandom);
//===============重点=========================
//生成密钥
SecretKey secretKey = keyGenerator.generateKey();
//转换成AES的密钥
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
}
}
注意:这里最好指定一下随机数的生成算法。不然win和linux系统生成的可能不一致导致最终解密失败
SecureRandom secureRandom = SecureRandom.getInstance(SECURERANDOM_MODE);
secureRandom.setSeed(password.getBytes());
keyGenerator.init(128, secureRandom);
使用代码
private static String key = "testPassword";
@IgnoreAuth
@PostMapping("test")
@WxApi
public Response test() {
String content = "这是一串加密的代码";
return Response.success(AESUtils.encrypt(content, key));
}
@IgnoreAuth
@PostMapping("test2")
@WxApi
public Response test2(String content, String key) {
return Response.success(AESUtils.decrypt(content, key));
}
加密结果
解密结果
好了,以上就是本人在写微信敏感消息解密的一点心得。如果有哪里不对,还请大家多多指出。谢谢