android之AES加密

来源:互联网 发布:fifa聚宝盆网络繁忙 编辑:程序博客网 时间:2024/05/29 11:43

使用环境:

需要加密用户的账号和密码

使用方法:

1 在线加密解密:

http://www.seacha.com/tools/aes.html?src=helloworld&mode=ECB&keylen=128&key=admin&iv=&bpkcs=pkcs5padding&session=0ZaE1yBj5XAle8hyW91S&aes=8865a0062c87452c6ca58e0bb4e9b98f&encoding=base64&type=0

2  内容简要:



3 使用方法:

缺什么你调什么,如果需要用到base64就将base64转换的方法加进去

        4 AES的类(有不懂的或者使用的时候有疑问,联系我532245792)

public class AES {    public static String Encrypt(String src, String key) throws Exception {        // 判断密钥是否为空        if (key == null) {            System.out.print("密钥不能为空");            return null;        }        // 密钥补位        int plus = 16 - key.length();        byte[] data = key.getBytes("utf-8");        byte[] raw = new byte[16];        byte[] plusbyte = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};        for (int i = 0; i < 16; i++) {            if (data.length > i) raw[i] = data[i];            else raw[i] = plusbyte[plus];        }        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 算法/模式/补码方式        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);        byte[] encrypted = cipher.doFinal(src.getBytes("utf-8"));        //return new Base64().encodeToString(encrypted);//base64        return binary(encrypted, 16); //十六进制    }    /**     * 解密     */    public static String Decrypt(String src, String key) throws Exception {        try {            // 判断Key是否正确            if (key == null) {                System.out.print("Key为空null");                return null;            }            // 密钥补位            int plus = 16 - key.length();            byte[] data = key.getBytes("utf-8");            byte[] raw = new byte[16];            byte[] plusbyte = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};            for (int i = 0; i < 16; i++) {                if (data.length > i) raw[i] = data[i];                else raw[i] = plusbyte[plus];            }            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");            cipher.init(Cipher.DECRYPT_MODE, skeySpec);            //byte[] encrypted1 = new Base64().decode(src);//base64            byte[] encrypted1 = toByteArray(src);//十六进制            try {                byte[] original = cipher.doFinal(encrypted1);                String originalString = new String(original, "utf-8");                return originalString;            } catch (Exception e) {                System.out.println(e.toString());                Log.e("log","解密失败1=》"+ e.toString());                return null;            }        } catch (Exception ex) {            System.out.println(ex.toString());            Log.e("log","解密失败2=》"+ ex.toString());            return null;        }    }    /**     * 将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代表正数    }    /**     * 16进制的字符串表示转成字节数组     *     * @param hexString 16进制格式的字符串     * @return 转换后的字节数组     **/    public static byte[] toByteArray(String hexString) {        if (hexString.isEmpty())            throw new IllegalArgumentException("this hexString must not be empty");        hexString = hexString.toLowerCase();        final byte[] byteArray = new byte[hexString.length() / 2];        int k = 0;        for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);            byteArray[i] = (byte) (high << 4 | low);            k += 2;        }        return byteArray;    }    public static void main(String[] args) throws Exception {    }}

原创粉丝点击