This is the 23rd day of my participation in the August More Text Challenge

This article will demonstrate AES algorithm based on Java language. ECB mode will not be shown in this article because of its low security compared with other working modes (not recommended). If you really need to use it in work, please implement it by yourself.

Due to space constraints, all demo code will only show the key steps, if you want to get started, please go to github.com/aurthurxlc/…

Explain more data filling mode (M), is not to say only the ECB, CBC work mode can be populated with data, in fact all working mode can populate data encryption, only need to and can decrypt the party convention, but the ECB, CBC both working mode must fill, it is impossible to data encryption.

Encryption of the core code is very simple, because the Java JDK has been packaged well, directly call.

/** * AES encryption decrypts the main class */
public class AESCrypto {
    private final AESMode mode;
    private final SecretKey key;

    /** * constructor */
    public AESCrypto(AESMode mode, SecretKey key) {
        this.mode = mode;
        this.key = key;
    }

    /** * encryption */
    public byte[] encryptWithIV(byte[] plainText, IvParameterSpec iv)
            throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {

        Cipher cipher = Cipher.getInstance(mode.getMode());
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        return cipher.doFinal(plainText);
    }

    /** * decrypt */
    public byte[] decryptWithIV(byte[] cipherText, IvParameterSpec iv)
            throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {

        Cipher cipher = Cipher.getInstance(mode.getMode());
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        returncipher.doFinal(cipherText); }}Copy the code

Note that the encryptWithIV method returns a Byte Array value, which can be converted to Hex or Base64 if needed. See the ByteUtil class for details.

AESMode is my configuration enumeration class a common work mode, concrete AESMode class, can according to need to be expanded, the standard JDK support pattern please check: docs.oracle.com/javase/8/do…

Use our encryption and decryption code example:

public class AESCryptoTest {
    IvParameterSpec iv;
    SecretKey key;
    String pText;

    @Before
    public void init(a) throws NoSuchAlgorithmException {
        // Prepare parameters
        iv = AESTool.generateIV();
        key = AESTool.generateKey(128);
        pText = "AES algorithm based on Java practical demonstration";

        System.out.println("key: " + BytesUtil.bytesToHex(key.getEncoded()));
        System.out.println("iv: " + BytesUtil.bytesToHex(iv.getIV()));
        System.out.println("pText: " + pText);
    }

    @Test
    public void test(a) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {

        / / encryption
        AESCrypto aes = new AESCrypto(AESMode.CBC_PKCS5Padding, key);
        byte[] cBytes = aes.encryptWithIV(BytesUtil.stringToBytes(pText), iv);
        System.out.println("Encrypted cText:" + BytesUtil.bytesToHex(cBytes));

        / / decryption
        byte[] pBytes = aes.decryptWithIV(cBytes, iv);
        System.out.println("Decrypted pText:"+ BytesUtil.bytesToString(pBytes)); }}Copy the code

The following output is displayed:

Key: e43ee68382dc550fbd1d329486febdd4 iv: ddffc44a93503156abb36e9bbca876f8 pText: AES algorithm based on Java practical demonstration encrypted cText: E8aa678c21aa028988cd74ee2835344519014a4e9365cb8dda7cf24bfe95dfdf0e047cf979587b02500ccad15415b1c3 decrypted pText: AES algorithm based on Java field demoCopy the code