Easy AES encryption and decryption tool in Java

Keywords: encoding

1. Background

In the development process, set the public third-party callable interface. If the request is initiated through http, parameters and so on will be in the form of clear text. In order to prevent the interface from being exploded and opened, I use the method of clear text encrypted transmission here.

2. What is AES

Advanced Encryption Standard (AES) is a block encryption standard. This standard is used to replace the original DES, which has been analyzed by many parties and widely used all over the world.

So why is the original DES replaced? The reason is that it uses 56 bit key, which is easy to be cracked. AES can use 128, 192, and 256 bit keys, and use 128 bit packets to encrypt and decrypt data, which is relatively much safer. A perfect encryption algorithm can't be broken in theory unless the exhaustive method is used. It is unrealistic to use the exhaustive method to crack the encrypted data whose key length is more than 128 bits. Statistics show that even using the fastest computer in the world, it will take billions of years to exhaust the 128 bit key, let alone to crack the AES algorithm with 256 bit key length.

At present, there are still organizations in the world studying how to break through the solid wall of AES, but because the breaking time is too long, AES is guaranteed, but the time used is shrinking. With the increase of computer computing speed and the emergence of new algorithms, the attack on AES will only be more and more fierce and will not stop.

AES is now widely used in finance, online trading, wireless communication, digital storage and other fields, and has undergone the most rigorous test, but it may follow DES one day.

3. Code plus test

It has solved the situation of Chinese scrambling in the transmission process. If "+" cannot be received, the + sign will be converted to% 2B in the encryption string. If other symbols cannot be received in the transmission, they can be converted to the corresponding hexadecimal encoding format for transmission.

public class AESUtils {

    public static final String AES_KEY = "edFabJn3ZA==7JVk";//Key Custom 16 bit

    /**
     * AES Encryption + Base64 transcoding
     *
     * @param data Plaintext (HEX)
     * @return
     */
    public static String encrypt(String data) {
        byte[] keyb = null;
        try {
            keyb = AES_KEY.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } // Plaintext
        SecretKeySpec sKeySpec = new SecretKeySpec(keyb, "AES");
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] bjiamihou = null;
        String miwen = "";
        try {
            bjiamihou = cipher.doFinal(data.getBytes("utf-8"));
            // After byte encryption
            miwen = Base64.encodeBase64String(bjiamihou);// Ciphertext encrypted with base64
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return miwen;
    }

    /**
     * Base64 Decoding + AES decoding
     *
     * @param data Ciphertext (HEX)
     * @return
     */
    public static String decrypt(String data){
        byte[] keyb = null;
        try {
            keyb = AES_KEY.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        byte[] miwen = Base64.decodeBase64(data);
        SecretKeySpec sKeySpec = new SecretKeySpec(keyb, "AES");
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] bjiemihou = null;
        String mingwen = "";
        try {
            bjiemihou = cipher.doFinal(miwen);
            // After byte encryption
            mingwen = new String(bjiemihou,"utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mingwen;
    }

    public static void main(String[] args) throws Exception {
        // Test encryption tool class
        String data = "{\"appCode\":\"portal\",\"createtime\":1592981430336,\"msgContent\":\"Here is the body of the notice\",\"msgSignature\":\"Center( Janson)\",\"msgStatus\":0,\"msgTitle\":\"Test notification task\",\"msgType\":1,\"objectCreateTime\":1592981430322,\"objectId\":\"123123123Id\",\"permission\":0,\"receiveUsers\":\"450503\",\"sendObject\":1,\"status\":0,\"userType\":1}";//Plaintext
        String miwen = encrypt(data);// encryption
        System.out.println(miwen);
        System.out.println(decrypt(miwen));// decrypt

    }

}

 

Posted by alpinec on Sun, 28 Jun 2020 19:12:40 -0700