我在Android上使用Cipher
方法进行了一些测试,以加密文本(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
对于i。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)联想Android 21 ARM64
对于i in 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)三星Android 16 ARMv7
对于i in 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)在不同设备上的效果最佳或最差。
答案 0 :(得分:1)
我之前一直在寻找用于其他平台的快速密码,您可能会看到以下站点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”值