Android中AES的加密和解密以及秘钥安全问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010872619/article/details/83106422

import android.content.Context;
import android.text.TextUtils;

import android.util.Base64;

import com.yzzh.cateringsystem.BuildConfig;
import com.yzzh.cateringsystem.R;

import java.math.BigInteger;

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

import Decoder.BASE64Decoder;


/**
 * @作者:TJ
 * @时间:2018/7/23 16:23
 * @描述:AES的加密和解密
 * @注意点: 
 * 1.Base64.encodeToString(bytes, Base64.DEFAULT)这个方法必须要用Android的库android.util.Base64,不然会报错
 * 2.因为Android系统原因,秘钥不能保证绝对安全,只能增加破解难度,这里主要通过将秘钥分解,存放在不同的位置,获取的时候在组合;
 */
public class Aes {

    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    /**
     * 秘钥第一部分:配置文件
     *
     * @return
     */
    public static String getSk1() {
        return BuildConfig.SK1;
    }

    /**
     * 秘钥第二部分:资源文件
     *
     * @param context
     * @return
     */
    public static String getSK2(Context context) {
        return context.getString(R.string.sk2);
    }


    /**
     * 秘钥第三部分:java文件(建议加一些复杂的算法获取)
     *
     * @return
     */
    public static String getSK3() {
        return "6666";
    }

    /**
     * 获取秘钥
     *
     * @param context
     * @return
     */
    public static String getSecretKey(Context context) {
        return plusString(getSk1(), getSK2(context), getSK3());
    }


    /**
     * aes解密
     *
     * @param encrypt 内容
     * @return
     * @throws Exception
     */
    public static String aesDecrypt(Context context, String encrypt) {
        try {
            return aesDecrypt(encrypt, getSecretKey(context));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * aes加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesEncrypt(Context context, String content) {
        try {
            return aesEncrypt(content, getSecretKey(context));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将byte[]转为各种进制的字符串
     *
     * @param bytes byte[]
     * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    /**
     * base 64 encode
     *
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    /**
     * base 64 decode
     *
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception {
        return TextUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }


    /**
     * AES加密
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     * @throws Exception
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }


    /**
     * AES加密为base 64 code
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * AES解密
     *
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey   解密密钥
     * @return 解密后的String
     * @throws Exception
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }


    /**
     * 将base 64 code AES解密
     *
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return TextUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    /**
     * 拼接字符串
     *
     * @param strs
     * @return
     */
    public static String plusString(String... strs) {
        StringBuilder builder = new StringBuilder();
        String[]      var2    = strs;
        int           var3    = strs.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String str = var2[var4];
            if (!TextUtils.isEmpty(str)) {
                builder.append(str);
            }
        }
        return builder.toString();
    }
}

这里还导入了一个jar包 sun.misc.BASE64Decoder.jar
点击下载最新版

猜你喜欢

转载自blog.csdn.net/u010872619/article/details/83106422