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

来源:互联网 发布:聚才道软件jucaidaorj 编辑:程序博客网 时间:2024/05/17 16:55

网上很多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;    }}

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

  • javax.crypto.Cipher类提供加密和解密功能,该类是JCE框架的核心。
  • 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
  • Android AES加密解密
  • Android数据加密之Aes加密
  • android使用AES加密和解密文件
0 0
原创粉丝点击