博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AES加密解密算法简介
阅读量:7181 次
发布时间:2019-06-29

本文共 3729 字,大约阅读时间需要 12 分钟。

hot3.png

前言

美国国家标准技术研究所在2001年发布了高级加密标准(AES)。AES是一个对称分组密码算法,旨在取代DES成为广泛使用的标准。 根据使用的密码长度,AES最常见的有3种方案,用以适应不同的场景要求,分别是AES-128、AES-192和AES-256。本文主要对AES-128进行介绍,另外两种的思路基本一样,只是轮数会适当增加。

AES加密过程图解

加密算法图解 AES加密过程涉及到4种操作:
  1. 字节替代(SubBytes)
  2. 行移位(ShiftRows)
  3. 列混淆(MixColumns)
  4. 轮密钥加(AddRoundKey)
解密过程分别为对应的逆操作。由于每一步操作都是可逆的,按照相反的顺序进行解密即可恢复明文。加解密中每轮的密钥分别由初始密钥扩展得到。算法中16字节的明文、密文和轮密钥都以一个4x4的矩阵表示。

加密代码示例

java加密代码

/**     * 对字符串加密     *     * @param str     * @return     * @throws InvalidKeyException     * @throws IllegalBlockSizeException     * @throws BadPaddingException     */    public byte[] encrytor(String str, String password) throws InvalidKeyException,            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException {        KeyGenerator kgen = KeyGenerator.getInstance("AES");        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");        random.setSeed(password.getBytes());        kgen.init(128, random);        SecretKey secretKey = kgen.generateKey();        byte[] enCodeFormat = secretKey.getEncoded();        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");        Cipher cipher = Cipher.getInstance("AES");        byte[] byteContent = str.getBytes("utf-8");        cipher.init(Cipher.ENCRYPT_MODE, key);        byte[] result = cipher.doFinal(byteContent);        return result;    }    /**     * 对字符串解密     *     * @param buff     * @return     * @throws InvalidKeyException     * @throws IllegalBlockSizeException     * @throws BadPaddingException     */    public byte[] decryptor(byte[] buff, String password) throws InvalidKeyException,            IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {        KeyGenerator kgen = KeyGenerator.getInstance("AES");        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");        random.setSeed(password.getBytes());        kgen.init(128, random);        SecretKey secretKey = kgen.generateKey();        byte[] enCodeFormat = secretKey.getEncoded();        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");        Cipher cipher = Cipher.getInstance("AES");        cipher.init(Cipher.DECRYPT_MODE, key);        byte[] result = cipher.doFinal(buff);        return result; // 加密    }    /**     * 将二进制转换成16进制     *     * @param buf     * @return     */    public String parseByte2HexStr(byte buf[]) {        StringBuffer sb = new StringBuffer();        for (int i = 0; i < buf.length; i++) {            String hex = Integer.toHexString(buf[i] & 0xFF);            if (hex.length() == 1) {                hex = '0' + hex;            }            sb.append(hex.toUpperCase());        }        return sb.toString();    }    /**     * 将16进制转换为二进制     *     * @param hexStr     * @return     */    public byte[] parseHexStr2Byte(String hexStr) {        if (hexStr.length() < 1)            return null;        byte[] result = new byte[hexStr.length() / 2];        for (int i = 0; i < hexStr.length() / 2; i++) {            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);            result[i] = (byte) (high * 16 + low);        }        return result;    }

加密解密示例

String content="www.buglife.cn";        //秘钥        String password = "buglife-secret";        byte[] encryptResult = encrytor(content, password);        //加密结果        String encryptResultStr = parseByte2HexStr(encryptResult);        //解密        byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);        byte[] decryptResult = decryptor(decryptFrom, password);        //解密结果        String res = new String(decryptResult);

转载于:https://my.oschina.net/crazyharry/blog/1105228

你可能感兴趣的文章
保证Linux Apache Web服务器安全的10个建议
查看>>
mysql-5.6.20 源码安装的一些问题
查看>>
Nginx与Apache、Tomcat、Resin动静分离核心配置
查看>>
Sublime Text使用入门8——扩展之命令
查看>>
EasyUI:easyui-combobox(清除选中项)
查看>>
window.location使用
查看>>
thinkphp框架开启页面gzip压缩
查看>>
gcc and g++分别是gnu的c & c++编译器
查看>>
centos 例行性工作转发外部邮箱
查看>>
工作中使用了一些触发器
查看>>
[每日一题] 11gOCP 1z0-052 :2013-09-7 The usage of the SQL*LOAD utility.............................
查看>>
我的友情链接
查看>>
async & await 的前世今生(Updated)
查看>>
揭开云“误”山的面纱
查看>>
Lua5.0 词法分析
查看>>
Solutions Log (2014-08)
查看>>
Java程序员从笨鸟到菜鸟之(七十八)细谈Spring(七)spring之JDBC访问数据库及配置详解...
查看>>
ElasticSearch(七)之elasticsearch集群搭建及参数详解
查看>>
Rails best practices
查看>>
Solaris 测试bonding的网卡是否可以 failover
查看>>