[JAVA实战篇] AES加密的JAVA实现及AES算法讲解

AES加密算法原理

1. S-P结构加密

AES加密算法是2001年由美国提出的互联网加密算法,从密码学的角度来讲,AES是典型的S-P结构加密。什么是S-P结构加密呢,手残博主画了一张图帮助大家理解:S-P加密结构
从这张图中可以看到,M明文经过了两次加密运算生成了密文,不过生成密文的过程中会伴随着大量的迭代运算,最后生成密文,这就是S-P结构的加密算法的大概流程。

2. AES加密算法

网上有很多AES加密算法的详细讲解,不过对于一些没有密码学基础或者高等数学底子不扎实的同学可能就是劝退教程(大佬请忽略)。
下面附上一个执行AES算法具体流程的链接:AES加密算法
如果你看完这篇博客已经很轻松的就理解了这个加密算法,您可以选择继续看脑残博主的小白讲解给博主一些建议或者纠正脑残博主的错误,或者节约时间做一些其他有意义的事情,比如自己实现代码~;如果你没有理解这到底再说什么,就让博主为你理清AES算法的逻辑思路叭。
重要提示:随博主理清逻辑思路后请再次点开刚才的链接,对AES算法进行较深入的学习。
这里脑残博主画了一张流程图来讲解AES算法的思路和大概流程,帮助大家理解AES算法:
AES加密思路
下面分段讲解这张流程图的思路和AES加密的逻辑思路:
可以看到手残博主在图中用红色框框标记的三个加密算法(字节代换、行移位与列混淆、轮密钥加),请先不要纠结这三个加密算法,你会发现当你把这三个加密算法合为一体的时候,它就是上文所说的S-P加密算法。这也是AES加密的逻辑架构。
当你很轻松的认为:“啊~就这?这就是AES加密”的时候,不错,你已经理解了AES加密的思路 [鼓掌],就是这么简单,但是请理清思路后继续了解具体的AES加密思路,因为这有助于你的代码实现。
加密顺序
相信大多数人在看到手残博主画的这张图的时候一定会认为实现程序的第一步在于输入明文~但是博主认为把第一步放在用户密钥上会更有助于对算法的理解。
第一步,在对用户密钥进行简单的密钥扩展以后,将扩展密钥放入待执行的程序之中等待加密算法调用;
第二步,输入明文,对明文进行初始的密钥加运算;
第三步,进入S-P加密结构,对处理过的密钥进行再次加工;
第四步,对第三步执行结束的字符串进行迭代控制,进行迭代加密;
第五步,密文跳出迭代,生成成功~

现在,你已经完全理解了AES加密的逻辑思路,请查看这里AES加密算法,了解AES加密每一步的具体运算。

3. AES加密的JAVA实现

如果你已经完全理解了AES的加密流程,可以通过下列代码对AES实现加密流程:

package com.mei.aes;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class AES {

    static final String algorithm="AES";

    public static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator secretGenerator=KeyGenerator.getInstance(algorithm);
        SecureRandom secureRandom=new SecureRandom();
        secretGenerator.init(secureRandom);
        SecretKey secretKey=secretGenerator.generateKey();
        return secretKey;

    }
    final static String charsetName="Utf-8";
    static Charset charset=Charset.forName(charsetName);

    public static byte[] encrypt(String content,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return aes(content.getBytes(charset),Cipher.ENCRYPT_MODE, secretKey);
    }

    public static String decrypt(byte[] contentArray,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] result= aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
        return new String(result,charsetName);

    }

    private static byte[] aes(byte[] contentArray,int mode,SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher=Cipher.getInstance(algorithm);
        cipher.init(mode, secretKey);
        byte[] result=cipher.doFinal(contentArray);
        return result;
    }

    public static void main(String[] args) {
        String content="你好,我是MAX";
        try {
            SecretKey secretKey=generateKey();
            byte[] encryptResult=encrypt(content,secretKey);
            System.out.println("加密后的结果为:"+new String(encryptResult,charsetName));
            String decrypetResult=decrypt(encryptResult,secretKey);
            System.out.println("解密后的结果为:"+decrypetResult);
        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unused")
    private static byte[] encrypt(String content) {
        // TODO Auto-generated method stub
        return null;
    }
}

4.代码讲解

其实博主写的是一个非常简单的AES加密算法,主要分为几个部分:

密钥部分

//生成密钥
public static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator secretGenerator=KeyGenerator.getInstance(algorithm);
        SecureRandom secureRandom=new SecureRandom();
        secretGenerator.init(secureRandom);
        SecretKey secretKey=secretGenerator.generateKey();
        return secretKey;

加密部分

public static byte[] encrypt(String content,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return aes(content.getBytes(charset),Cipher.ENCRYPT_MODE, secretKey);
    }
private static byte[] aes(byte[] contentArray,int mode,SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher=Cipher.getInstance(algorithm);
        cipher.init(mode, secretKey);
        byte[] result=cipher.doFinal(contentArray);
        return result;
    }

解密部分(解密不过是逆向的加密)

public static String decrypt(byte[] contentArray,SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] result= aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
        return new String(result,charsetName);

    }
private static byte[] aes(byte[] contentArray,int mode,SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher=Cipher.getInstance(algorithm);
        cipher.init(mode, secretKey);
        byte[] result=cipher.doFinal(contentArray);
        return result;
    }

主函数调用

public static void main(String[] args) {
        String content="你好,我是MAX";
        try {
            SecretKey secretKey=generateKey();
            byte[] encryptResult=encrypt(content,secretKey);
            System.out.println("加密后的结果为:"+new String(encryptResult,charsetName));
            String decrypetResult=decrypt(encryptResult,secretKey);
            System.out.println("解密后的结果为:"+decrypetResult);
        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

emmm…对AES加密算法的解析到这里就结束咯,标题3的代码直接运行就可以实现,后面的代码每个片段的实现代码也仅仅只有几行,相信各位大佬可以很快的理解~
另外如果大佬们博主有什么错误或者建议的话,欢迎在评论区批评指正~谢谢!

猜你喜欢

转载自blog.csdn.net/murongxuege/article/details/108815470
今日推荐