zoukankan      html  css  js  c++  java
  • 对称加密 加密算法,加密模式,填充模式

    定义

    采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。

    一 加密算法

    DES : Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。

    AES : Advanced Encryption Standard, 高级加密标准,在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

    特点
    加密速度快, 可以加密大文件。
    密文可逆, 一旦密钥文件泄漏, 就会导致数据暴露。
    加密后编码表找不到对应字符, 出现乱码。
    一般结合Base64使用。

    DES加密,API示例:

    public class DesDemo {
        public static void main(String[] args) throws Exception {
    
            //原文
            String input = "尚硅";
    
            //密钥,des加密必须是8位
            String key = "12345678";
    
            //加密的算法
            String algorithm = "DES";
    
            //获取Cipher对象的算法/模式/填充
            String transformation = "DES";
    
            //加密
            String encryptDES = encryptDES(input, key, algorithm, transformation);
            System.out.println(encryptDES);
    
            //解密
            String decryptDES = decryptDES(encryptDES, key, algorithm, transformation);
            System.out.println(decryptDES);
    
        }
    
        private static String encryptDES(String input, String key, String algorithm, String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    
            //获取加密对象
            Cipher cipher = Cipher.getInstance(transformation);
            //指定密钥规则
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            //对加密进行初始化
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
    
            //执行加密
            byte[] bytes = cipher.doFinal(input.getBytes());
    
            //字节数组出现负数,ascii码找不到,因此进行base64转码
            String encode = Base64.encode(bytes);
            return encode;
        }
    
        private static String decryptDES(String input, String key, String algorithm, String transformation) throws Exception {
            //获取加密对象
            Cipher cipher = Cipher.getInstance(transformation);
            //指定密钥规则
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            //对加密进行初始化
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
    
            //执行加密
            byte[] bytes = cipher.doFinal(Base64.decode(input));
    
            return new String(bytes);
        }
    }

    AES加密API于DES类似,将上述代码中的DES换成AES即可。同时AES加密,密钥必须为16个字节。

    二 加密模式

    ECB : Electronic codebook, 电子密码本. 需要加密的消息按照块密码的块大小被分为数个块,并对每个块进行独立加密。
    优点 : 可以并行处理数据。
    缺点 : 同样的原文生成同样的密文, 不能很好的保护数据。
    同时加密,原文是一样的,加密出来的密文也是一样的。

    CBC : Cipher-block chaining, 密码块链接. 每个明文块先与前一个密文块进行异或后,再进行加密。在这种方法中,每个密文块都依赖于它前面的所有明文块。

    当加密第一个明文分组时,由于不存在前一个密文块,因此需要事先准备一个长度为一个分组的比特序列来代替前一个密文块,这个比特序列称为初始化向量(Initialization Vector),通常缩写为IV,一般来说,每次加密时都会随机产生一个不同的比特序列来作为初始化向量。

    优点 : 同样的原文生成的密文不一样
    缺点 : 串行处理数据

    三 填充模式

    当需要按块处理的数据, 数据长度不符合块处理需求时, 按照一定的方法填充满块长的规则。
    NoPadding,不填充。如果不填充,那么在DES加密算法下, 要求原文长度必须是8byte的整数倍。在AES加密算法下, 要求原文长度必须是16byte的整数倍。
    PKCS5Padding:数据块的大小为8位, 不够就补足

    Tips:
    默认情况下, 加密模式和填充模式为 : ECB/PKCS5Padding
    如果使用CBC模式, 在初始化Cipher对象时, 需要增加参数, 初始化向量IV : IvParameterSpec iv = new IvParameterSpec(key.getBytes());

    DES加密算法,CBC加密模式,带填充的API示例:

    public class DesDemo {
        public static void main(String[] args) throws Exception {
    
            //原文
            String input = "尚硅";
    
            //密钥,des加密必须是8位
            String key = "12345678";
    
            //加密算法
            String algorithm = "DES";
    
            //加密算法/加密模式/填充模式
            String transformation = "DES/CBC/PKCS5Padding";
    
            //加密
            String encryptDES = encryptDES(input, key, algorithm, transformation);
            System.out.println(encryptDES);
    
            //解密
            String decryptDES = decryptDES(encryptDES, key, algorithm, transformation);
            System.out.println(decryptDES);
    
        }
    
        private static String encryptDES(String input, String key, String algorithm, String transformation) throws Exception {
    
            //获取加密对象
            Cipher cipher = Cipher.getInstance(transformation);
            //指定密钥规则
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            //对加密进行初始化,添加iv向量,也必须为8个字节
            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,iv);
    
            //执行加密
            byte[] bytes = cipher.doFinal(input.getBytes());
    
            //字节数组出现负数,ascii码找不到,因此进行base64转码
            String encode = Base64.encode(bytes);
            return encode;
        }
    
        private static String decryptDES(String input, String key, String algorithm, String transformation) throws Exception {
            //获取加密对象
            Cipher cipher = Cipher.getInstance(transformation);
            //指定密钥规则
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
            //对加密进行初始化,添加iv向量
            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,iv);
    
            //执行加密
            byte[] bytes = cipher.doFinal(Base64.decode(input));
    
            return new String(bytes);
        }
    }
  • 相关阅读:
    Design Pattern
    javascript summary
    nodejs template
    MVC---Case 1
    About js
    本地schemeApp扩展
    BNU4208:Bubble sort
    [置顶] think in java interview-高级开发人员面试宝典代码示例
    java+socket 简易聊天工具
    oracle 字段自增 两段代码搞定
  • 原文地址:https://www.cnblogs.com/noyouth/p/13185391.html
Copyright © 2011-2022 走看看