} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
-
AES CTR 解密
-
填充为PKCS5
-
@param encode 加密过后的文件
-
@param iv 偏移量
-
@param key 秘钥文件
*/
public static String decryptCRT(String encode, String iv, String key) {
String decoded = “”;
try {
byte[] bytes = Hex.decode(encode);
IvParameterSpec ivSpec = new IvParameterSpec(
iv.getBytes());
Key keys = new SecretKeySpec(key.getBytes(), “AES”);
Cipher cipher = Cipher.getInstance(“AES/CTR/PKCS5Padding”);
cipher.init(Cipher.DECRYPT_MODE, keys, ivSpec);
byte[] ret = cipher.doFinal(bytes);
decoded = new String(ret);
return decoded.trim();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return decoded;
}
/**
-
AES ECB加密
-
填充为 “\0” noPadding
-
@param content 要加密的内容
-
@param key 加密文件的秘钥
-
@return 输出Base64的结果
**/
public static String encryptECB(byte[] content, String key) {
String encodeStr = “”;
try {
byte[] keyBytes = key.getBytes();
SecretKeySpec se
cretKeySpec = new SecretKeySpec(keyBytes, “AES”);
Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
//加密数据
int length = content.length;
int add = 16 - length % 16;
if (add == 16) {
add = 0;
}
byte[] contentNew = new byte[length + add];
//用’\0’加密
for (int i = 0; i < length + add; i++) {
if (i < length) {
contentNew[i] = content[i];
} else {
contentNew[i] = ‘\0’ ;
}
}
byte[] resultBytes = cipher.doFinal(contentNew);
//结果用Base64
encodeStr = new String(Base64Util.encode(resultBytes));
return encodeStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
-
AES ECB解密
-
@param encode 加密过后的文件
-
@param key 秘钥文件
*/
public static String decryptECB(String encode, String key) {
String decoded = “”;
byte[] decodeBase64 = Base64.decode(encode);
int length = decodeBase64.length;
int add = 16 - length % 16;
if (add == 16) {
add = 0;
}
byte[] decodeContentNew = new byte[length + add];
//填充’\0’
for (int i = 0; i < length + add; i++) {
if (i < length) {
decodeContentNew[i] = decodeBase64[i];
} else {
decodeContentNew[i] = ‘\0’ ;
}
}
try {
Key keys = new SecretKeySpec(key.getBytes(), “AES”);
Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);
cipher.init(Cipher.DECRYPT_MODE, keys);
byte[] ret = cipher.doFinal(decodeContentNew);
decoded = new String(ret);
return decoded.trim();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return decoded;
}