Home > front end >  AES/GCM encryption on Android (Java), 'or' Kotlin statement equivalent in Java
AES/GCM encryption on Android (Java), 'or' Kotlin statement equivalent in Java

Time:10-14

I'm trying to implement encryption in my app. I found some Kotlin code on GitHub and tutorials on how to implement it, until I found this block:

val kgps = KeyGenParameterSpec.Builder("my_aes_key", KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
        // This is required to be able to provide the IV ourselves
        .setRandomizedEncryptionRequired(false)
        .build()

I'm still using Java for the Android app, I'm still wondering how can I implement this line in Java (or what is the equivalent statement for it)?

KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT

I'm facing a compile error if I try to replace the or with || binary operator, since that operator isn't compatible with for comparing integers. The error seems missing when I use the bitwise | operator but I can't be sure, is it the correct implementation of it.

CodePudding user response:

Yes, or in Kotlin is the same as | in Java.

According to the docs, KeyProperties.PURPOSE_ENCRYPT and KeyProperties.PURPOSE_DECRYPT are both integers. That means the or function being used is this one, which performs a bitwise or. You're correct that that's equivalent to the Java | operator. Kotlin doesn't have bitwise operators in the language, so it uses the infix extension functions and and or instead.

  • Related