在 Kotlin 中使用 ByteArrays 和 SecretKeySpec

Working with ByteArrays and SecretKeySpec in Kotlin

我有一个看似简单的任务..

拿一个文件,打开它,将字节流作为AES密钥,在Android Kotlin

中实例化一个javax.crypto.spec.SecretKeySpec
if (key == null) {

  val my_bytes: ByteArray = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 
  val algo = "AES"

  val secretKey = SecretKeySpec(my_bytes, algo)

  saveSecretKey(sharedPref, secretKey!!)
  return secretKey
}

编辑:SecretKeySpec() 现在有效。我只需要知道如何将文件中的字节正确地放入 android 应用程序。应用程序中的硬编码不安全吗?我应该将密钥存储为文件并从 android 文件系统中读取它吗?

如果您将密钥作为文件存储在外部驱动器上,将会发生以下情况:

  1. 您需要获得用户的许可才能read/write到外部驱动器
  2. 因为密钥在外部驱动器上,所以容易受到: 2.1 被用户删除 2.2 被 app/person 阅读的人不是你想要的

由于秘钥是对称的,既可用于加密也可用于解密

现在,对于我们的鸟:

从磁盘加载文件:This explains it quite well