基于Android系统的AES加密、解密的JAVA实现

网上很多code运行时会出现下面这个错误:
javax.crypto.BadPaddingException: pad block corrupted
多数是传入的key有问题,不妨试试下面的code。

以下代码在android L50、M60、N70、N71测试通过。采用AES + BASE64双重加密。

import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by williamvon on 17-5-9.
 */

public class AESUtils {
    private static final String TAG = "AESUtils";

    // CBC(Cipher Block Chaining, 加密快链)模式,PKCS7Padding补码方式
    // AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
    private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
    // AES 加密
    private static final String AES = "AES";
    // 密钥偏移量
    private static final String mstrIvParameter = "1234567890123456";
    /* key必须为16位,可更改为自己的key */
    //String mstrTestKey = "1234567890123456";

    // 加密
    public static String encrypt(String strKey, String strClearText) throws Exception {
        Log.d(TAG, "### begin encrypt: ");
        Log.d(TAG, "key = " + strKey + ",ClearText: " + strClearText);

        if (TextUtils.isEmpty(strClearText)) {
            Log.e(TAG, "clear text is empty.");
            return null;
        }

        if (null == strKey) {
            Log.e(TAG, "encrypt KEY is null.");
            return null;
        }

        // check the KEY is 16 or not
        if (16 != strKey.length()) {
            Log.e(TAG, "encrypt KEY length is 16.");
            return null;
        }

        try {
            byte[] raw = strKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);

            Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
            IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] cipherText = cipher.doFinal(strClearText.getBytes());
            Log.d(TAG, "encrypt result(not BASE64): " + cipherText.toString());
            String strBase64Content = Base64.encodeToString(cipherText, Base64.DEFAULT); // encode it by BASE64 again
            Log.d(TAG, "encrypt result(BASE64): " + strBase64Content);

            return strBase64Content;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    // 解密
    public static String decrypt(String strKey, String strCipherText) throws Exception {
        Log.d(TAG, "### begin decrypt: ");
        Log.d(TAG, "key = " + strKey + ",CipherText: " + strCipherText);

        if (TextUtils.isEmpty(strCipherText)) {
            Log.e(TAG, "cipher text is empty.");
            return null;
        }

        if (null == strKey) {
            Log.e(TAG, "decrypt KEY is null.");
            return null;
        }

        // check the KEY is 16 or not
        if (16 != strKey.length()) {
            Log.e(TAG, "decrypt KEY length is 16.");
            return null;
        }

        try {
            byte[] raw = strKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);

            Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
            IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT); // decode by BASE64 first
            Log.d(TAG, "BASE64 decode result(): " + cipherText.toString());
            byte[] clearText = cipher.doFinal(cipherText);
            String strClearText = new String(clearText);
            Log.d(TAG, "decrypt result: " + strClearText);

            return strClearText;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

更多内容读者可以参考以下文章:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值