Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading