AES的256带偏移量、128不带偏移量的加解密算法(PKC

AES的加解密:

AES 256加密支持

  • java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法

解决方法:

stackoverflow.com/questions/6…
JDK8 jar包下载地址:
www.oracle.com/technetwork…
JDK7 jar包下载地址:
www.oracle.com/technetwork…
JDK6 jar包下载地址:
www.oracle.com/technetwork…

1.在 jdk安装目录中(%JAVA_HOME%\jre\lib\ext)添加 jar 包 bcprov-jdk16-1.46.jar。
2.在 jdk安装目录下( %JAVA_HOME%\jre\lib\security )修改 java.security 文件,将第74行(我的是在74行)的 
security.provider.7=com.sun.security.sasl.Provider 替换为security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider
3.替换( %JAVA_HOME%\jre\lib\security )下面的local_policy.jar和US_export_policy.jar
备注:
JDK1.8.0_151无需去官网下载 local_policy.jar US_export_policy.jar这个jar包,只需要修改Java\jdk1.8.0_151\jre\lib\security这目录下的java.security文件配置即可。
从Java 1.8.0_151和1.8.0_152开始,为JVM启用 无限制强度管辖策略 有了一种新的更简单的方法。如果不启用此功能,则不能使用AES-256。
crypto.policy=unlimited
复制代码
  • 测试代码使用jdk1.8.0_131 ,详细代码如下:

AES 256位 带偏移量 加解密算法

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
 * AES加密工具类:256位 不带偏移量
 *
 * @author lilinshen
 * @date 2021/03/31
 */
public class AesUtils {

    //偏移量
    public static final String VIPARA = "f%Z4F+qtFh624970";
    //编码方式
    public static final String CODE_TYPE = "UTF-8";
    //填充类型(注:CBC)
    public static final String AES_TYPE = "AES/CBC/PKCS7Padding";
    //私钥,AES固定格式为128/192/256 bits.即:16/24/32bytes
    private static final String AES_KEY = "AG+BwcnekYZy$9f7X#b2zdB93brfFMmz";
    
    /**
     * 加密带偏移量
     */
    public static String encrypt(String cleartext) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(cleartext.getBytes(CODE_TYPE));
            return new BASE64Encoder().encode(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 解密带偏移量
     */
    public static String decrypt(String encrypted) {
        try {
            byte[] byteMi = new BASE64Decoder().decodeBuffer(encrypted);
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, CODE_TYPE);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    
    /**
     * 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String content = "{\"vid\":73160,\"road\":\"default1\",\"timestamp\":1585815671868,\"devicetype\":\"pc\",\"encode_sign\":\"a069831103819c3c76a23fa084747bac\"}";
        System.out.println(encryptPy(content));
        System.out.println(decryptPy(encryptPy(content)));
    }
}
复制代码

AES 128位 不带偏移量 加解密算法

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
 * AES加密工具类:128位 不带偏移量
 *
 * @author lilinshen
 * @date 2021/03/31
 */
public class AesUtils {
    //编码方式
    public static final String CODE_TYPE = "UTF-8";
    //填充类型(注:ECB)
    public static final String AES_TYPE = "AES/ECB/PKCS7Padding";
    //私钥,AES固定格式为128/192/256 bits.即:16/24/32bytes
    private static final String AES_KEY = "46cc793c53dc451b";

    /**
     * 加密不带偏移量
     */
    public static String encrypt(String cleartext) {
        try {
            SecretKeySpec sKey = new SecretKeySpec(AES_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, sKey);
            byte[] decrypted = cipher.doFinal(cleartext.getBytes(CODE_TYPE));
            return Base64.encodeBase64String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 解密不带偏移量
     */
    public static String decrypt(String content) {
        try {
            byte[] sourceBytes = Base64.decodeBase64(content);
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_KEY.getBytes(), "AES"));
            byte[] decoded = cipher.doFinal(sourceBytes);
            return new String(decoded, CODE_TYPE);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String content = "{\"vid\":73160,\"road\":\"default1\",\"timestamp\":1585815671868,\"devicetype\":\"pc\",\"encode_sign\":\"a069831103819c3c76a23fa084747bac\"}";
        System.out.println(encrypt(content));
        System.out.println(decrypt(encrypt(content)));
    }
}

复制代码