【Android】AES加密算法的Android实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

【Android】AES加密算法的Android实现 1import android.text.TextUtils;
2import android.util.Base64;
3import android.util.Log;
4
5import javax.crypto.Cipher;
6import javax.crypto.spec.IvParameterSpec;
7import javax.crypto.spec.SecretKeySpec;
8
9public class AESCBCUtils {
10private static final String TAG = "AESCBCUtils";
11
12// CBC(Cipher Block Chaining, 加密快链)模式,PKCS5Padding补码⽅式
13// AES是加密⽅式 CBC是⼯作模式 PKCS5Padding是填充模式
14/**
15 * 加解密算法/⼯作模式/填充⽅式
16*/
17private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
18// AES 加密
19private static final String AES = "AES";
20
21// 密钥偏移量
22//private static final String mstrIvParameter = "1234567890123456";
23/* key必须为16位,可更改为⾃⼰的key */
24//String mstrTestKey = "1234567890123456";
25
26// 加密
27/**
28 * AES 加密
29 *
30 * @param strKey 加密密钥
31 * @param strClearText 待加密内容
32 * @param mstrIvParameter 密钥偏移量
33 * @return返回Base64转码后的加密数据
34*/
35public static String encrypt_AES(String strKey, String strClearText, String mstrIvParameter){
36
37try {
38byte[] raw = strKey.getBytes();
39// 创建AES密钥
40 SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
41// 创建密码器
42 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
43// 创建偏移量
44 IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
45// 初始化加密器
46 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
47// 执⾏加密操作
48byte[] 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
54return strBase64Content;
55 } catch (Exception e) {
56 e.printStackTrace();
57 }
58
59return null;
60 }
61
62// 解密
63/**
64 * AES 解密
65 *
66 * @param strKey 解密密钥
67 * @param strCipherText 待解密内容
68 * @param mstrIvParameter 偏移量
69 * @return返回Base64转码后的加密数据
70*/
71public static String decrypt(String strKey, String strCipherText, String mstrIvParameter) throws Exception {
72
73try {
74byte[] raw = strKey.getBytes("ASCII");
75// 创建AES秘钥
76 SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
77// 创建密码器
78 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
79// 创建偏移量
80 IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
81// 初始化解密器
82 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
83// 执⾏解密操作
84byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT); // decode by BASE64 first 85//Log.d(TAG, "BASE64 decode result(): " + cipherText.toString());
86byte[] clearText = cipher.doFinal(cipherText);
87 String strClearText = new String(clearText);
88//Log.d(TAG, "decrypt result: " + strClearText);
89
90return strClearText;
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94
95return null;
96 }
97 }
在使⽤时可以直接调⽤:
1//密钥
2 String AESKey = "1234567890123456";
3//偏移量
4 String AESIv = "1234567890123456";
5
6//密码加密
7 String pwdAES = AESCBCUtils.encrypt_AES(AESKey, loginPassword.getText().toString(), AESIv);
8//⽤户名加密
9 String UserNameAES = AESCBCUtils.encrypt_AES(AESKey, loginAccount.getText().toString(), AESIv);在线AES加解密验证 |。

相关文档
最新文档