decode = (Button) findViewById(R.id.btn_decode);
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;
public class AESUtils {
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[] {};
}
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;
public class ArrayUtils {
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;
}
public static byte[] noPadding(byte[] paddingBytes, int dataLength) {
if (paddingBytes == null) {
return null;
}
byte[] noPaddingBytes = null;
if (dataLength > 0) {
if (paddingBytes.length > dataLength) {
noPaddingBytes = new byte[dataLength];
System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, dataLength);
} else {
noPaddingBytes = paddingBytes;
}
} else {
int index = paddingIndex(paddingBytes);
if (index > 0) {
noPaddingBytes = new byte[index];
System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, index);
}
}
return noPaddingBytes;
}
结尾腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我
源数组
@return 补齐的位置
*/
结尾腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升
[外链图片转存中…(img-ZV3lFkpy-1643517866562)]
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
[外链图片转存中…(img-z10CjNiM-1643517866564)]
有Android开发3-5年基础,希望突破瓶颈,成为架构师的小伙伴,可以关注我
本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)