Java中解密微信加密數(shù)據(jù)工具類
更新時間:2021年06月23日 15:54:47 作者:Asurplus、
最近小編一直在開發(fā)微信公眾號、小程序項目,微信返回給我們的數(shù)據(jù)都是加密的,我們需要使用sessionkey配合解密,才能看到我們想要的數(shù)據(jù),基于代碼怎么實現(xiàn)呢,下面小編給大家?guī)砹薐ava中解密微信加密數(shù)據(jù)工具類的完整代碼,一起看看吧
當(dāng)我們開發(fā)微信公眾號,小程序等,微信返回給我們的數(shù)據(jù)往往是經(jīng)過加密的,我們需要使用 sessionKey 配合解密,才能得到我們想要的數(shù)據(jù)
1、引入依賴
<!-- lombok依賴 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- alibaba的fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> <!-- 工具包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <!-- rsa加密工具--> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.55</version> </dependency>
2、解密工具類
import com.alibaba.fastjson.JSONObject; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.AlgorithmParameters; import java.security.Security; import java.util.Arrays; /** * 解密微信加密數(shù)據(jù)工具類 */ @Slf4j public class WechatUtils { /** * 解密微信加密數(shù)據(jù) * * @param encryptedData * @param iv * @param sessionkey * @return */ public static JSONObject decryptWechatData(String encryptedData, String iv, String sessionkey) { // 被加密的數(shù)據(jù) 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"); if (StringUtils.isNotBlank(result)) { log.info("----------解密微信數(shù)據(jù)成功----------"); return JSONObject.parseObject(result); } } } catch (Exception e) { e.printStackTrace(); log.info("----------解密微信數(shù)據(jù)失敗----------"); } return null; } }
這樣,我們將微信加密的數(shù)據(jù),轉(zhuǎn)化成了 JSON 對象,就得到了我們想要的數(shù)據(jù)了
以上就是Java中解密微信加密數(shù)據(jù)工具類的詳細(xì)內(nèi)容,更多關(guān)于Java加密解密工具類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用SpringMVC的@Validated注解驗證的實現(xiàn)
這篇文章主要介紹了使用SpringMVC的@Validated注解驗證的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08IDEA使用Mybatis插件 MyBatisCodeHelper-Pro的圖文教程
這篇文章主要介紹了IDEA使用Mybatis插件 MyBatisCodeHelper-Pro的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09