AES encryption of Android data encryption

Advanced Encryption Standard (AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the federal government of the United States. This standard is used to replace the original DES, which has been analyzed by many parties and widely used all over the world. In short, it is the enhanced version of DES, which has higher encryption strength than des.

Like DES, AES has four encryption modes: electronic cipher mode (ECB), encryption block link mode (CBC), encryption feedback mode (CFB) and output feedback mode (OFB). For the introduction of encryption mode, this article is recommended: Working mode of AES (ECB, CBC, CFB, OFB)

Direct to code:

Encryption (byte level)

    /* key used for encryption */
    private static final String AES_KEY = "KUbHwTqBy6TBQ2gN";
    /* IV used for encryption */
    private static final String AES_IV = "pIbF6GR3XEN1PG05";

 /**
     * AES encryption
     *
     * @param content Content to be encrypted
     * @param key     secret key
     * @return Encrypted data
     */
    public static byte[] encryptAES(byte[] content, byte[] key) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            // AES is encryption mode, CBC is working mode, PKCS5Padding is filling mode
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            // IV is the initial vector, which can enhance the strength of the password
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(AES_IV.getBytes()));
            return cipher.doFinal(content);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

Decryption (byte level)

/**
     * AES Decrypt
     *
     * @param content Content to be decrypted
     * @param key     secret key
     * @return Decrypted data
     */
    public static byte[] decryptAES(byte[] content, byte[] key) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(AES_IV.getBytes()));
            return cipher.doFinal(content);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

Encryption (String level)

    private final static String TOKEN_KEY = "91a29fa7w46d8x41";

    public static String encrypt(String plain) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
            SecretKeySpec newKey = new SecretKeySpec(TOKEN_KEY.getBytes(), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
            return new String(cipher.doFinal(plain.getBytes()));
        } catch (Exception e) {
            Ln.e(e);
            return null;
        }
    }


Decryption (String level)

 public static String decrypt(String encoded) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
            SecretKeySpec newKey = new SecretKeySpec(TOKEN_KEY.getBytes(), "AES");
            cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
            return new String(cipher.doFinal(encoded.getBytes()));
        } catch (Exception e) {
            Ln.e(e);
            return null;
        }
    }

Posted on Mon, 06 Jan 2020 12:37:56 -0800 by saadshams