Common encryption and decryption algorithms -AES

Copyright: Merry due share and eternity, knowledge sharing and immortal, I share my happiness ... https://blog.csdn.net/walter247443819/article/details/84839267

Common encryption and decryption algorithms -AES

A, AES encryption Overview

Advanced Encryption Standard (English: Advanced Encryption Standard, abbreviation: AES), also known as Rijndael cipher In cryptography, a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, it has been widely analyzed and multi- used around the world. After a five-year selection process, the Advanced Encryption Standard on November 26, 2001 issued by the US National Institute of Standards and Technology (NIST) in the FIPS PUB 197, and became effective standards in 2002, May 26. In 2006, the Advanced Encryption Standard has become one of the most popular symmetric key encryption algorithm.

Rijndael cipher design and strive to meet the following three criteria:

① resistant to all known attacks.
  ② speed on multiple platforms, compact coding.
  ③ simple design.
  Most of the current block cipher, which is Feistel structure round function.
  Rijndael no such structure.
  Rijndael round function consists of three different homogeneous reversible transform

In the application, although DES in security is fragile, but because of mass production fast DES chip, making the DES can still continue to use temporarily, in order to improve security strength, typically using three separate DES key. DES but sooner or later to be replaced by AES. Stream cryptosystem block cipher in theory than in mature and safe, but not included in the next generation of encryption standard.
AES encrypted data block packet length must be 128 bits, the key 128 may be any bit length, 192-bit, 256-bit one (if insufficient data block and key lengths will be filled). There are many rounds AES encryption and transformation repeated. The steps outlined below: 1, the Key Expansion (KeyExpansion), 2, initial round (Initial Round), 3, repeated rounds (Rounds), each one in turn comprising: SubBytes, ShiftRows, MixColumns, AddRoundKey , 4, the final wheel (Final round), the final round did not MixColumns.

AES is a symmetric encryption algorithm (PS: symmetric encryption is used to encrypt and decrypt the password using the password is the same, that is, asymmetric encryption and decryption key is not the same)
AES is a new alternative to DES encryption standard, we can not say It is the safest, such as when using asymmetric encryption, a safety factor will be higher.
Asymmetric encryption encryption and decryption factor different factors, can achieve higher strength encryption, such as the current digital certificate will use this model, but its operation is relatively slow.

参考链接:AES加密和解密参考链接

Two, Java implemented AES encryption and decryption
. 1, AES encryption and decryption java carrying jar package without the need to introduce other additional separate jar

2, AES encryption and decryption codes

package demo.security;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Scanner;

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

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/*
 * AES对称加密和解密
 */
public class SymmetricEncoder {
    /*
     * 加密
     * 1.构造密钥生成器
     * 2.根据ecnodeRules规则初始化密钥生成器
     * 3.产生密钥
     * 4.创建和初始化密码器
     * 5.内容加密
     * 6.返回字符串
     */
    public static String AESEncode(String encodeRules, String content) {
        try {
            //1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            //2.根据ecnodeRules规则初始化密钥生成器
            //生成一个128位的随机源,根据传入的字节数组
            keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            //3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte[] raw = original_key.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, "AES");
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte[] byte_encode = content.getBytes("utf-8");
            //9.根据密码器的初始化方式--加密:将数据加密
            byte[] byte_AES = cipher.doFinal(byte_encode);
            //10.将加密后的数据转换为字符串
            //这里用Base64Encoder中会找不到包
            //解决办法:
            //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
            String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
            //11.将字符串返回
            return AES_encode;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }
    /*
     * 解密
     * 解密过程:
     * 1.同加密1-4步
     * 2.将加密后的字符串反纺成byte[]数组
     * 3.将加密内容解密
     */
    public static String AESDncode(String encodeRules, String content) {
        try {
            //1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            //2.根据ecnodeRules规则初始化密钥生成器
            //生成一个128位的随机源,根据传入的字节数组
            keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            //3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte[] raw = original_key.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, "AES");
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            //8.将加密并编码后的内容解码成字节数组
            byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
            /*
             * 解密
             */
            byte[] byte_decode = cipher.doFinal(byte_content);
            String AES_decode = new String(byte_decode, "utf-8");
            return AES_decode;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        //如果有错就返加nulll
        return null;
    }

    public static void main(String[] args) {
        SymmetricEncoder se = new SymmetricEncoder();
        Scanner scanner = new Scanner(System.in);
        /*
         * 加密
         */
        System.out.println("使用AES对称加密,请输入加密的规则");
        String encodeRules = scanner.next();
        System.out.println("请输入要加密的内容:");
        String content = scanner.next();
        System.out.println("根据输入的规则" + encodeRules + "加密后的密文是:" + se.AESEncode(encodeRules, content));

        /*
         * 解密
         */
        System.out.println("使用AES对称解密,请输入加密的规则:(须与加密相同)");
        encodeRules = scanner.next();
        System.out.println("请输入要解密的内容(密文):");
        content = scanner.next();
        System.out.println("根据输入的规则" + encodeRules + "解密后的明文是:" + se.AESDncode(encodeRules, content));
    }

} 

输出结果:
复制代码
使用AES对称加密,请输入加密的规则
使用AES对称加密
请输入要加密的内容:
使用AES对称加密
根据输入的规则使用AES对称加密加密后的密文是:Z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw+AY=
使用AES对称解密,请输入加密的规则:(须与加密相同)
使用AES对称加密
请输入要解密的内容(密文):
Z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw+AY=
根据输入的规则使用AES对称加密解密后的明文是:使用AES对称加密

Guess you like

Origin blog.csdn.net/walter247443819/article/details/84839267