欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信小程序Java解密AES加密数据

程序员文章站 2024-03-14 15:33:58
...

介绍

微信demo下载下来发现没有Java版本,只能再找资料解密
微信小程序Java解密AES加密数据
我的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>