Android实现ECB加解密

本文详细介绍了一种在Android开发中实现数据加密和解密的方法,通过ECB(电子密码本)模式使用AES算法,提供了具体的Java代码示例,包括AESUtils类的加密解密功能及MainActivity中的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在Android开发中,出于安全考虑,常常需要对字段进行加密或解密,以下为Android实现ECB(Electronic Codebook,电码本)加解密的具体代码。

AESUtils.java

定义一个AESUtils类,实现加密、解密等方法:

package com.example.ecb;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtils {
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";

    /**
     * SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
     */
    private SecretKeySpec key;

    public AESUtils(String hexKey) {
        key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM);
    }

    /**
     * AES加密
     * @param data
     * @return
     * @throws Exception
     */
    public String encryptData(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器
        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
        return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
    }

    /**
     * AES解密
     * @param base64Data
     * @return
     * @throws Exception
     */
    public String decryptData(String base64Data) throws Exception{
        Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(Base64.getDecoder().decode(base64Data)));
    }

    /**
     * hex字符串 转 byte数组
     * @param s
     * @return
     */
    private static byte[] hex2byte(String s) {
        if (s.length() % 2 == 0) {
            return hex2byte (s.getBytes(), 0, s.length() >> 1);
        } else {
            return hex2byte("0"+s);
        }
    }

    private static byte[] hex2byte (byte[] b, int offset, int len) {
        byte[] d = new byte[len];
        for (int i=0; i<len*2; i++) {
            int shift = i%2 == 1 ? 0 : 4;
            d[i>>1] |= Character.digit((char) b[offset+i], 16) << shift;
        }
        return d;
    }
}

MainActivity.java

MainActivity.java中使用AESUtils类的方法,对测试数据进行加解密:

package com.example.ecb;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.ecb.AESUtils;

public class MainActivity extends AppCompatActivity {
	private String key = "aaaabbbbccccdddd";//密钥
	private String data1 = "btI0LsoveoZ0C58fF4JTRw==";//待解密数据
	private String data2 = "faergtrf";//待加密数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //解密测试:
        AESUtils util = new AESUtils(key);
        try {
            Log.d("Jason", "decode result:"+ util.decryptData(data1)); //
        } catch (Exception e) {
            e.printStackTrace();
        }
        //加密测试:
        AESUtils util2 = new AESUtils(key);
        try {
            Log.d("Jason", "encode result:" + util2.encryptData(data2));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果

在安卓虚拟机或真机上运行程序,logcat中显示的log如下,成功完成了加解密:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值