Android-AES加解密

久未见 提交于 2020-01-25 21:59:27

项目Aes功能背景:
早期用的是jni写的aes加密算法,其实android 本身就实现了aes算法。
于是封装了一个工具类(实际元素值要变,比如加密模式,偏移量等等),这里写法只是参考,封装的有点欠妥,但思路简介易懂,可以随手修改。

在这里插入图片描述
加密在线链接
做这块要与服务器的算法一致才是可以,所以要对以上的元素(加密模式,填充、数据块,偏移量等元素,仔细校验。)

偏移量最少:16字节长度 那就是填0000000000000000


import android.util.Base64;

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

/**
 *
 * android 对应的aes 工具  --- 可以替换本项目中的jni aes
 * @author bingley
 * @date 2020/1/13.
 */
public class AESUtils {


    public static String algorithm = "AES/CBC/PKCS5Padding"; //使用的算法 算法/模式/补码方式, 目前支持ECB和CBC模式
    public static String ivParameter = "0000000000000000"; // -- 偏移量,CBC模式时需要
    public static String encodingFormat = "UTF-8"; //-- 字符串编码方式
    public static String sKey = "ncgZd87hCrDmLCDE";  // -- 加密密钥


    private static final int keyLenght = 32;
    private static final String defaultV = "0";

    /**
     * AES加密,返回BASE64编码后的加密字符串
     *
     * @param sSrc -- 待加密内容
     * @param sKey -- 加密密钥
     * @throws Exception
     */
    public static String aesEncrypt(String sSrc, String sKey) throws Exception {
        String toMakekey = toMakekey(sKey, keyLenght, defaultV);


        Cipher cipher = Cipher.getInstance(algorithm);
        byte[] raw = toMakekey.getBytes(encodingFormat);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        //使用CBC模式,需要一个向量iv,可增加加密算法的强度
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(encodingFormat));
        if (algorithm.contains("CBC")) {
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        }
        byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
        return new String(Base64.encode(encrypted, Base64.NO_WRAP));
    }


    /**
     * AES解密
     *
     * @param sSrc -- 待解密Base64字符串
     * @param sKey -- 加密密钥
     * @return 解密后的字符串
     * @throws Exception
     */
    public static String aesDecrypt(String sSrc, String sKey) throws Exception {
        String toMakekey = toMakekey(sKey, keyLenght, defaultV);


        byte[] raw = toMakekey.getBytes(encodingFormat);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance(algorithm);
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(encodingFormat));
        if (algorithm.contains("CBC")) {
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        } else {//ECB模式
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        }
        //先用base64解密
        byte[] encrypted1 = Base64.decode(sSrc, Base64.NO_WRAP);
        byte[] original = cipher.doFinal(encrypted1);
        return new String(original, encodingFormat);
    }


    /**
     * 密钥key ,默认补的数字,补全16位数,以保证安全补全至少16位长度,android和ios对接通过
     * @param str
     * @param strLength
     * @param val
     * @return
     */
    private static String toMakekey(String str, int strLength, String val) {

        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer buffer = new StringBuffer();
                buffer.append(str).append(val);
                str = buffer.toString();
                strLen = str.length();
            }
        } else {
            str = str.substring(0, 33);
        }
        return str;
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!