AES加密解密(ECB模式),面试官问的那些Android原理你都懂吗

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进阶系统学习视频
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 AES 加密解密算法 ECB 模式的 C 语言实现。这个实现假设输入数据已经被填充到 AES 块大小的倍数(例如,128 位块大小的 AES 需要填充为 16 字节的倍数)。 ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include "aes.h" void aes_encrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_enc(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_encrypt(&ctx, input + i, output + i); } } void aes_decrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_dec(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_decrypt(&ctx, input + i, output + i); } } int main() { // Example usage uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; uint8_t input[32] = {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x31, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}; uint8_t output[32]; aes_encrypt_ecb(key, input, output, 32); aes_decrypt_ecb(key, output, input, 32); printf("Input: "); for (int i = 0; i < 32; ++i) { printf("%02x ", input[i]); } printf("\n"); printf("Output: "); for (int i = 0; i < 32; ++i) { printf("%02x ", output[i]); } printf("\n"); return 0; } ``` 这个实现使用了 Crypto++ 库中的 AES 实现。在 `aes.h` 头文件中定义了 `aes_context` 结构体,用于保存 AES 加密算法的上下文信息。`aes_setkey_enc()` 和 `aes_setkey_dec()` 函数分别用于设置加密和解密密钥。`aes_encrypt()` 和 `aes_decrypt()` 函数分别用于加密和解密数据块。在 `aes_encrypt_ecb()` 和 `aes_decrypt_ecb()` 函数中,使用了 `for` 循环来对输入数据进行分块并进行加密或解密操作。最后,`main()` 函数演示了如何使用这些函数来加密和解密数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值