posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
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   邢帅杰  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-09-09 js几种循环方式
点击右上角即可分享
微信分享提示