Android AES加密解密

補齊方式采用的是:不足16字節,補齊內容為差值(比如數據是10個字節,補齊的內容就是6)。

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

/** 
 * AES加密解密工具 
 *  
 * @author yangle 
 */  
public class AESUtils {  

    /** 
     * AES加密 
     *  
     * @param data 
     *            將要加密的內容 
     * @param key 
     *            密鑰 
     * @return 已經加密的內容 
     */  
    public static byte[] encrypt(byte[] data, byte[] key) {  
        //不足16字節,補齊內容為差值  
        int len = 16 - data.length % 16;  
        for (int i = 0; i < len; i++) {  
            byte[] bytes = { (byte) len };  
            data = ArrayUtils.concat(data, bytes);  
        }  
        try {  
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");  
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");  
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);  
            return cipher.doFinal(data);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return new byte[] {};  
    }  

    /** 
     * AES解密 
     *  
     * @param data 
     *            將要解密的內容 
     * @param key 
     *            密鑰 
     * @return 已經解密的內容 
     */  
    public static byte[] decrypt(byte[] data, byte[] key) {  
        data = ArrayUtils.noPadding(data, -1);  
        try {  
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");  
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");  
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);  
            byte[] decryptData = cipher.doFinal(data);  
            int len = 2 + ByteUtils.byteToInt(decryptData[4]) + 3;  
            return ArrayUtils.noPadding(decryptData, len);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return new byte[] {};  
    }  
}

合并數組與去除補齊工具

/** 
 * 數組工具 
 *  
 * @author yangle 
 */  
public class ArrayUtils {  

    /** 
     * 合并數組 
     *  
     * @param firstArray 
     *            第一個數組 
     * @param secondArray 
     *            第二個數組 
     * @return 合并后的數組 
     */  
    public static byte[] concat(byte[] firstArray, byte[] secondArray) {  
        if (firstArray == null || secondArray == null) {  
            return null;  
        }  
        byte[] bytes = new byte[firstArray.length + secondArray.length];  
        System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);  
        System.arraycopy(secondArray, 0, bytes, firstArray.length,  
                secondArray.length);  
        return bytes;  
    }  

    /** 
     * 去除數組中的補齊 
     *  
     * @param paddingBytes 
     *            源數組 
     * @param dataLength 
     *            去除補齊后的數據長度 
     * @return 去除補齊后的數組 
     */  
    public static byte[] noPadding(byte[] paddingBytes, int dataLength) {  
        if (paddingBytes == null) {  
            return null;  
        }  

        byte[] noPaddingBytes = null;  
        if (dataLength > 0) {  
            if (paddingBytes.length > dataLength) {  
                noPaddingBytes = new byte[dataLength];  
                System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, dataLength);  
            } else {  
                noPaddingBytes = paddingBytes;  
            }  
        } else {  
            int index = paddingIndex(paddingBytes);  
            if (index > 0) {  
                noPaddingBytes = new byte[index];  
                System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, index);  
            }  
        }  

        return noPaddingBytes;  
    }  

    /** 
     * 獲取補齊的位置 
     *  
     * @param paddingBytes 
     *            源數組 
     * @return 補齊的位置 
     */  
    private static int paddingIndex(byte[] paddingBytes) {  
        for (int i = paddingBytes.length - 1; i >= 0; i--) {  
            if (paddingBytes[i] != 0) {  
                return i + 1;  
            }  
        }  
        return -1;  
    }  
}

 

來自:http://www.jianshu.com/p/dd2ae6166532

 

 本文由用戶 fv6688 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!