小程序加密數據解密算法java版

小程序開發者如須要獲取敏感數據,須要對接口返回的加密數據( encryptedData )進行對稱解密。 解密算法以下:java

  1. 對稱解密使用的算法爲 AES-128-CBC,數據採用PKCS#7填充。
  2. 對稱解密的目標密文爲 Base64_Decode(encryptedData)。
  3. 對稱解密祕鑰 aeskey = Base64_Decode(session_key), aeskey 是16字節。
  4. 對稱解密算法初始向量 爲Base64_Decode(iv),其中iv由數據接口返回。

這裏使用微信提供的 PKCS7Encoder 對小程序進行加密數據解密, 也提供了加密方法算法


import java.nio.charset.Charset;
import java.util.Arrays;

/** * 提供基於PKCS7算法的加解密接口. */
public class PKCS7Encoder {

    static Charset CHARSET = Charset.forName("utf-8");
    static int BLOCK_SIZE = 32;

    /** * 得到對明文進行補位填充的字節. * * @param count 須要進行填充補位操做的明文字節個數 * @return 補齊用的字節數組 */
    public static byte[] encode(int count) {
        // 計算須要填充的位數
        int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
        if (amountToPad == 0) {
            amountToPad = BLOCK_SIZE;
        }
        // 得到補位所用的字符
        char padChr = chr(amountToPad);
        String tmp = new String();
        for (int index = 0; index < amountToPad; index++) {
            tmp += padChr;
        }
        return tmp.getBytes(CHARSET);
    }

    /** * 刪除解密後明文的補位字符 * * @param decrypted 解密後的明文 * @return 刪除補位字符後的明文 */
    public static byte[] decode(byte[] decrypted) {
        int pad = decrypted[decrypted.length - 1];
        if (pad < 1 || pad > 32) {
            pad = 0;
        }
        return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
    }

    /** * 將數字轉化成ASCII碼對應的字符,用於對明文進行補碼 * * @param a 須要轉化的數字 * @return 轉化獲得的字符 */
    static char chr(int a) {
        byte target = (byte) (a & 0xFF);
        return (char) target;
    }

}
複製代碼

import java.util.ArrayList;

public class ByteGroup {

    ArrayList<Byte> byteContainer = new ArrayList<Byte>();

    public byte[] toBytes() {
        byte[] bytes = new byte[byteContainer.size()];
        for (int i = 0; i < byteContainer.size(); i++) {
            bytes[i] = byteContainer.get(i);
        }
        return bytes;
    }

    public ByteGroup addBytes(byte[] bytes) {
        for (byte b : bytes) {
            byteContainer.add(b);
        }
        return this;
    }

    public int size() {
        return byteContainer.size();
    }
}
複製代碼

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


/** * @author yyb * @since 2018-01-10 */
public class WxCryptUtils {

    /** * 小程序 數據解密 * * @param encryptData 加密數據 * @param iv 對稱解密算法初始向量 * @param sessionKey 對稱解密祕鑰 * @return 解密數據 */
    public static String decrypt(String encryptData, String iv, String sessionKey) throws Exception {
        AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("AES");
        algorithmParameters.init(new IvParameterSpec(Base64.decodeBase64(iv)));
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), algorithmParameters);
        byte[] decode = PKCS7Encoder.decode(cipher.doFinal(Base64.decodeBase64(encryptData)));
        String decryptStr = new String(decode, StandardCharsets.UTF_8);
        return decryptStr;
    }
    
    /** * 數據加密 * * @param data 須要加密的數據 * @param iv 對稱加密算法初始向量 * @param sessionKey 對稱加密祕鑰 * @return 加密數據 */
    public static String encrypt(String data, String iv, String sessionKey) throws Exception {
        AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("AES");
        algorithmParameters.init(new IvParameterSpec(Base64.decodeBase64(iv)));
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), algorithmParameters);
        byte[] textBytes = json.getBytes(StandardCharsets.UTF_8);
        ByteGroup byteGroup= new ByteGroup();
        byteGroup.addBytes(textBytes);
        byte[] padBytes = PKCS7Encoder.encode(byteGroup.size());
        byteGroup.addBytes(padBytes);
        byte[] encryptBytes = cipher.doFinal(byteGroup.toBytes());
        return Base64.encodeBase64String(encryptBytes);
    }
    
    
    public static void main(String[] args) throws Exception {
        // 微信 小程序的 測試數據
        String encrypt = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM\n" +
                " QmRzooG2xrDcvSnxIMXFufNstNGTyaGS\n" +
                " 9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+\n" +
                " 3hVbJSRgv+4lGOETKUQz6OYStslQ142d\n" +
                " NCuabNPGBzlooOmB231qMM85d2/fV6Ch\n" +
                " evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6\n" +
                " /1Xx1COxFvrc2d7UL/lmHInNlxuacJXw\n" +
                " u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn\n" +
                " /Hz7saL8xz+W//FRAUid1OksQaQx4CMs\n" +
                " 8LOddcQhULW4ucetDf96JcR3g0gfRK4P\n" +
                " C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB\n" +
                " 6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns\n" +
                " /8wR2SiRS7MNACwTyrGvt9ts8p12PKFd\n" +
                " lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV\n" +
                " oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG\n" +
                " 20f0a04COwfneQAGGwd5oa+T8yO5hzuy\n" +
                " Db/XcxxmK01EpqOyuxINew==";

        String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
        String iv = "r7BXXKkLb8qrSNn05n0qiA==";

        String decrypt = WxCryptUtils.decrypt(encrypt, iv, sessionKey);
        System.out.println(decrypt);
    }
}
複製代碼

最後的結果:apache

{
  "country": "CN",
  "unionId": "ocMvos6NjeKLIBqg5Mr9QjxrP1FA",
  "gender": 1,
  "province": "Guangdong",
  "city": "Guangzhou",
  "avatarUrl": "http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0",
  "openId": "oGZUI0egBJY1zhBYw2KhdUfwVJJE",
  "nickName": "Band",
  "language": "zh_CN",
  "watermark": {
    "appid": "wx4f4bc4dec97d474b",
    "timestamp": 1477314187
  }
}
複製代碼
相關文章
相關標籤/搜索