Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest cipher on Android for encryption and decryption

I did some tests with Cipher methods on Android to encrypt text (String) and decrypt it back to original string.

I need the fastest method (best performance). And it seems Blowfish is the fastest.

Am I right? Or is there something faster? Mb some other methods? I just need to encrypt strings

I'm also thinking about adding C/C++ implementation of Blowfish to Android (NDK, Cmake). Performance should be better.

Test code:

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)
}

Results:

enter image description here

Update

More results (including external library ChaCha and internal):

1) Android 28 Emulator x86

for i in 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) Lenovo Android 21 ARM64

for 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) Samsung Android 16 ARMv7

for 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 (from third-party library bcprov-jdk15on) works the best or the worst on different devices...

like image 306
user924 Avatar asked Sep 15 '26 19:09

user924


1 Answers

I was looking for a fast cipher before for other platforms, you may see following site https://rweather.github.io/arduinolibs/crypto.html

I found the ChaCha cipher fast and still commonly available. Speed of AES may depend on the platform version whether hardware acceleration is supported https://android.stackexchange.com/questions/186664/do-android-phones-have-hardware-chips-for-encryption-if-its-software-only-the (Blowfish is not yet broken, but its author has already expressed the cipher should be considered obsolete)

as improvement - cipher modes (ctr, ofb) need no padding, you may use 'NoPadding' value there

like image 185
gusto2 Avatar answered Sep 18 '26 09:09

gusto2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!