Java AES CBC/ECB NoPadding 加密128位

部署运行你感兴趣的模型镜像

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Random;

/**
 * AES加密工具类
 */
public class AESUtil {


    /**
     * 加密Key 需要16位 可用数字与字母组成
     */
    private static String key = "Ijasw2o93jd81kdD";
    /**
     * 偏移量 需要16位
     */
    private static String iv = "4w2Df1xSj5ff662d";

    private static Logger log = LoggerFactory.getLogger(AESUtil.class);

    private static Base64.Decoder decoder;

    private static Base64.Encoder encoder;



    static {
        decoder = Base64.getDecoder();
        encoder = Base64.getEncoder();
    }



    public static String getSixteenBitString(){
        StringBuffer sb = new StringBuffer();
        String[] chars = new String[]{
                "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","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",
        };
        int len = chars.length;
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            sb.append(chars[random.nextInt(len-1)]);
        }
        return sb.toString();

    }



    /**
     * AES加密
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String encryptAES_CBC(String data,String key,String iv) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/NoPadding");

            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes();
            int dataLength = dataBytes.length;
            if (dataLength % blockSize != 0) {
                dataLength = dataLength + (blockSize - (dataLength % blockSize));
            }
            byte[] plaintext = new byte[dataLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            byte[] bytes = cipher.doFinal(plaintext);
            return encoder.encodeToString(bytes);

        }catch (Exception e) {
            log.error("AES加密失败");
            log.error(e.getMessage());

        }

        return null;

    }
    /**
     * AES解密
     * @param data
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String decrptyAES_CBC(String data,String key,String iv){
        try {
            byte[] bytes = decoder.decode(data);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] plainByte = cipher.doFinal(bytes);
            return new String(plainByte).trim();
        }catch (Exception e){
            log.error("AES解密失败");
            log.error(e.getMessage());
        }

        return null;
    }


    /**
     * AES加密
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptAES_ECB(String data,String key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        int blockSize = cipher.getBlockSize();
        byte[] dataBytes = data.getBytes();
        int dataLength = dataBytes.length;
        if(dataLength % blockSize != 0){
            dataLength = dataLength + (blockSize - (dataLength % blockSize));
        }
        byte [] plaintext = new byte[dataLength];
        System.arraycopy(dataBytes,0,plaintext,0,dataBytes.length);
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] bytes = cipher.doFinal(plaintext);
        return encoder.encodeToString(bytes);


    }

    /**
     * AES解密
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrptyAES_ECB(String data,String key) throws Exception{
        byte[] bytes = decoder.decode(data);
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] plainByte = cipher.doFinal(bytes);
        return new String(plainByte).trim();
    }



    public static void main(String[] args) throws Exception{
        String s = "123231312";
        String encrypt = encryptAES_ECB(s,key);
        System.out.println(encrypt);
        System.out.println(decrptyAES_ECB(encrypt,key));
        String s1 = encryptAES_CBC(s, key, iv);
        System.out.println(s1);
        System.out.println(decrptyAES_CBC(s1,key,iv));
        getSixteenBitString();
    }



}

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值