编辑代码

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;



class Main {
	public static void main(String[] args) {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
		System.out.println("Hello world!   - java.jsrun.net ");
         // 生成随机的128位密钥
        byte[] key = getRule(16);
        // 要加密的明文数据
        String plaintext = "Hello, World!";
        // 加密数据
        String encrypt = encrypt(plaintext, key);
        // 解密数据
        String decryptedPlaintext = decrypt(encrypt, key);
        System.out.println("明文数据: " + plaintext);
        System.out.println("加密后的数据: " + encrypt);
        System.out.println("解密后的数据: " + decryptedPlaintext);
		System.out.println("Hello, World");
	}


	 /**
     * 选择AES算法和加密模式(例如,CBC模式)
     */
    public static final String AES_CBC = "AES/CBC/PKCS5Padding";
    /**
     * 创建密钥规范
     */
    public static final String AES_KEY = "AES";

 
    /**
     * 获取规则key(随机生成的规则)
     *
     * @param i 秘钥16, 24, 32位数
     * @return
     */
    public  byte[] getRule(int i) throws Exception {
        if (i != 16 && i != 24 && i != 32) {
            throw new Exception("规则位数必须是16或24或32位数!");
        }
        StringBuffer sb = new StringBuffer();
        // 生成随机的密钥
        byte[] key = new byte[i];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(key);
        for (int in : key) {
            sb.append(in).append(",");
        }
        String substring = sb.substring(0, sb.length() - 1);
        System.out.println("秘钥:" + substring);
        return key;
    }
 
    /**
     * 创建密钥规范
     *
     * @param key 秘钥
     * @return
     */
    public static SecretKeySpec secretKeySpec(byte[] key) {
        return new SecretKeySpec(key, AES_KEY);
    }
 
    /**
     * 选择AES算法和加密模式
     *
     * @param key 秘钥
     * @param i   加密1 解密2
     * @return
     * @throws Exception
     */
    public static Cipher cipher(byte[] key, int i) throws Exception {
        SecretKeySpec secretKeySpec = secretKeySpec(key);
        // 创建初始化向量
        IvParameterSpec ivParameterSpec = new IvParameterSpec(key);
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = Cipher.getInstance(AES_CBC);
        // 初始化加密器
        cipher.init(i, secretKeySpec, ivParameterSpec);//加密
        return cipher;
    }
 
    /**
     * 加密
     *
     * @param plaintext 加密明文
     * @param key       秘钥
     * @return
     * @throws Exception
     */
    public static String encrypt(String plaintext, byte[] key) throws Exception {
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = cipher(key, Cipher.ENCRYPT_MODE);
        // 加密数据
        byte[] bytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        String encode = Base64.getEncoder().encodeToString(bytes);
        return encode;
    }
 
    /**
     * 解密
     *
     * @param encrypt 加密的数据
     * @param key     秘钥
     * @return
     * @throws Exception
     */
    public static String decrypt(String encrypt, byte[] key) throws Exception {
        byte[] decode = Base64.getDecoder().decode(encrypt);
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = cipher(key, Cipher.DECRYPT_MODE);
        // 解密数据
        byte[] plaintextBytes = cipher.doFinal(decode);
        String plaintext = new String(plaintextBytes, StandardCharsets.UTF_8);
        return plaintext;
    }
}