1. 程式人生 > >微信小程式Java解密AES加密資料

微信小程式Java解密AES加密資料

介紹

微信demo下載下來發現沒有Java版本,只能再找資料解密
這裡寫圖片描述
我的Java解密程式碼是這樣寫的,可以用

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto
.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class MyAESUtil { public static String decryptWXAppletInfo(String sessionKey, String encryptedData, String iv) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String result = null;
byte[] encrypData = Base64.decodeBase64(encryptedData); byte[] ivData = Base64.decodeBase64(iv); byte[] sessionKeyB = Base64.decodeBase64(sessionKey); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivData); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(sessionKeyB, "AES"
); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] doFinal = cipher.doFinal(encrypData); result = new String(doFinal); return result; } }

依賴包:

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>