暗号化
fun String.encrypt(): String {
//それぞれ16文字にする
val secretKey = "abcdefghijklmnop"
val initializationKey = "ABCDEFGHIJKLMNOP"
val key = SecretKeySpec(secretKey.toByteArray(), "AES")
val iv = IvParameterSpec(initializationKey.toByteArray())
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
cipher.init(Cipher.ENCRYPT_MODE, key, iv)
val byteResult = cipher.doFinal(this.toByteArray())
return Base64.encodeToString(byteResult, Base64.NO_WRAP)
}
復号化
fun String.decrypt(): String {
//それぞれ16文字にする
val secretKey = "abcdefghijklmnop"
val initializationKey = "ABCDEFGHIJKLMNOP"
val key = SecretKeySpec(secretKey.toByteArray(), "AES")
val iv = IvParameterSpec(initializationKey.toByteArray())
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
cipher.init(Cipher.DECRYPT_MODE, key, iv)
val byteResult = cipher.doFinal(Base64.decode(this, Base64.NO_WRAP))
return String(byteResult)
}