AES算法学习02:原理总结和实现(ECB)

一 原理介绍:


其实AES就是对16byte(128bit)数据进行加密的过程。说白了就是把128位通过一系列的变化变成另一个128数据。


      这里主要用到2个关键的东西。密钥(key)这个是绝对不能省的。key要先扩张,然后进行10次的行列变化,与数据进行抑或操作。最终才能得到加密后的数据。

     此外还有一个东西 就是初始向量(IV)。 其实还真可以不用它,用它只是可以把加密变得更难破解。

     具体用法:

u8 data[16]={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

u8 IV[16]={ 0x30, 0x31, 0x32, 0x33,
  0x34, 0x35, 0x36, 0x37,
  0x38, 0x39, 0x61, 0x62,
  0xd63, 0x64, 0x65, 0x66};

其实只要把

 for(i=0;i<16;i++)
 {
  data[i]=data[i]^IV[i];
 }

然后再把data作为新的数据加密就OK了。

当然你也可以不用他。

那么如果数据不是16的倍数改怎么办呢。这里我们就用到的数据扩张


AES支持支持几种填充:NoPadding,PKCS5Padding,ISO10126Padding,PaddingMode.Zeros;PaddingMode.PKCS7;

这里要说明一下PKCS5Padding和PKCS7Padding是一样的(原因可以参考http://www.cnblogs.com/midea0978/articles/1437257.html)。


PKCS7Padding:

      PKCS7 就是数据少几个就填充几个。比如数据{1,2,3,4,5,6,7,8,9,10},少了6个,那么就填充6个6(注意是0x06,而不是字符6,字符6实际上是0x36)即:

{1,2,3,4,5,6,7,8,9,10,6,6,6,6,6,6}

注意一定要是16个数据(1个数据是8位,这样就是128位)这样才能进行AES加密。

切记!


PaddingMode.Zeros

这个填充方式,个人比较喜欢。就是在后面补充0,无论缺多少就补多少个0。

再提醒一下就是如果刚满16个,那就要在补充称16个字节。一定要比原先的多。(每种补充都要满足这样。这里非常容易被忽略).

也就是说如果{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

PaddingMode.Zeros模式

就要补充称{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

PKCS7Padding模式就要{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16}.这样再代入加密算法才是最正直的AES。


二 参数介绍:

AES密钥、初始向量、加密模式、填充方式

        我们常说的AES-128、AES-192、AES-256三种加密方式,数字表示的是密钥长度,比如AES-128说的是密钥是128位,也就是16个字节长度的字符串。JDK目前只支持AES-128加密,也就是传入的密钥必须是长度为16的字符串。

        初始向量Iv(Initialization Vector),使用除ECB以外的其他加密模式均需要传入一个初始向量,其大小与块大小相等,AES块大小是128bit,所以Iv的长度是16字节,初始向量可以加强算法强度。

加密模式(Cipher Mode)有CBC、ECB、CTR、OFB、CFB五种。

        填充方式(Padding)决定了最后的一个块需要填充的内容,填充方式有PKCS5Padding、PKCS7Padding、NOPADDING三种,但是JDK只提供了PKCS5Padding、NOPADDING两种,填充方式为PKCS5Padding时,最后一个块需要填充χ个字节,填充的值就是χ;填充方式为NOPADDING时,最后的一个块填充的内容由程序员自己决定,通常填充0。



三 在线工具:

可以使用实现工具现行测试自己需要加密的数据,可选择不同参数进行对比,

加密解密工具:http://tool.chacuo.net/cryptaes


四 具体使用(java):

说明:这里参考的代码中,原作者在密码补位上似乎理解有偏差,补位应补16进制0,此处作者理解为加密内容的补位,密码不足16位需要补位的读者注意!若密码就为16位,忽略本段话。

算法:AES
模式:ECB
密钥长度:128位
密钥:自己填( 代码中带补位功能
补码方式:PKCS5Padding/PKCS7Padding (这两个补码方式出来的结果都一样,没有区别
加密结果编码方式:十六进制/base64(加密解密的方法里面两种编码方式的代码都有)

AES.java

 import java.math.BigInteger;

        import javax.crypto.Cipher;
        import javax.crypto.spec.SecretKeySpec;

        import org.apache.commons.codec.binary.Base64;
        import org.apache.commons.codec.binary.Hex;

public class AES {

    /**
     * 加密
     * @param String src 加密字符串 
     * @param String key 密钥
     * @return 加密后的字符串
     */
    public static String Encrypt(String src, String key) throws Exception {
        // 判断密钥是否为空
        if (key == null) {
            System.out.print("密钥不能为空");
            return null;
        }

        // 密钥补位
        int plus= 16-key.length();
        byte[] data = key.getBytes("utf-8");
        byte[] raw = new byte[16];
        byte[] plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
        for(int i=0;i<16;i++)
        {
            if (data.length > i)
                raw[i] = data[i];
            else
                raw[i] = plusbyte[plus];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");    // 算法/模式/补码方式 
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(src.getBytes("utf-8"));

        //return new Base64().encodeToString(encrypted);//base64
        return binary(encrypted, 16); //十六进制
    }

    /**
     * 解密
     * @param String src 解密字符串 
     * @param String key 密钥
     * @return 解密后的字符串
     */
    public static String Decrypt(String src, String key) throws Exception {
        try {
            // 判断Key是否正确
            if (key == null) {
                System.out.print("Key为空null");
                return null;
            }

            // 密钥补位
            int plus= 16-key.length();
            byte[] data = key.getBytes("utf-8");
            byte[] raw = new byte[16];
            byte[] plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
            for(int i=0;i<16;i++)
            {
                if (data.length > i)
                    raw[i] = data[i];
                else
                    raw[i] = plusbyte[plus];
            }

            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);

            //byte[] encrypted1 = new Base64().decode(src);//base64
            byte[] encrypted1 = toByteArray(src);//十六进制

            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    /**
     * 将byte[]转为各种进制的字符串 
     * @param bytes byte[] 
     * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制 
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix){
        return new BigInteger(1, bytes).toString(radix);   // 这里的1代表正数  
    }

    /**
     * 16进制的字符串表示转成字节数组
     *
     * @param hexString 16进制格式的字符串            
     * @return 转换后的字节数组
     **/
    public static byte[] toByteArray(String hexString) {
        if (hexString.isEmpty())
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static void main(String[] args) throws Exception {
        // 密钥
        String key = "smph20151208shao";
        // 需要加密的字符串
        String src = "出版社";

        System.out.println(src);

        // 加密
        String enString = Encrypt(src, key);
        System.out.println("加密后的字串是:" + enString);

        // 解密
        String DeString = Decrypt(enString, key);

        System.out.println("解密后的字串是:" + DeString);
    }

}  

运行结果:

出版社
加密后的字串是:c4735c066ad590cb11e0fd6084592df2

解密后的字串是:出版社

------------------------------------------分隔符------------------------------------------

今天拿到了客户加密后的文件,发觉XML是整个文件加密,且按照每三个字符加密,加密后的字符串带换行,XML开始位置竟然还有乱码。

修改后的解密代码如下:


/**
 * 解密
 * @param String src 解密字符串 
 * @param String key 密钥
 * @return 解密后的字符串
 */
public static String decrypt(String src, String key) throws Exception {
    try {
        // 判断Key是否正确
        if (key == null) {
            System.out.print("Key为空null");
            return null;
        }

        // 密钥补位
        int plus= 16-key.length();
        byte[] data = key.getBytes("utf-8");
        byte[] raw = new byte[16];
        byte[] plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
        for(int i=0;i<16;i++)
        {
            if (data.length > i)
                raw[i] = data[i];
            else
                raw[i] = plusbyte[plus];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        //byte[] encrypted1 = new Base64().decode(src);//base64
        byte[] encrypted1 = toByteArray(src);//十六进制

        try {
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original,"utf-8");
            return originalString;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
        return null;
    }
}

/**
 * 将byte[]转为各种进制的字符串 
 * @param bytes byte[] 
 * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制 
 * @return 转换后的字符串
 */
public static String binary(byte[] bytes, int radix){
    return new BigInteger(1, bytes).toString(radix);   // 这里的1代表正数  
}

/**
 * 16进制的字符串表示转成字节数组
 *
 * @param hexString 16进制格式的字符串            
 * @return 转换后的字节数组
 **/
public static byte[] toByteArray(String hexString) {
    if (hexString.isEmpty())
        throw new IllegalArgumentException("this hexString must not be empty");

    hexString = hexString.toLowerCase();
    final byte[] byteArray = new byte[hexString.length() / 2];
    int k = 0;
    for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
        byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
        byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
        byteArray[i] = (byte) (high << 4 | low);
        k += 2;
    }
    return byteArray;
}

/**
 * 执行解码并生成解码后文件
 *
 * @param sourceFile 加密文件
 * @param targetFile 解密后生成文件
 **/
public static void decryptFile(String sourceFile, String targetFile) {
    // 密钥
    String key = "smbk20160101shao";

    try{
        File file = new File(targetFile);
        if (file.exists())
            file.delete();
        file.createNewFile();

        // read net source file to local target file
        URL url = new URL(encodeUrlToUTF8(sourceFile));
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String s;
        while ((s = reader.readLine()) != null) {
            writeFile(targetFile, decrypt(changeString(s), key));
        }
        reader.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

/**
 *
 * Url中的中文转换
 *
 */
public static String encodeUrlToUTF8(String url) throws UnsupportedEncodingException{
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < url.length(); i++) {
        String s = url.substring(i, i + 1);
        byte[] bytes = s.getBytes("UTF-8");
        // 中文字符是2个字节,符号和英文为1个字节  
        if (bytes.length == 1) {
            if (bytes[0] == ' ')
                sb.append("%20");
            else
                sb.append(s);
        } else {
            sb.append(URLEncoder.encode(s, "UTF-8"));
        }
    }
    return sb.toString();
}


/**
 *
 * 除去乱码
 *
 */
private static String changeString(String str){
    String str_Result = "", str_OneStr = "";
    for (int z = 0; z < str.length(); z++) {
        str_OneStr = str.substring(z, z + 1);
        if (!str_OneStr.matches("[\u4e00-\u9fa5]+")) {
            if (str_OneStr.matches("[\\x00-\\x7F]+")) {
                str_Result = str_Result + str_OneStr;
            }
        }
    }
    return str_Result;
}

/**
 *
 * 将内容写入输出文件
 *
 */
public static void writeFile(String fileName, String content) throws IOException{
    FileWriter fw = new FileWriter(fileName, true);
    fw.write(content);
    //刷新缓冲区
    fw.flush();
    //关闭文件流对象
    fw.close();
}

调用:

decryptFile("http://localhost:8080/Test/Source/test.xml", "D:/Test/test.xml")

博文参考资料:

1: AES-128  ECB 加密有感  http://blog.sina.com.cn/s/blog_60cf051301015orf.html

2:AES/ECB/PKCS5Padding/PKCS7Padding 128位密钥(带密钥补位功能)加密解密

https://blog.csdn.net/sunroyi666/article/details/52300443

3:java使用AES加密解密 AES-128-ECB加密  :https://www.cnblogs.com/chen-lhx/p/5817161.html

4:AES加密算法的理解及Java实现  https://blog.csdn.net/qq_29078329/article/details/73611074



猜你喜欢

转载自blog.csdn.net/lucasxu01/article/details/80958372