文章目录
-
- [1. key填充](#1. key填充)
- [2. 加密](#2. 加密)
- [3. 解密](#3. 解密)
1. key填充
aes算法对key的字节数有要求 所以对输入的key要做填充处理
kotlin
fun fillKey(key: String): ByteArray {
val random = SecureRandom.getInstance("SHA1PRNG")
random.setSeed(key.toByteArray())
val generator = KeyGenerator.getInstance("AES")
generator.init(random)
return generator.generateKey().encoded
}
2. 加密
kotlin
fun ktAes(key: String, source: String): String {
val keyBytes = fillKey(key)
return Aes(keyBytes).encryptBase64(source)
}
3. 解密
kotlin
fun ktDeAes(key: String, source: String): String {
val keyBytes = fillKey(key)
return Aes(keyBytes).decryptStr(source)
}