溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中的解密微信和加密數(shù)據(jù)工具類

發(fā)布時(shí)間:2021-06-24 09:09:53 來源:億速云 閱讀:187 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Java中解密微信和加密數(shù)據(jù)工具類”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java中解密微信和加密數(shù)據(jù)工具類”吧!

當(dāng)我們開發(fā)微信公眾號(hào),小程序等,微信返回給我們的數(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 對(duì)象,就得到了我們想要的數(shù)據(jù)了

到此,相信大家對(duì)“Java中解密微信和加密數(shù)據(jù)工具類”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI