AES-CBC-PKCS5Padding加密算法Java实现

来源:互联网 发布:途虎 淘宝 编辑:程序博客网 时间:2024/05/22 00:17
public class AesEncryptionUtil {    private static final String TAG = "AesEncryptionUtil";    // /** 创建密钥 **/    private static SecretKeySpec createKey(String key) {        byte[] data = null;        if (key == null) {            key = "";        }        StringBuffer sb = new StringBuffer(16);        sb.append(key);        while (sb.length() < 16) {            sb.append("0");        }        if (sb.length() > 16) {            sb.setLength(16);        }        try {            data = sb.toString().getBytes("UTF-8");            Log.i(TAG, data.length + "");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return new SecretKeySpec(data, "AES");    }    private static IvParameterSpec createIV(String password) {        byte[] data = null;        if (password == null) {            password = "";        }        StringBuffer sb = new StringBuffer(16);        sb.append(password);        while (sb.length() < 16) {            sb.append("0");        }        if (sb.length() > 16) {            sb.setLength(16);        }        try {            data = sb.toString().getBytes("UTF-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return new IvParameterSpec(data);    }    // /** 加密字节数据 **/    public static byte[] encrypt(byte[] content, String password, String iv) {        String CipherMode = "AES/CBC/PKCS5Padding";        try {            SecretKeySpec key = createKey(password);            Cipher cipher = Cipher.getInstance(CipherMode);            cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv));            byte[] result = cipher.doFinal(content);            return result;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    // /** 解密字节数组 **/    public static byte[] decrypt(byte[] content, String password, String iv) {        String CipherMode = "AES/CBC/PKCS5Padding";        try {            SecretKeySpec key = createKey(password);            Cipher cipher = Cipher.getInstance(CipherMode);            cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv));            byte[] result = cipher.doFinal(content);            return result;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static String encrypt(String sSrc, String aesKey, String aesIV) {        String CipherMode = "AES/CBC/NoPadding";        byte[] raw = aesKey.getBytes();        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");        Cipher cipher = null;        try {            cipher = Cipher.getInstance(CipherMode);//"算法/模式/补码方式"            IvParameterSpec iv = new IvParameterSpec(aesIV.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);        } catch (Exception e) {            e.printStackTrace();            Log.i("AESEncrypt", "AESEncrypt.encrypt()  Exception");        }        byte[] srawt = sSrc.getBytes();        int len = srawt.length;        /* 计算补0后的长度 */        while (len % 16 != 0) len++;        byte[] sraw = new byte[len];        /* 在最后补0 */        for (int i = 0; i < len; ++i) {            if (i < srawt.length) {                sraw[i] = srawt[i];            } else {                sraw[i] = 0;            }        }        byte[] encrypted = null;        try {            encrypted = cipher.doFinal(sraw);        } catch (Exception e) {            e.printStackTrace();            Log.i("AESEncrypt", "AESEncrypt.encrypt()  Exception");        }        String data = new String(Base64Encoder.encode(encrypted));        return data;    }    public static String decrypt(String sSrc, String aesKey, String aesIV) {        String CipherMode = "AES/CBC/NoPadding";        try {            byte[] raw = aesKey.getBytes("ASCII");            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = Cipher.getInstance(CipherMode);            IvParameterSpec iv = new IvParameterSpec(aesIV.getBytes());            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);            byte[] encrypted1 = Base64Encoder.decode(sSrc);//先用base64解密            try {                byte[] original = cipher.doFinal(encrypted1);                String originalString = new String(original);                return originalString.trim();            } catch (Exception e) {                System.out.println(e.toString());                return null;            }        } catch (Exception ex) {            System.out.println(ex.toString());            return null;        }    }}