Android(安卓)AES加密解密
16lz
2022-01-11
补齐方式采用的是:不足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;}}
更多相关文章
- 解决overlaps the location of another project的办法
- Android(安卓)组件 ContentProvider 内容提供者
- 【Android】文件读写操作(含SDCard的读写)
- Android开发之SlidingDrawer(一)
- Android(安卓)对话框【Dialog】去除白色边框代码
- Android.InstallDevelopmentEvn
- 待验证内容。
- Android(安卓)TextView内容居中和控件居中
- Android定义字符串数组资源并在程序中使用