新增AES加密方式

This commit is contained in:
wenc000 2020-10-30 16:34:52 +08:00
parent ae06a9791f
commit 3ddf96bd4c
3 changed files with 23 additions and 6 deletions

View File

@ -77,7 +77,11 @@ public class WechatMiniAppAuthServiceImpl implements IWechatMiniAppAuthService {
LOG.debug("绑定用户 | 登录"); LOG.debug("绑定用户 | 登录");
String wechatSignInfo = Base64.encodeBase64String(AesUtil.aesCommonEncoder("WECHAT_SIGN_INFO", new StringBuilder(wechatMiniAppUserInfo.getOpenid()).append("_WenG_").append(wechatMiniAppProperties.getAppKey()).toString()).getBytes("UTF-8")); String wechatSignInfo = Base64.encodeBase64String(AesUtil.aesCommonEncoder("WECHAT_SIGN_INFO", new StringBuilder(wechatMiniAppUserInfo.getOpenid()).append("_WenG_").append(wechatMiniAppProperties.getAppKey()).toString()).getBytes("UTF-8"));
String token = getAppToken(wechatSignInfo); String token = getAppToken(wechatSignInfo);
WechatMiniAppManager.getInstance().addUser(token, wechatMiniAppUserInfo); if (token.endsWith("_0") || token.endsWith("_1")) {
WechatMiniAppManager.getInstance().addUser(token.substring(0, token.length() - 2), wechatMiniAppUserInfo);
} else {
WechatMiniAppManager.getInstance().addUser(token, wechatMiniAppUserInfo);
}
return new SuccessResultData(token); return new SuccessResultData(token);
} }

View File

@ -1,15 +1,13 @@
package com.cm.common.utils; package com.cm.common.utils;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException; import java.security.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AesUtil { public class AesUtil {
@ -110,6 +108,9 @@ public class AesUtil {
byte[] enCodeFormat = key.getBytes(); byte[] enCodeFormat = key.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
if (StringUtils.equals(paddingType, PKCS_7)) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
Cipher cipher = Cipher.getInstance("AES/CBC/" + paddingType); Cipher cipher = Cipher.getInstance("AES/CBC/" + paddingType);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
return cipher.doFinal(byteContent); return cipher.doFinal(byteContent);
@ -143,7 +144,11 @@ public class AesUtil {
public static byte[] aesCommonDecoderDetail(byte[] keyBytes, byte[] encryptedBytes, byte[] ivBytes, String paddingType) throws Exception { public static byte[] aesCommonDecoderDetail(byte[] keyBytes, byte[] encryptedBytes, byte[] ivBytes, String paddingType) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/" + paddingType); Cipher cipher;
if (StringUtils.equals(paddingType, PKCS_7)) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
cipher = Cipher.getInstance("AES/CBC/" + paddingType);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
return cipher.doFinal(encryptedBytes); return cipher.doFinal(encryptedBytes);
} }

View File

@ -78,6 +78,7 @@
<jna-platform.version>5.5.0</jna-platform.version> <jna-platform.version>5.5.0</jna-platform.version>
<spring-security-jwt.version>1.0.9.RELEASE</spring-security-jwt.version> <spring-security-jwt.version>1.0.9.RELEASE</spring-security-jwt.version>
<netty.version>4.1.50.Final</netty.version> <netty.version>4.1.50.Final</netty.version>
<bouncycastle.version>1.46</bouncycastle.version>
</properties> </properties>
<!-- 依赖管理 --> <!-- 依赖管理 -->
@ -431,6 +432,13 @@
<version>${netty.version}</version> <version>${netty.version}</version>
</dependency> </dependency>
<!-- netty end --> <!-- netty end -->
<!-- bouncycastle start -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
<!-- bouncycastle end -->
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>