AES加解密

一:AES加解密背景和算法

详细介绍地址:AES地址

AES属于对称加密,即密钥和公钥是一样的。

二:代码实现

AESUtils实体类:
package aes;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author lizhangyu
 * @date 2021/5/4 0:10
 */
public class AESUtils {

    /**
     * 编码格式
     */
    public static final String CHARSET = "UTF-8";

    /**
     * 加密算法
     */
    public static final String AES_ALGORITHM = "AES";

    /**
     * 生成密钥
     * @return
     */
    public static String generateKey() {
        try {
            //1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance(AES_ALGORITHM);
            //2.生成一个128位的随机源
            keygen.init(128, new SecureRandom());
            //3.产生原始对称密钥
            SecretKey secretKey = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte[] byteKey = secretKey.getEncoded();
            //5.返回密钥
            return Hex.encodeHexString(byteKey);
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }

    /**
     * 加密
     * @param thisKey 密钥
     * @param data 数据
     * @return 加密后的数据
     */
    public static String encode(String thisKey, String data) {
        try {
            //1.转换KEY
            Key key = new SecretKeySpec(Hex.decodeHex(thisKey), AES_ALGORITHM);
            //2.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            //3.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //4.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte[] byte_encode = data.getBytes(CHARSET);
            //5.根据密码器的初始化方式--加密:将数据加密
            byte[] result = cipher.doFinal(byte_encode);
            //6.将字符串返回
            return Hex.encodeHexString(result);
        }catch (DecoderException e) {
            e.printStackTrace();
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }

    /**
     * 解密
     * @param thisKey 密钥
     * @param data 加密的数据
     * @return 解密后的数据
     */
    public static String decode(String thisKey, String data) {
        try {
            //1.转换KEY
            Key key = new SecretKeySpec(Hex.decodeHex(thisKey), AES_ALGORITHM);
            //2.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            //3.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            //4.将加密并编码后的内容解码成字节数组
            byte[] byte_content = Hex.decodeHex(data);
            //5.解密
            byte[] byte_decode = cipher.doFinal(byte_content);
            //6.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            String result = new String(byte_decode, CHARSET);
            return result;
        }catch (DecoderException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }

    public static void main(String[] args) {
        String key = generateKey();
        System.out.println("生成的密钥为:" + key);
//        String pubKey = "6aa7066f738828f333b1bb84b62606c6";
//        String priKey = "6aa7066f738828f333b1bb84b62606c6";
        String data = "使用AES对称加密,请输入加密的规则使用AES对称加密AAAAAABBBBB";
        String encodeData = encode(key, data);
        System.out.println("加密后的数据为:" + encodeData);
        String decodeData = decode(key, encodeData);
        System.out.println("解密后的数据为:" + decodeData);
    }
}

运行结果:

生成的密钥为:af4259a41cca574be273f995d3b124e1
加密后的数据为:a9cdad9e6bbd6dd3f63590743276ee67f105491d395dc5d2add8f1cba0c8e1d6a2b3716c4f9ffc03a0d485b07f5d5623a9cdad9e6bbd6dd3f63590743276ee67628eaab6126dc2d758f8cb9d6fdb1c39618a6cc8e9f94880905486e1356e2c4e
解密后的数据为:使用AES对称加密,请输入加密的规则使用AES对称加密AAAAAABBBBB

Process finished with exit code 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值