0
点赞
收藏
分享

微信扫一扫

AES加密解密(ECB模式),kotlin静态编译

boom莎卡拉卡 2022-01-31 阅读 82

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;

/**

  • 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;

}

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;

}

/**

  • 获取补齐的位置

  • @param paddingBytes

  •        源数组 
    
  • @return 补齐的位置

*/

结尾

  • 腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升

img

  • 一线互联网Android面试题含详解(初级到高级专题)

img

  •        源数组 
    
  • @return 补齐的位置

*/

结尾

  • 腾讯T4级别Android架构技术脑图;查漏补缺,体系化深入学习提升

[外链图片转存中…(img-ZV3lFkpy-1643517866562)]

  • 一线互联网Android面试题含详解(初级到高级专题)

[外链图片转存中…(img-z10CjNiM-1643517866564)]

举报

相关推荐

0 条评论