对称加密算法(AES算法)

一、概述

        对称加密算法就是传统的用一个秘钥进行加密和解密。从程序的角度看,所谓加密,就是这样一个函数,它接收密码和明文,然后输出密文:
secret = encrypt(key, message);
而解密则相反,它接收密码和密文,然后输出明文:
plain = decrypt(key, secret);

        在软件开发中,常用的对称加密算法有:

算法密钥长度(bit)工作模式填充模式
DES56/64ECB/CBC/PCBC/CTR/...NoPadding/PKCS5Padding/...
AES128/192/256ECB/CBC/PCBC/CTR/...NoPadding/PKCS5Padding/PKCS7Padding/...
IDEA128ECBPKCS5Padding/PKCS7Padding/...

         密钥长度直接决定加密强度,而工作模式和填充模式可以看成是对称加密算法的参数和格式选择。Java标准库提供的算法实现并不包括所有的工作模式和所有填充模式。

二、使用AES加密和解密

        AES算法是目前应用最广泛的加密算法。比较常见的工作模式是ECB和CBC。

1.ECB模式加密与解密

        ECB模式是最简单的AES加密模式,它需要一个固定长度的密钥,固定的明文会生成固定的密文。

        Java标准库提供对称加密接口,使用时时按以下步骤编写代码:
1.使用getInstance()方法,根据算法名称/工作模式/填充模式获取Cipher实例
2.根据算法名称初始化一个SecretKey实例,密钥必须是指定长度
3.使用SerectKey初始化Cipher实例,并设置加密或解密模式
4.传入明文或密文,获得密文或明文。

注:加密与解密方法的参数类型都是字节数组

代码如下: 

// AES + ECB
public class Work1 {
    public static void main(String[] args) throws GeneralSecurityException {
        // 原文:
        String message = "三更灯火五更鸡,正是男儿读书时";
        System.out.println("Message(原始信息): " + message);

        // 128位密钥 = 16 bytes Key:
        byte[] key = "1234567890abcdef".getBytes();

        // 加密:
        byte[] data = message.getBytes();
        byte[] encrypted = encrypt(key, data);
        System.out.println("Encrypted(加密内容): " + Base64.getEncoder().encodeToString(encrypted));

        // 解密:
        byte[] decrypted = decrypt(key, encrypted);
        System.out.println("Decrypted(解密内容): " + new String(decrypted));
    }

    // 加密:
    public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        // 创建密码对象,需要传入算法/工作模式/填充模式
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        // 根据key的字节内容,"恢复"秘钥对象
        SecretKey secretKey = new SecretKeySpec(key,"AES");
        // 初始化秘钥:设置加密模式ENCRYPT_
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        // 根据原始内容(字节),进行加密
        return cipher.doFinal(input);
    }

    // 解密:
    public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        // 创建密码对象,需要传入算法/工作模式/填充模式
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        // 根据key的字节内容,"恢复"秘钥对象
        SecretKey secretKey = new SecretKeySpec(key,"AES");
        // 初始化秘钥:设置解密模式DECRYPT_MODE
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        // 根据原始内容(字节),进行解密
        return cipher.doFinal(input);
    }
}

2.CBC模式加密与解密

        ECB模式是最简单的AES加密模式,这种一对一的加密方式会导致安全性降低。所以,更好的方式是通过CBC模式,它需要一个随机数作为IV参数,这样对于同一份明文,每次生成的密文都不同:在CBC模式下,需要一个随机生成的16字节IV参数,必须使用SecureRandom生成。并且因为多了一个IvparameterSpec实例,因此,初始化方法需要调用Cipher的重载方法传入IvparameterSpec。(每次的IV不同,密文也不同)

        CBC模式加密在ECB加密步骤的基础上增加了创建iv参数的过程,解密则需要把加密后的数据分割为16个字节数组的iv参数和剩余的密文数组然后进行解密。

//AES + CBC
public class Work2 {
    public static void main(String[] args) throws Exception {
        // 原文:
        String message = "三更灯火五更鸡,正是男儿读书时";
        System.out.println("Message(原始信息): " + message);

        // 256位密钥 = 32 bytes Key:
        byte[] key = "12345678912345678912345678912345".getBytes();

        // 加密:
        byte[] data = message.getBytes();
        byte[] encrypted = encrypt(key,data);
        System.out.println("Encrypted(加密内容): " +
                Base64.getEncoder().encodeToString(encrypted));

        // 解密:
        byte[] decrypted = decrypt(key,encrypted);
        System.out.println("Decrypted(解密内容): " + new String(decrypted));
    }

    // 加密:
    public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        // 设置算法/工作模式CBC/填充
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // 恢复秘钥对象
        SecretKey secretKey = new SecretKeySpec(key,"AES");
        // CBC模式需要生成一个16 bytes的initialization vector:
        SecureRandom secureRandom = SecureRandom.getInstanceStrong();
        byte[] iv = secureRandom.generateSeed(16);
        System.out.println("IV字节数组内容:" + Arrays.toString(iv));
        System.out.println("IV字节数组长度:" + iv.length);
        IvParameterSpec ivps = new IvParameterSpec(iv);
        // 初始化秘钥:操作模式、秘钥、IV参数
        cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivps);

        // 加密
        byte[] bytes = cipher.doFinal(input);
        // IV不需要保密,把IV和密文一起返回:
        return join(iv,bytes);
    }

    // 解密:
    public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
        // 把input分割成IV和密文:
        byte[] iv = new byte[16];
        byte[] data =new byte[input.length-16];
        System.arraycopy(input,0,iv,0,16);
        System.arraycopy(input,16,data,0,data.length);
        System.out.println(Arrays.toString(iv));

        // 解密:
        SecretKey secretKey = new SecretKeySpec(key,"AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // 初始化秘钥:操作模式、秘钥、IV参数
        cipher.init(Cipher.DECRYPT_MODE,secretKey,ivParameterSpec);
        // 解密操作
        return cipher.doFinal(data);
    }

    // 合并数组
    public static byte[] join(byte[] bs1, byte[] bs2) {
        byte[] r = new byte[bs1.length + bs2.length];
        System.arraycopy(bs1,0,r,0,bs1.length);
        System.arraycopy(bs2,0,r,bs1.length,bs2.length);
        return r;
    }
}

 小结:


● 对称加密算法使用同一个密钥进行加密和解密,常用算法有DES、AES和IDEA等;
● 密钥长度由算法设计决定,长度越长,密钥越安全,AES的密钥长度是128/192/256位;
● 使用对称加密算法需要指定算法名称、工作模式和填充模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起个有趣的名字好难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值