您的位置 首页 java

AES加密介绍及样例

AES简介

AES

AES代表高级加密系统及其对称加密算法,它是由美国国家标准技术研究院(NIST)于2001年建立的电子数据加密规范

AES模式

对称/ 分组密码 一般分为流加密(如OFB、CFB等)和块加密(如ECB、CBC等)。对于流加密,需要将分组密码转化为流模式工作。对于块加密(或称分组加密),如果要加密超过块大小的数据,就需要涉及填充和链加密模式。

  • ECB (Electronic Code Book电子密码本)模式

ECB模式是最早采用和最简单的模式,它将加密的数据分成若干组,每组的大小跟加密密钥长度相同,然后每组都用相同的密钥进行加密。

优点:

1.简单; 2.有利于并行计算; 3.误差不会被传送; 

缺点: 

1.不能隐藏明文的模式; 2.可能对明文进行主动攻击; 因此,此模式适于加密小消息。

  • CBC (Cipher Block Chaining,加密块链)模式

优点:

1.不容易主动攻击,安全性好于ECB,适合传输长度长的 报文 ,是SSL、IPSec的标准。 缺点: 1.不利于并行计算; 2.误差传递; 3.需要初始化向量IV

  • CFB (Cipher FeedBack Mode,加密反馈)模式

优点:

1.隐藏了明文模式; 2.分组密码转化为流模式; 3.可以及时加密传送小于分组的数据; 

缺点:

1.不利于并行计算; 2.误差传送:一个明文单元损坏影响多个单元; 3.唯一的IV;

  • OFB (Output FeedBack,输出反馈)模式

优点:

1.隐藏了明文模式; 2.分组密码转化为流模式; 3.可以及时加密传送小于分组的数据; 

缺点:

1.不利于并行计算; 2.对明文的主动攻击是可能的; 3.误差传送:一个明文单元损坏影响多个单元 。

AES模式和填充方式

AES一般是16个字节为一块,然后对这一整块进行加密,如果输入的字符串不够16个字节,就需要补位

AES加密介绍及样例

前端AES加密

AES/CBC/NoPadding

  function getAES(string) {
        var key = '1qaz2013qazwsx92';
        var iv = '1qazsx12zxmd2093';
        var encrypted = getAesString(string, key, iv);
        return encrypted;
    };

    function getAesString(data, key, iv) {
        var key = CryptoJS.enc.Utf8.parse(key);
        var iv = CryptoJS.enc.Utf8.parse(iv);
        var encrypted = CryptoJS.AES.encrypt(data, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        });
        return encrypted.toString();
    };  

Java后端加密处理

AES/CBC/NoPadding

  import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * AES工具类
 *
 */
public class AESUtil {

    private final static String KEY = "1qaz2013qazwsx92"; // key:必须16个字符
    private final static String IV = "1qazsx12zxmd2093";  // 偏移量:必须16个字符

    public static void main(String[] args) throws UnsupportedEncodingException {
        String content = "123456          ";

        System.out.println("加密前:" + content+"长度"+content.length());
        //加密
        String encrypted = encrypt(content);
        //解密
        System.out.println("加密后:" + encrypted);

        String content1 = "13100001002     ";

        System.out.println("加密前:" + content1+"长度"+content.length());
        //加密
        String encrypted1 = encrypt(content1);
        //解密

        System.out.println("加密后:" + encrypted1);

        String decrypted = decrypt(encrypted);
        System.out.println("解密后:" + decrypted);

        String decrypted1 = decrypt("5uh0xttJfSbQVE5lIHMP7A==");
        System.out.println("解密后:" + decrypted1+"长度"+decrypted1.length());
    }

    /**
     * 加密返回的数据转换成 String 类型
     *
     * @param content 明文
     */
    public static String encrypt(String content) throws UnsupportedEncodingException {
        byte[] bytes= Base64.getEncoder().encode(aesCbcEncrypt(content.getBytes("UTF-8"), KEY.getBytes("UTF-8"), IV.getBytes("UTF-8")));
        return new String(bytes);

    }

    /**
     * 将解密返回的数据转换成 String 类型
     *
     * @param content Base64编码的密文
     */
    public static String decrypt(String content) throws UnsupportedEncodingException {
        byte[] bytes = Base64.getDecoder().decode(content.getBytes("UTF-8"));
        return new String((aesCbcDecrypt( byte s, KEY.getBytes("UTF-8"), IV.getBytes("UTF-8"))));
    }

    //加密
    private static byte[] aesCbcEncrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");  //指定加密方式
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //解密
    private static byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}  

文章来源:智云一二三科技

文章标题:AES加密介绍及样例

文章地址:https://www.zhihuclub.com/187125.shtml

关于作者: 智云科技

热门文章

网站地图