首页 > 解决方案 > Android上最快的加密和解密密码

问题描述

我使用CipherAndroid 上的方法进行了一些测试,以加密文本 ( 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)在不同的设备上效果最好或最差......

标签: androidencryptionaesblowfish

解决方案


我之前一直在为其他平台寻找快速密码,您可能会看到以下站点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”值


推荐阅读