13 changed files with 206 additions and 12 deletions
@ -1,5 +1,5 @@ |
|||||
spring: |
spring: |
||||
profiles: |
profiles: |
||||
active: dev |
active: prod |
||||
include: util-dev,common |
include: util-prod,common |
||||
|
|
||||
|
@ -0,0 +1,27 @@ |
|||||
|
package com.ccsens.wechatutil.bean.po; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/9/27 10:59 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class WxPhoneDecryptInfo { |
||||
|
@ApiModelProperty("用户绑定的手机号(国外手机号会有区号)") |
||||
|
private String phoneNumber; |
||||
|
@ApiModelProperty("没有区号的手机号") |
||||
|
private String purePhoneNumber; |
||||
|
@ApiModelProperty("区号") |
||||
|
private int countryCode; |
||||
|
private WaterMark watermark; |
||||
|
|
||||
|
@Data |
||||
|
public static class WaterMark { |
||||
|
/** 单位:秒 */ |
||||
|
private Long timestamp; |
||||
|
private String appid; |
||||
|
} |
||||
|
} |
@ -0,0 +1,102 @@ |
|||||
|
package com.ccsens.wechatutil.wxmini; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.ccsens.wechatutil.bean.po.WxPhoneDecryptInfo; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.codec.binary.Hex; |
||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
|
|
||||
|
import javax.crypto.Cipher; |
||||
|
import javax.crypto.spec.IvParameterSpec; |
||||
|
import javax.crypto.spec.SecretKeySpec; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.security.*; |
||||
|
import java.security.spec.InvalidParameterSpecException; |
||||
|
|
||||
|
import org.apache.xmlbeans.impl.util.Base64; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/9/27 10:47 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class MiniEncryptionAndDecryptionUtil { |
||||
|
|
||||
|
private final static String sha = "SHA"; |
||||
|
/**加密方式*/ |
||||
|
private final static String keyAlgorithm = "AES"; |
||||
|
|
||||
|
|
||||
|
static { |
||||
|
Security.addProvider(new BouncyCastleProvider()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* SHA1签名(获取手机号) |
||||
|
* @param data 原始数据 |
||||
|
* @return 签名 |
||||
|
* @throws |
||||
|
*/ |
||||
|
public static String sha1(String data) { |
||||
|
log.info("签名原始数据:{}", data); |
||||
|
MessageDigest md; |
||||
|
try { |
||||
|
md = MessageDigest.getInstance(sha); |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
log.error("微信小程序签名异常:", e); |
||||
|
return null; |
||||
|
} |
||||
|
md.update(data.getBytes()); |
||||
|
String result = Hex.encodeHexString(md.digest()); |
||||
|
log.info("签名结果:{}", result); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 解密(获取手机号) |
||||
|
* @param originalContent 待解密数据 |
||||
|
* @param sessionKey 会话密钥 |
||||
|
* @param iv 加密算法的初始向量 |
||||
|
* @return 解密数据 |
||||
|
*/ |
||||
|
public static String decryption(String originalContent, String sessionKey, String iv ){ |
||||
|
try { |
||||
|
log.info("数据解密, original:{}, key:{}, iv:{}", originalContent, sessionKey, iv); |
||||
|
//数据填充方式
|
||||
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); |
||||
|
Key sKeySpec = new SecretKeySpec(Base64.decode(sessionKey.getBytes()), keyAlgorithm); |
||||
|
// 初始化
|
||||
|
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(Base64.decode(iv.getBytes()))); |
||||
|
byte[]data = cipher.doFinal(Base64.decode(originalContent.getBytes())); |
||||
|
String result = new String(data, StandardCharsets.UTF_8); |
||||
|
log.info("微信小程序解密数据:{}", result); |
||||
|
return result; |
||||
|
} catch (Exception e) { |
||||
|
log.error("解密异常:", e); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成iv |
||||
|
* @param iv iv |
||||
|
* @return iv对象 |
||||
|
* @throws NoSuchAlgorithmException 没有签名 |
||||
|
* @throws InvalidParameterSpecException 无效参数 |
||||
|
*/ |
||||
|
private static AlgorithmParameters generateIV(byte[] iv) throws NoSuchAlgorithmException, InvalidParameterSpecException { |
||||
|
AlgorithmParameters params = AlgorithmParameters.getInstance(keyAlgorithm); |
||||
|
params.init(new IvParameterSpec(iv)); |
||||
|
return params; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
String content = "aH7vLxzu3upsT8kK5DbjCbsIoOkTKIKUSNruHbhmmH6km/Yeb1eCsdfFY4HNPeNZH0oBRGmQ7sn8c/PaaYlLfi9598ghEvfl3HENWSLl43MqNXEFvCtTzxhc0Y7dJRtKCIWL4EEr8M42XxnhH77lAmpLomL+fbzw8upmz+gcJWqYMnXPKtGH2B2BeHC/njUgeuAeTqR7zuYihPyVY4Ol8g=="; |
||||
|
String iv = "eW1+fg0f3TOU25MQfvTDwg=="; |
||||
|
String sessionKey = "gA+NNouMd0FMmaf3LkQrMA=="; |
||||
|
decryption(content, sessionKey, iv); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue