[Android] implementation of AES encryption algorithm

Time:2021-11-26
1 import android.text.TextUtils;
 2 import android.util.Base64;
 3 import android.util.Log;
 4 
 5 import javax.crypto.Cipher;
 6 import javax.crypto.spec.IvParameterSpec;
 7 import javax.crypto.spec.SecretKeySpec;
 8 
 9 public class AESCBCUtils {
10     private static final String TAG = "AESCBCUtils";
11 
12 // CBC (cipher block chaining) mode, pkcs5padding complement mode
13 // AES is the encryption mode, CBC is the working mode, and pkcs5padding is the filling mode
14     /**
15 * encryption and decryption algorithm / working mode / filling mode
16      */
17     private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
18 // AES encryption
19     private static final String AES = "AES";
20     
21 // key offset
22     //private static final String mstrIvParameter = "1234567890123456";
23 / * the key must be 16 bits and can be changed to its own key*/
24     //String mstrTestKey = "1234567890123456";
25 
26 // encryption
27     /**
28 * AES encryption
29      *
30 * @ param strkey encryption key
31 * @ param strcleartext content to be encrypted
32 * @ param mstrivparameter key offset
33 * @ return returns the encrypted data after Base64 transcoding
34      */
35     public static String encrypt_AES(String strKey, String strClearText, String mstrIvParameter){
36 
37         try {
38             byte[] raw = strKey.getBytes();
39 // create AES key
40             SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
41 // create a cipher
42             Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
43 // create offset
44             IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
45 // initialize the encryptor
46             cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
47 // perform encryption
48             byte[] cipherText = cipher.doFinal(strClearText.getBytes());
49             //Log.d(TAG, "encrypt result(not BASE64): " + cipherText.toString());
50             String strBase64Content = Base64.encodeToString(cipherText, Base64.DEFAULT); // encode it by BASE64 again
51             //Log.d(TAG, "encrypt result(BASE64): " + strBase64Content);
52             strBase64Content = strBase64Content.replaceAll(System.getProperty("line.separator"), "");
53 
54             return strBase64Content;
55         } catch (Exception e) {
56             e.printStackTrace();
57         }
58 
59         return null;
60     }
61 
62 // decryption
63     /**
64 * AES decryption
65      *
66 * @ param strkey decryption key
67 * @ param strciphertext content to be decrypted
68 * @ param mstrivparameter offset
69 * @ return returns the encrypted data after Base64 transcoding
70      */
71     public static String decrypt(String strKey, String strCipherText, String mstrIvParameter) throws Exception {
72 
73         try {
74             byte[] raw = strKey.getBytes("ASCII");
75 // create AES secret key
76             SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
77 // create cipher
78             Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
79 // create offset
80             IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
81 // initialize the decryptor
82             cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
83 // decrypt
84             byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT); // decode by BASE64 first
85             //Log.d(TAG, "BASE64 decode result(): " + cipherText.toString());
86             byte[] clearText = cipher.doFinal(cipherText);
87             String strClearText = new String(clearText);
88             //Log.d(TAG, "decrypt result: " + strClearText);
89 
90             return strClearText;
91         } catch (Exception e) {
92             e.printStackTrace();
93         }
94 
95         return null;
96     }
97 }

 

You can directly call:

1 // key
2 String AESKey = "1234567890123456";
3 // offset
4 String AESIv = "1234567890123456";
5 
6 // password encryption
7 String pwdAES = AESCBCUtils.encrypt_AES(AESKey, loginPassword.getText().toString(), AESIv);
8 // user name encryption
9 String UserNameAES = AESCBCUtils.encrypt_AES(AESKey, loginAccount.getText().toString(), AESIv);

 

Online AES encryption and decryption verification|Online AES encryption and decryption