tvDecode = (TextView) findViewById(R.id.tv_decode);
initListener();
}
private void initListener() {
encryption.setOnClickListener(this);
decode.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_encryption://加密
String encryptionString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionString)) {
Toast.makeText(mContext, “请输入加密内容”, Toast.LENGTH_SHORT).show();
return;
}
encrypt = AESUtils.encrypt(encryptionString.getBytes(), key.getBytes());
tvEncryption.setText(new String(encrypt));
break;
case R.id.btn_decode://解密
String decodeString = tvEncryption.getText().toString().trim();
if (TextUtils.isEmpty(decodeString)) {
Toast.makeText(mContext, “请先加密”, Toast.LENGTH_SHORT).show();
return;
}
byte[] decrypt = AESUtils.decrypt(encrypt, key.getBytes());
tvDecode.setText(new String(decrypt));
break;
}
}
}
AESUtils
package tsou.com.encryption.aesecb;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
-
AES加密解密工具
-
@author huangxiaoguo
*/
public class AESUtils {
/**
-
AES加密
-
@param data
-
将要加密的内容
-
@param key
-
密钥
-
@return 已经加密的内容
*/
public static byte[] encrypt(byte[] data, byte[] key) {
//不足16字节,补齐内容为差值
int len = 16 - data.length % 16;
for (int i = 0; i < len; i++) {
byte[] bytes = { (byte) len };
data = ArrayUtils.concat(data, bytes);
}
try {
SecretKeySpec skeySpec = new SecretKeySpec(key, “AES”);
Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return new byte[] {};
}
/**
-
AES解密
-
@param data
-
将要解密的内容
-
@param key
-
密钥
-
@return 已经解密的内容
*/
public static byte[] decrypt(byte[] data, byte[] key) {
data = ArrayUtils.noPadding(data, -1);
try {
SecretKeySpec skeySpec = new SecretKeySpec(key, “AES”);
Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decryptData = cipher.doFinal(data);
int len = 2 + ByteUtils.byteToInt(decryptData[4]) + 3;
return ArrayUtils.noPadding(decryptData, len);
} catch (Exception e) {
e.printStackTrace();
}
return new byte[] {};
}
}
ArrayUtils
package tsou.com.encryption.aesecb;
/**
-
数组工具
-
@author huangxiaoguo
*/
public class ArrayUtils {
/**
-
合并数组
-
@param firstArray
-
第一个数组
-
@param secondArray
-
第二个数组
-
@return 合并后的数组
*/
public static byte[] concat(byte[] firstArray, byte[] secondArray) {
if (firstArray == null || secondArray == null) {
return null;
}
byte[] bytes = new byte[firstArray.length + secondArray.length];
System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
System.arraycopy(secondArray, 0, bytes, firstArray.length,
secondArray.length);
return bytes;
}
/**
-
去除数组中的补齐
-
@param paddingBytes
-
源数组
-
@param dataLength
-
去除补齐后的数据长度
-
@return 去除补齐后的数组
*/
public static byte[] noPadding(byte[] paddingBytes, int dataLength) {
if (paddingBytes == null) {
return null;
尾声
开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。
这是我在这行工作10几年积累的一些资料,如果还想继续在这行业走下去的,或者现在打算跳槽,可以**私信【学习】**我愿意把资料免费分享给大家。
或者直接点击下面链接领取
Android学习PDF+架构视频+面试文档+源码笔记
- 330页 PDF Android核心笔记
- 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题
- PDF和思维脑图,包含知识脉络 + 诸多细节
- Android进阶系统学习视频
- 330页 PDF Android核心笔记
[外链图片转存中…(img-uRRhDhi6-1710332538633)]
- 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题
[外链图片转存中…(img-mJI6acOF-1710332538633)]
[外链图片转存中…(img-C0edLL0s-1710332538634)]
- PDF和思维脑图,包含知识脉络 + 诸多细节
[外链图片转存中…(img-xqzUx9fb-1710332538634)]
- Android进阶系统学习视频