Aes生成key和iv,加密解密

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;


/**
 * Created with IntelliJ IDEA.
 *
 * @author : Future master
 * @version : 1.0
 * @Project : C4
 * @Package : org.example
 * @ClassName : Aes.java
 * @createTime : 2021/11/26 18:05
 */
public class Aes {
    private String Key;
    private String Iv;
    private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
    private static final String AES = "AES";
    //构造函数进行初始化key和iv(随机生成)
    public Aes(){
        this.Key = getRandom(16);
        this.Iv = getRandom(16);
    }
    public String getKey(){
        return this.Key;
    }
    public String getIv(){
        return this.Iv;
    }
    public String getRandom(int length){
        char[] arr = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k',
                'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        String result = String.valueOf(arr[(int)Math.floor(Math.random()*36)]);
        for(int i = 1;i 
  

import java.nio.charset.StandardCharsets;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class AESUtils {
    public AESUtils() {
    }

    public static String decrypt(byte[] secretBytes, String secretKey) throws Exception {
        if (secretKey.length() != 16) {
            throw new RuntimeException("key length must be 16 for AES");
        } else {
            SecretKey key = new SecretKeySpec(secretKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(key.getAlgorithm());
            cipher.init(2, key);
            return new String(cipher.doFinal(secretBytes), StandardCharsets.UTF_8);
        }
    }

    public static byte[] encrypt(String simple, String secretKey) throws Exception {
        if (secretKey.length() != 16) {
            throw new RuntimeException("key length must be 16 for AES");
        } else {
            SecretKey key = new SecretKeySpec(secretKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(key.getAlgorithm());
            cipher.init(1, key);
            return cipher.doFinal(simple.getBytes(StandardCharsets.UTF_8));
        }
    }

    /** @deprecated */
    public static String encryptData(String key, String iv, String content) throws Exception {
        byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(1, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        return Base64.encodeBase64String(encryptedBytes);
    }

    /** @deprecated */
    public static String decryptData(String key, String iv, String content) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(2, secretKey, ivParameterSpec);
        byte[] encryptedBytes = Base64.decodeBase64(content);
        byte[] result = cipher.doFinal(encryptedBytes);
        return new String(result, StandardCharsets.UTF_8);
    }

    public static String encryptToBase64(String key, String iv, String content) throws Exception {
        byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(1, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        return Base64.encodeBase64String(encryptedBytes);
    }

    public static String decryptFromBase64(String key, String iv, String base64Str) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(2, secretKey, ivParameterSpec);
        byte[] encryptedBytes = Base64.decodeBase64(base64Str);
        byte[] result = cipher.doFinal(encryptedBytes);
        return new String(result, StandardCharsets.UTF_8);
    }

    public static String encryptToHex(String key, String iv, String content) throws Exception {
        byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(1, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        return Hex.encodeHexString(encryptedBytes);
    }

    public static String decryptFromHex(String key, String iv, String hexStr) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(2, secretKey, ivParameterSpec);
        byte[] result = cipher.doFinal(Hex.decodeHex(hexStr));
        return new String(result, StandardCharsets.UTF_8);
    }
}

你可能感兴趣的:(util工具类,java,servlet,jvm)