欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AES加密算法

程序员文章站 2022-07-10 15:30:40
...

添加maven依赖:


        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.codec</artifactId>
            <version>1.8</version>
        </dependency>

 

AES加密算法主要CBC,和EBC两种加密方法。两种加密方法都是是将明文分成128位的分组,EBC模式用秘钥对每个明文分组单独进行加密,而CBC模式后面分组的加密依赖前面分组参与加密过程,他需要一个秘钥KEY和一个初始向量IV

 

AES CBC加密模式需要一个初始向量IV,和秘钥KEY进行加密

CBC加密模式代码:

public class AESCBC {

    //解***(自行随机生成)
    public static final String KEY = "9B51BE373C236F20";//**key
    public static final String IV  = "6A136D8E7E8EEE78";//向量iv


    //加密
    public static String encrypt(String content){
        try {
            byte[] raw = KEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

            //"算法/模式/补码方式"
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //使用CBC模式,需要一个向量iv,可增加加密算法的强度
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
            byte[] encrypted = cipher.doFinal(content.getBytes());
            return new BASE64Encoder().encode(encrypted);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //解密
    public static String decrypt(String content){
        try {
            byte[] raw = KEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        String content = "666666";
        System.out.println("加密前:"+content);
        
        String ens1 = encrypt(content);

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

        System.out.println("解密后:" + decrypt(ens1));

    }
}

EBC加密模式代码:

public class AESECB {

    // AES加密要求key必须要128个比特位(这里需要长度为16,否则会报错)
    private static final String KEY = "9B51BE373C236F20";
    
    private static String encrypt(String content, String encryptKey){
        if(StringUtils.isEmpty(content) || StringUtils.isEmpty(encryptKey)){
            return null;
        }else {
            try {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                kgen.init(128);
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
                return Base64.encodeBase64String(cipher.doFinal(content.getBytes("utf-8")));
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }

        }

    }

    private static String decrypt(String encryptStr, String decryptKey){
        if(StringUtils.isEmpty(encryptStr) || StringUtils.isEmpty(decryptKey)){
            return null;
        }else {
            try {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                kgen.init(128);

                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
                byte[] decryptBytes = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptStr));

                return new String(decryptBytes);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    }
    public static void main(String[] args){
        String content = "12tffggfgfhgaeaddaa4524";

        System.out.println("加密前:" + content);

        System.out.println("加***和解***:" + KEY);

        String enc = encrypt(content, KEY);
        System.out.println("加密后:" + enc);

        String dec = decrypt(enc, KEY);

        System.out.println("解密后:" + dec);
    }

}

 

相关标签: java AES