AES128、AES192、AES256加密、解密算法工具类

本文详细介绍了使用AES算法进行数据加密和解密的过程,包括配置模式、填充、密钥及偏移量,提供了具体的Java代码实现,展示了如何通过16、24或32位密钥实现128、192或256位加密,并使用Base64进行编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要配置AES_MODE(模式和填充)、AES_KEY(密钥)、AES_IV(偏移量)

至于是128还是192还是256,是根据密钥的长度自动确定的,且密钥长度只有三种:16、24、32

128的16位key,16位iv
192的24位key,16位iv
256的32位key,16位iv

测试地址:http://www.ssleye.com/aes_cipher.html

import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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;

/**
 * author:wangjinqiang
 * date:2019/12/12
 */
public class SecurityAES {
    private static final String TAG = "SecurityAES";
    /**
     * 模式/填充
     */
    private final static String AES_MODE = "AES/CBC/PKCS5PADDING";
    /**
     * AES加密时的秘钥
     * 128的16位key,16位iv
     * 192的24位key,16位iv
     * 256的32位key,16位iv
     */
    private final static String AES_KEY = "01234567890123456789012345678901";
    /**
     * 偏移量,长度必须16位,多了少了都不行
     */
    private final static String AES_IV = "0123456789012345";
    /**
     * AES专用秘钥
     */
    private static SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), "AES");

    static {
        Log.e(TAG, "数据块长度: " + AES_KEY.getBytes().length * 8);
    }

    /**
     * 加密文本
     *
     * @param content 明文
     * @return 加密后Base64的字符串
     */
    public static String encrypt(String content) {
        try {
            // 创建AES密码器
            Cipher cipher = Cipher.getInstance(AES_MODE);
            // 将文本进行utf-8的编码
            byte[] byteContent = content.getBytes("utf-8");

            //偏移量
            byte[] iv = AES_IV.getBytes();
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            // 初始化AES密码器为加密器
            cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
            // 进行AES加密
            byte[] result = cipher.doFinal(byteContent);
            // 将byte数组转为Base64字符串
            return Base64.encodeToString(result, Base64.NO_WRAP);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解码文本
     *
     * @param encryptContent 密文(Base64)
     * @return 明文
     */
    public static String decrypt(String encryptContent) {
        try {
            byte[] content = Base64.decode(encryptContent, Base64.NO_WRAP);
            // 创建AES密码器
            Cipher cipher = Cipher.getInstance(AES_MODE);
            //偏移量
            byte[] iv = AES_IV.getBytes();
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            // 初始化AES密码器为解密器
            cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);// 初始化
            // 进行AES解密
            byte[] result = cipher.doFinal(content);
            // 将二进制转为字符串
            return new String(result);
        } 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 (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将byte数组转换成16进制的字符串
     *
     * @param buf
     * @return 16进制的字符串
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            //将每个字节都转成16进制的
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                //为保证格式统一,用两位16进制的表示一个字节
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制的字符串转换为byte数组
     *
     * @param hexStr
     * @return byte数组
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        //两个16进制表示一个字节,所以字节数组大小为hexStr.length() / 2
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            //每次获取16进制字符串中的两个转成10进制(0-255)
            int num = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
            //将10进制强转为byte
            result[i] = (byte) num;
        }
        return result;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值