•  
  • 摘要:微信小程序解密建议使用1.6及以上的环境使用maven下载jar包org.bouncycastlebcprov-jdk15on1.55加密类代码importorg.bouncycastle.jce.provider.BouncyCastleProvider;importorg.bouncycastle.util.Arrays;importjavax.crypto.Cipher;importjavax.crypto.NoSuchPaddingException;importja
  • 微信小程序解密

    建议使用1.6及以上的环境

    使用maven下载jar包


    org.bouncycastle 
    bcprov-jdk15on 
    1.55 

    加密类代码

    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.bouncycastle.util.Arrays;
    import javax.crypto.Cipher;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.Security;
    /**
    * Created by wind on 2016/12/24.
    */
    /**
    *
    * @author ngh
    * AES128 算法
    *
    * CBC 模式
    *
    * PKCS7Padding 填充模式
    *
    * CBC模式需要添加一个参数iv--对称解密算法初始向量 iv
    *
    * 介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别
    * 要实现在java端用PKCS7Padding填充,需要用到bouncycastle组件来实现
    */
    public class Pkcs7Encoder {
    // 算法名称
    static final String KEY_ALGORITHM = "AES";
    // 加解密算法/模式/填充方式
    static final String algorithmStr = "AES/CBC/PKCS7Padding";
    private static Key key;
    private static Cipher cipher;
    boolean isInited = false;
    //默认对称解密算法初始向量 iv
    static byte[] iv = { 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37, 0x30, 0x38 };
    public static void init(byte[] keyBytes) {
    // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
    int base = 16;
    if (keyBytes.length % base != 0) {
    int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
    byte[] temp = new byte[groups * base];
    Arrays.fill(temp, (byte) 0);
    System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
    keyBytes = temp;
    }
    // 初始化
    Security.addProvider(new BouncyCastleProvider());
    // 转化成JAVA的密钥格式
    key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
    try {
    // 初始化cipher
    cipher = Cipher.getInstance(algorithmStr, "BC");
    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchProviderException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /**
    * 加密方法
    * --使用默认iv时
    * @param content
    * 要加密的字符串
    * @param keyBytes
    * 加密密钥
    * @return
    */
    public static byte[] encrypt(byte[] content, byte[] keyBytes) {
    byte[] encryptedText = encryptOfDiyIV(content,keyBytes,iv);
    return encryptedText;
    } /**
    * 解密方法
    * --使用默认iv时
    * @param encryptedData
    * 要解密的字符串
    * @param keyBytes
    * 解密密钥
    * @return
    */
    public static byte[] decrypt(byte[] encryptedData, byte[] keyBytes) {
    byte[] encryptedText = decryptOfDiyIV(encryptedData,keyBytes,iv);
    return encryptedText;
    }
    /**
    * 加密方法
    * ---自定义对称解密算法初始向量 iv
    * @param content
    * 要加密的字符串
    * @param keyBytes
    * 加密密钥
    * @param ivs
    * 自定义对称解密算法初始向量 iv
    * @return 加密的结果
    */
    public static byte[] encryptOfDiyIV(byte[] content, byte[] keyBytes, byte[] ivs) {
    byte[] encryptedText = null;
    init(keyBytes);
    System.out.println("IV:" + new String(ivs));
    try {
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivs));
    encryptedText = cipher.doFinal(content);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return encryptedText;
    }
    /**
    * 解密方法
    *
    * @param encryptedData
    * 要解密的字符串
    * @param keyBytes
    * 解密密钥
    * @param ivs
    * 自定义对称解密算法初始向量 iv
    * @return
    */
    public static byte[] decryptOfDiyIV(byte[] encryptedData, byte[] keyBytes,byte[] ivs) {
    byte[] encryptedText = null;
    init(keyBytes);
    System.out.println("IV:" + new String(ivs));
    try {
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivs));
    encryptedText = cipher.doFinal(encryptedData);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return encryptedText;
    }

    测试类

    public class Test {
    public static void main(String[] args) throws Exception { String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==";
    String iv="r7BXXKkLb8qrSNn05n0qiA==";
    String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
    toStr(sessionKey,encryptedData,iv);
    }
    private static void toStr(String sessionKey,String encryptedData ,String iv) throws Exception{
    byte[] sessionKeyBy = BASE64.decryptBASE64(sessionKey.getBytes());
    byte[] encryptedDataBy = BASE64.decryptBASE64(encryptedData.getBytes());
    byte[] ivBy = BASE64.decryptBASE64(iv.getBytes());
    byte[] dec = Pkcs7Encoder.decryptOfDiyIV(encryptedDataBy, sessionKeyBy,ivBy);
    System.out.println(new String(dec));
    }

    测试类中的Base64 带测试方法 
    import org.apache.commons.codec.binary.Base64;
    /** * BASE64加密解密 */ public class BASE64 { /** * BASE64解密 * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(byte[] key) throws Exception { return (new Base64()).decode(key); } /** * BASE64加密 * @param key * @return * @throws Exception */ public static byte[] encryptBASE64(byte[] key) throws Exception { return (new Base64()).encode(key); } public static void main(String[] args) throws Exception { byte[] data = BASE64.encryptBASE64("http://aub.iteye.com/".getBytes()); System.out.println("加密前:" + new String(data)); byte[] byteArray = BASE64.decryptBASE64(data); System.out.println("解密后:" + new String(byteArray)); } } 完成 
    会出现的一些错误:


    cannot load mian class


    打包成jar包以后,在正式运行执行:java bcprov-jdk150on-1.55.jar之前,需要删除 .jar包中的一个文件:META-INF/BCKEY.DSA

    no such provider: BC 
    在jdk中的jre/lib/security修改java.security文件, security.provider.6=com.sun.security.sasl.Provider 下面添加 security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider
    在/jre/lib/ext中添加bcprov-jdk15-135.jar的jar包
    bcprov-jdk16-143.jar提供加密,解密,生成密钥对等方法