Android AES加解密优化

该博客详细介绍了在Android平台上实现AES加解密的步骤,包括使用CBC模式、PKCS5Padding填充以及Base64编码解码。通过一个静态内部类`AES`提供了加密和解密的公共方法,确保了接口请求数据的安全性。同时,博客还讨论了如何避免常见的加密异常,并优化了加解密过程的性能。

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

package com.wangzhong.fortune.utils;


import android.util.Base64;

import com.wangzhong.fortune.GlobalConstants;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    private static AES mHttpAes; // 接口请求加密
    private final static String KEY = GlobalConstants.AES_KEY; //配置key
    private final String IV = "csc-api-iv-param";
    private final String WAY = "AES/CBC/PKCS5Padding";
    private IvParameterSpec ivParameterSpec;
    private SecretKeySpec secretKeySpec;
    private Cipher mCipherEncrypt, mCipherDecrypt;

    private AES(String key) {
        // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
        byte[] enCodeFormat = key.getBytes();
        byte[] initParam = IV.getBytes();
        ivParameterSpec = new IvParameterSpec(initParam);
        // 指定加密的算法、工作模式和填充方式
        secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        try {
            mCipherEncrypt = Cipher.getInstance(WAY);
            mCipherDecrypt = Cipher.getInstance(WAY);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public static AES getHttpAes() {
        if (mHttpAes == null) {
            synchronized (AES.class) {
                if (mHttpAes == null) {
                    mHttpAes = new AES(KEY);
                }
            }
        }
        return mHttpAes;
    }


    private String encryptAES(String content) {
        byte[] encryptedBytes = new byte[0];
        try {
            byte[] byteContent = content.getBytes("utf-8");
            mCipherEncrypt.init(Cipher.ENCRYPT_MODE, secretKeySpec,
                    ivParameterSpec);
            encryptedBytes = mCipherEncrypt.doFinal(byteContent);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        // 同样对加密后数据进行 base64 编码
        return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
    }

    private String decryptAES(String content) {
        // base64 解码
        byte[] encryptedBytes = Base64.decode(content, Base64.DEFAULT);
        byte[] result = new byte[0];
        try {
            mCipherDecrypt.init(Cipher.DECRYPT_MODE, secretKeySpec,
                    ivParameterSpec);
            result = mCipherDecrypt.doFinal(encryptedBytes);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        String re = new String(result, Charset.forName("utf-8"));
        return re;
    }

    public static String encryptHttp(String content) { //加密接口数据
        return getHttpAes().encryptAES(content);
    }

    public static String decryptHttp(String content) { //解密接口数据
        return getHttpAes().decryptAES(content);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值