与Android上的加密相比,解密要慢得多

我在Kotlin使用“CBC”模式和“PKCS5Padding”填充实现了“AES”加密和解密。 我注意到,虽然解密cipherInputStream.read(buffer)只读取512个字节,而不是8192字节的完整缓冲区大小。 这是为什么? 加密时使用整个缓冲区。

这些是我正在使用的常量,

 private val TRANSFORMATION = "AES/CBC/PKCS5Padding" private var SECRET_KEY_FAC_ALGORITHM = "PBKDF2WithHmacSHA1" private val SECRET_KEY_SPEC_ALGORITHM = "AES" private val cipher = Cipher.getInstance(TRANSFORMATION) private val random = SecureRandom() private val KEY_BITS_LENGTH = 256 private val IV_BYTES_LENGTH = cipher.blockSize private val SALT_BYTES_LENGTH = KEY_BITS_LENGTH / 8 private val ITERATIONS = 10000 

解密代码

 cis = CipherInputStream(input, cipher) val buffer = ByteArray(8192) var read = cis.read(buffer) while (read > -1) { fos.write(buffer, 0, read) read = cis.read(buffer) } 

加密代码

 fos.write(iv) fos.write(salt) cos = CipherOutputStream(fos, cipher) val buffer = ByteArray(8192) var read = input.read(buffer) while (read > -1) { cos.write(buffer, 0, read) read = input.read(buffer) } 

Kotlin language will be the best programming language for Android.