code
package com.xcg.webapp.common;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
/**
 * 加密/解密
 * */
public class EncryptUtil {
    /**
     * @param text           明文/base64密文
     * @param key            密钥 AES必须16字节/DES必须8字节/TripleDES必须24字节
     * @param transformation 转换方式 AES/DES
     * @param mode           加密/解密
     */
    public static String extracted(String text, String key, String transformation, boolean mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(transformation);
        //key与给定的密钥内容相关联的密钥算法的名称
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), transformation);
        //Cipher 的操作模式,加密模式:ENCRYPT_MODE、 解密模式:DECRYPT_MODE、包装模式:WRAP_MODE 或 解包装:UNWRAP_MODE)
        cipher.init(mode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] bytes = cipher.doFinal(mode ? text.getBytes(StandardCharsets.UTF_8) : Base64.getDecoder().decode(text));
        return mode ? new String(Base64.getEncoder().encode(bytes)) : new String(bytes);
    }
//    原文链接:https://blog.csdn.net/qq_51534884/article/details/130361254

    public static String encrypt(String key, String plaintext) {
        try {
            byte[] keyBytes = key.getBytes();
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
            byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
            return Base64.getEncoder().encodeToString(iv) + ":" + Base64.getEncoder().encodeToString(ciphertext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String key, String ciphertext) {
        try {
            byte[] keyBytes = key.getBytes();
            String[] parts = ciphertext.split(":");
            byte[] iv = Base64.getDecoder().decode(parts[0]);
            byte[] encrypted = Base64.getDecoder().decode(parts[1]);
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
            byte[] plaintext = cipher.doFinal(encrypted);
            return new String(plaintext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
//    原文链接:https://cloud.tencent.com/developer/information/%E7%94%A8Java%E5%8A%A0%E5%AF%86%E5%92%8C%E8%A7%A3%E5%AF%86%E5%AF%86%E7%A0%81
}

String text = "你好世界!!";
        String key = "12345678";//des必须8字节
        // 算法/模式/填充  默认 DES/ECB/PKCS5Padding
        String transformation = "DES";

        String key1 = "abcd56781234abcd";//aes必须16字节
        String transformation1 = "AES";

        String key2 = "123456781234567812345678";//TripleDES使用24字节的key
        String transformation2 = "TripleDes";

//        String extractedStr = AESDESUtil.extracted(text, key1, transformation1, true);
//        System.out.println("AES加密:" + extractedStr);
//        String extractedStr1 = AESDESUtil.extracted(extractedStr, key1, transformation1, false);
//        System.out.println("解密:" + extractedStr1);
//        原文链接:https://blog.csdn.net/qq_51534884/article/details/130361254

        String extractedStr = EncryptUtil.encrypt(key1, text);
        System.out.println("AES加密:" + extractedStr);
        String extractedStr1 = EncryptUtil.decrypt(key1, extractedStr);
        System.out.println("AES解密:" + extractedStr1);

 

posted on 2024-09-09 14:48  邢帅杰  阅读(6)  评论(0编辑  收藏  举报