AES有五种加密模式分别是CBC、ECB、CTR、OCF、CFB;
1.如果不选择填充会默认采用 ECB 模式和 PKCS5Padding 填充进行处理
AES 是块加密,块的长度是16个字节,如果原文不到16个字节,则需要填充至16个字节后再进行处理。
AES密文长度=(原文长度/16)*16+16;最终长度为N+1;所以一定要选择NoPadding模式。
2. 随机数生成器
在Android加密算法中需要随机数时要使用SecureRandom来获取随机数。
注意不要给SecureRandom设置种子。调用seeded constructor或者setSeed(byte[])是不安全的。 SecureRandom()默认使用的是dev/urandom作为种子产生器,这个种子是不可预测的。
开发者建议:
1、不要使用Random类来获取随机数。
2、在使用SecureRandom时候,不要设置种子
SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[16]; //随机秘钥
secureRandom.nextBytes(key)
/**
* 生成随机key
*
* @return key
* @throws Exception generate error
*/
public static byte[] getRawKey() throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
/**
* AES 加密原文16字节对齐,不足补零
*
* @param raw 加密秘钥
* @param source 被加密数据
* @return 密文
* @throws Exception
*/
public static byte[] encrypt(byte[] raw, byte[] source) throws Exception {
byte[] original;
if (raw.length != 16)
throw new IllegalArgumentException("key is not 16 bytes");
if (source.length % 16 != 0) {
original = new byte[(source.length / 16 + source.length % 16 == 0 ? 0 : 1) * 16];
System.arraycopy(source, 0, original, 0, source.length);
} else {
original = Arrays.copyOf(source, source.length);
}
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(original);
return encrypted;
}
/**
* AES机密密文,AES/ECB/NoPadding参数要和加密时一样,NoPadding原文密文长度一致。
*
* @param raw 秘钥
* @param encrypted 密文
* @return 原文
* @throws Exception
*/
public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
if (encrypted.length % 16 != 0)
return encrypted;
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
/**
* RC4 加密原文16字节对齐,不足补零
*
* @param raw 秘钥
* @param source 原文
* @return 密文
* @throws Exception
*/
public static byte[] rc4(byte[] raw, byte[] source) throws Exception {
byte[] original;
if (raw.length != 16)
throw new IllegalArgumentException("key is not 16 bytes");
if (source.length % 16 != 0) {
original = new byte[(source.length / 16 + source.length % 16 == 0 ? 0 : 1) * 16];
System.arraycopy(source, 0, original, 0, source.length);
} else {
original = Arrays.copyOf(source, source.length);
}
SecretKeySpec skeySpec = new SecretKeySpec(raw, "RC4");
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(original);
return encrypted;
}
/**
* 从大到小排序
*
* @param source
*/
public static void sortByDescending(int source[]) {
int swap = 0;
for (int position = 0; position < source.length; position++) {
for (int location = 0; location < source.length; location++) {
if (source[position] > source[location]) {
swap = source[position];
source[position] = source[location];
source[location] = swap;
}
}
}
}
/**
* 生成随机的length长的String。
*
* @param length 生成的字符长度。
* @return 生成的随机字符。
*/
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
转载于:https://blog.51cto.com/kz1080/1887741