java AES加密

AES加密有很多种模式

java默认支持:

AES/CBC/NoPadding (128) 
AES/CBC/PKCS5Padding (128) 
AES/ECB/NoPadding (128) 
AES/ECB/PKCS5Padding (128) 
DES/CBC/NoPadding (56) 
DES/CBC/PKCS5Padding(56) 
DES/ECB/NoPadding(56) 
DES/ECB/PKCS5Padding (56) 
DESede/CBC/NoPadding (168) 
DESede/CBC/PKCS5Padding (168) 
DESede/ECB/NoPadding (168) 
DESede/ECB/PKCS5Padding (168) 
RSA/ECB/PKCS1Padding ( 1024,2048 ) 
RSA/ECB/OAEPWithSHA-1AndMGF1Padding ( 1024,2048 ) 
RSA/ECB/OAEPWithSHA-256AndMGF1Padding ( 1024,2048 ) 

具体实现:

package com.budongfeng.tboot.modules.your.uitls;

import cn.hutool.crypto.CipherMode;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;

/**
 * @Author: WXM
 * @Description:
 * @Date: create in 2020/11/5 11:29
 */

public class DataEncryptUtil {
    private static String ALGORITHM = "AES";
    private static String CharaSet = "UTF-8";
    private static String ALGORITHM_MODE = "AES/CBC/PKCS7Padding";
    private static String IV = "0182068209920300";



    //获取SecretKey
//    private static SecretKey generaterKey(String seed) throws Exception{
//        //生成key对应的加密方式
//        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
//        //SecureRandom存放 生成SecretKey的参数,两种方式均可
//        SecureRandom secureRandom = new SecureRandom(seed.getBytes(CharaSet));
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
//        //把设置的盐值设置到SecureRandom中
        secureRandom.setSeed(seed.getBytes(CharaSet));
//        keyGenerator.init(128,secureRandom);
//        SecretKey secretKey = keyGenerator.generateKey();
//        return secretKey;
//    }

    //获取SecretKey
    private static SecretKeySpec generaterKey(String seed) throws Exception{
        SecretKeySpec secretKeySpec = new SecretKeySpec(seed.getBytes(), ALGORITHM);
        return secretKeySpec;
    }

    private static String aes(byte[] contentArray, int mode, SecretKey secretKey) throws Exception{
        //如果是ALGORITHM_MODE = "AES/CBC/PKCS7Padding",java默认中没有这种模式,需要手动加入
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //设置aes加密的具体模式
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE);
        //CBC模式需要设置偏移向量值
        IvParameterSpec iv = new IvParameterSpec(IV.getBytes(CharaSet));
        cipher.init(mode,secretKey,iv);
        if(mode == Cipher.ENCRYPT_MODE){
            byte[] bytes = cipher.doFinal(contentArray);
            return Base64.getEncoder().encodeToString(bytes);
        }else {
            byte[] decode = Base64.getDecoder().decode(contentArray);
            return new String (cipher.doFinal(decode),Charset.forName(CharaSet));
        }

    }

    public static String encrypt(String cont,String seed) throws Exception{
        SecretKey secretKey = generaterKey(seed);
        String aes = aes(cont.getBytes(CharaSet),Cipher.ENCRYPT_MODE,secretKey);
        return aes;
    }

    public static String decrypt(String cont,String seed) throws Exception{
        SecretKey secretKey = generaterKey(seed);
        String aes = aes(cont.getBytes(CharaSet),Cipher.DECRYPT_MODE,secretKey);
        return aes;
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值