android - Android上最快的加密和解密密码
问题描述
我使用Cipher
Android 上的方法进行了一些测试,以加密文本 ( String
) 并将其解密回原始字符串。
我需要最快的方法(最佳性能)。它似乎Blowfish
是最快的。
我对吗?还是有更快的东西?mb还有其他方法吗?我只需要加密字符串
我还在考虑将 Blowfish 的 C/C++ 实现添加到 Android(NDK、Cmake)。性能应该更好。
测试代码:
companion object {
private const val PLAY_TEST_TEXT = "Some Text Line !"
private val ISO = charset("ISO-8859-1") // is used because we can get our bytes from string back without any problems
private val UTF = charset("UTF-8")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val handler = Handler()
handler.postDelayed({
testFastestCipher("AES/ECB/PKCS5Padding", "AES", false)
testFastestCipher("AES/CBC/PKCS5Padding", "AES", true)
testFastestCipher("AES/CFB/PKCS5Padding", "AES", true)
testFastestCipher("AES/OFB/PKCS5Padding", "AES", true)
testFastestCipher("AES/CTR/PKCS5Padding", "AES", true)
testFastestCipher("Blowfish/ECB/PKCS5Padding", "Blowfish", false)
testFastestCipher("Blowfish/CBC/PKCS5Padding", "Blowfish", true)
testFastestCipher("Blowfish/CFB/PKCS5Padding", "Blowfish", true)
testFastestCipher("Blowfish/OFB/PKCS5Padding", "Blowfish", true)
testFastestCipher("Blowfish/CTR/PKCS5Padding", "Blowfish", true)
testFastestCipher("Blowfish/CTR/PKCS5Padding", "Blowfish", true)
if (Build.VERSION.SDK_INT >= 28) {
testFastestCipher("ChaCha20/None/NoPadding", "ChaCha20", true)
}
}, 2000)
}
private fun testFastestCipher(transformation: String, keyAlgorithm: String, requiresIV: Boolean) {
val cipherEnc = Cipher.getInstance(transformation)
val cipherDec = Cipher.getInstance(transformation)
val secretKeyString = when (keyAlgorithm) {
"ChaCha20" -> "aNdRgUkXp2s5v8y/B?E(G+KbPeShVmYq"
else -> "C&E)H@McQfTjWnZr"
}
val secretKey = SecretKeySpec(secretKeyString.toByteArray(), keyAlgorithm)
if (requiresIV) {
val ivKey = when (keyAlgorithm) {
"Blowfish" -> "12345678"
"ChaCha20" -> "123456789123"
else -> "1234567891230000"
}
val iv = IvParameterSpec(ivKey.toByteArray())
cipherEnc.init(Cipher.ENCRYPT_MODE, secretKey, iv)
cipherDec.init(Cipher.DECRYPT_MODE, secretKey, iv)
} else {
cipherEnc.init(Cipher.ENCRYPT_MODE, secretKey)
cipherDec.init(Cipher.DECRYPT_MODE, secretKey)
}
var encMsg = ""
var decMsg = ""
var timeTook = System.currentTimeMillis()
for (i in 0..5000) {
encMsg = encryptMsg(PLAY_TEST_TEXT, cipherEnc)
decMsg = decryptMsg(encMsg, cipherDec)
}
timeTook = System.currentTimeMillis() - timeTook
textView.text = "${textView.text}\n\n$transformation\nEnc (bytes displayed as ISO-8859-1): $encMsg\nDec: $decMsg\n$timeTook ms"
}
private fun encryptMsg(message: String, cipher: Cipher): String {
val bytes = cipher.doFinal(message.toByteArray(UTF))
return String(bytes, ISO)
}
private fun decryptMsg(cipherText: String, cipher: Cipher): String {
val encryptedString = cipherText.toByteArray(ISO)
return String(cipher.doFinal(encryptedString), UTF)
}
结果:
更新
更多结果(包括外部库ChaCha和内部):
1) Android 28 模拟器 x86
对于我在 0..500000
ChaCha (from third-party library bcprov-jdk15on) 3724 ms
Blowfish/ECB/PKCS5Padding 4216 ms
Blowfish/CBC/PKCS5Padding 4508 ms
Blowfish/OFB/PKCS5Padding 4991 ms
Blowfish/CTR/PKCS5Padding 5158 ms
Blowfish/CFB/PKCS5Padding 5296 ms
ChaCha20/None/NoPadding 5530 ms
AES/OFB/PKCS5Padding 6649 ms
AES/CTR/PKCS5Padding 6703 ms
AES/CFB/PKCS5Padding 7126 ms
AES/ECB/PKCS5Padding 10170 ms
AES/CBC/PKCS5Padding 10619 ms
2)联想安卓21 ARM64
对于我在 0..50000
Blowfish/ECB/PKCS5Padding 3270 ms
Blowfish/CTR/PKCS5Padding 3404 ms
Blowfish/CBC/PKCS5Padding 3483 ms
Blowfish/CFB/PKCS5Padding 3490 ms
Blowfish/OFB/PKCS5Padding 3502 ms
AES/CTR/PKCS5Padding 3577 ms
AES/CFB/PKCS5Padding 3692 ms
AES/OFB/PKCS5Padding 3709 ms
AES/ECB/PKCS5Padding 4739 ms
AES/CBC/PKCS5Padding 5248 ms
ChaCha (from third-party library bcprov-jdk15on) 10195 ms
3) 三星安卓 16 ARMv7
对于我在 0..50000
ChaCha (from third-party library bcprov-jdk15on) 3969 ms
Blowfish/ECB/PKCS5Padding 5282 ms
Blowfish/CTR/PKCS5Padding 5569 ms
AES/CTR/PKCS5Padding 6160 ms
Blowfish/CBC/PKCS5Padding 6201 ms
AES/CFB/PKCS5Padding 6217 ms
AES/CBC/PKCS5Padding 6320 ms
Blowfish/OFB/PKCS5Padding 6382 ms
Blowfish/CFB/PKCS5Padding 6808 ms
AES/OFB/PKCS5Padding 7224 ms
AES/ECB/PKCS5Padding 7247 ms
ChaCha(来自第三方库 bcprov-jdk15on)在不同的设备上效果最好或最差......
解决方案
我之前一直在为其他平台寻找快速密码,您可能会看到以下站点https://rweather.github.io/arduinolibs/crypto.html
我发现 ChaCha 密码很快并且仍然很常见。AES 的速度可能取决于平台版本是否支持硬件加速https://android.stackexchange.com/questions/186664/do-android-phones-have-hardware-chips-for-encryption-if-its-software- only-the(Blowfish 尚未破解,但其作者已经表示该密码应被视为已过时)
作为改进 - 密码模式(ctr,ofb)不需要填充,您可以在那里使用“NoPadding”值
推荐阅读
- javascript - 如何在javascript中的(多项选择)答案之间插入一个中断?
- c++ - 如何使用 OpenCV C++ 加载 Yolo 对象检测网络
- postgresql - 在 postgresql 中选择查询变得非常非常慢
- bash - Bash / Git - 如何使用当前 git 分支名称制作脚本
- android - 为什么在 layout_height="wrap_content" 中忽略卡片高度?
- ios - Swift 4 - 使用 UIPickerView 自定义 UICollectionReusableView
- excel - 无法踢出字符串周围和字符串之间的空格
- elasticsearch - logstash 剖析问题
- firebase - Firebase 存储允许将文件夹写入多个用户
- angular - AngularfireModule 类不是角度模块