AESUtil加密: Base64,AES嵌套双层加密
crazy code (kotlin)
/**
* @ClassName AESUtil
* @Description TODO
* @Author Kolin Zhao/ Mozhimen
* @Date 2021/10/14 15:13
* @Version 1.0
*/
object AESUtil {
//加密算法
private const val KEY_ALGORITHM = "AES"
//AES 的 密钥长度,32 字节,范围:16 - 32 字节
private const val SECURE_KEY_LENGTH = 16
//秘钥长度不足 16 个字节时,默认填充位数
private const val DEFAULT_VALUE = "0"
//字符编码
@RequiresApi(Build.VERSION_CODES.KITKAT)
private val CHARSET_UTF8 = StandardCharsets.UTF_8
//加解密算法/工作模式/填充方式
private const val CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"
//key
private const val SECURE_KEY = "xxxxxForTodo_Key"
//IV
private const val IV_STRING = "xxxxxForTodo_Iv"
/**
* 采用AES128加密
*
* @param content 要加密的内容
* @return
*/
@RequiresApi(Build.VERSION_CODES.KITKAT)
fun encrypt(content: String): String? {
try {
// 获得密匙数据
val rawKeyData = getAESKey(SECURE_KEY)
// 从原始密匙数据创建KeySpec对象
val key = SecretKeySpec(rawKeyData, KEY_ALGORITHM)
// Cipher对象实际完成加密操作
val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
// 用密匙初始化Cipher对象
val ivParameterSpec = IvParameterSpec(IV_STRING.toByteArray())
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec)
// 正式执行加密操作
val encryptByte = cipher.doFinal(content.toByteArray())
return Base64.encodeToString(encryptByte, Base64.NO_WRAP)
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
} catch (e: IllegalBlockSizeException) {
e.printStackTrace()
} catch (e: BadPaddingException) {
e.printStackTrace()
}
return null
}
/**
* 采用AES128解密
*
* @param content
* @param secureKey
* @return
* @throws Exception
* ,Exception
* @throws Exception
*/
@RequiresApi(Build.VERSION_CODES.KITKAT)
fun decrypt(content: String, secureKey: String): String? {
val data: ByteArray = Base64.decode(content, Base64.NO_WRAP)
try {
// 获得密匙数据
val rawKeyData = getAESKey(secureKey) // secureKey.getBytes();
// 从原始密匙数据创建一个KeySpec对象
val key = SecretKeySpec(rawKeyData, KEY_ALGORITHM)
// Cipher对象实际完成解密操作
val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
// 用密匙初始化Cipher对象
val initParam = IV_STRING.toByteArray()
val ivParameterSpec = IvParameterSpec(initParam)
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec)
return String(cipher.doFinal(data), CHARSET_UTF8)
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: IllegalBlockSizeException) {
e.printStackTrace()
} catch (e: BadPaddingException) {
e.printStackTrace()
}
return null
}
@RequiresApi(Build.VERSION_CODES.KITKAT)
@Throws(UnsupportedEncodingException::class)
fun getAESKey(key: String): ByteArray {
val keyBytes: ByteArray = key.toByteArray(CHARSET_UTF8)
val keyBytes16 = ByteArray(SECURE_KEY_LENGTH)
System.arraycopy(
keyBytes, 0, keyBytes16, 0,
keyBytes.size.coerceAtMost(SECURE_KEY_LENGTH)
)
return keyBytes16
}
}