Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cryptoAeadAES256GCM is not available #59

Open
AndroidCrypto opened this issue Jan 4, 2023 · 0 comments
Open

cryptoAeadAES256GCM is not available #59

AndroidCrypto opened this issue Jan 4, 2023 · 0 comments

Comments

@AndroidCrypto
Copy link

AndroidCrypto commented Jan 4, 2023

I'm trying to implement a string encryption using AES-256 in GCM mode with Lazysodium but the code fails and
ls.cryptoAeadAES256GCMIsAvailable() return false.

Running the (nearly) same code with ChaCha20Poly1305 gives the expected cipher- and decrypted-texts.

My dev-machine is a Mac with MacOs Ventura 13.1 and M1-chip.

I'm using these dependencies and they got loaded without any Gradle- or build error:

implementation 'com.goterl:lazysodium-android:5.1.0@aar'
implementation 'net.java.dev.jna:jna:5.12.1@aar'

I setup a simple Android app (link to the app: https://github.com/AndroidCrypto/LazysodiumSymmetricEncryption) that shows the complete workflow, here are the essential parts:

        // note: outputKey is a 32 bytes long byte array from a key derivation
        // generate a random nonce
        byte[] nonceByte = ls.nonce(AEAD.AES256GCM_NPUBBYTES);
        Log.i(TAG, "generate a random nonce for encryption: " + ls.sodiumBin2Hex(nonceByte));
        // generate a key from outputKey
        Key key = Key.fromBytes(outputKey);
        Log.i(TAG, "generate a key from outputKey");
        // encrypt
        Log.i(TAG, "encrypt the plaintext using AES-256 GCM algorithm");
        String ciphertext = ls.encrypt(plaintext, null, nonceByte, key, AEAD.Method.AES256GCM);
        // ciphertext is in hex encoding
        Log.i(TAG, "ciphertext: " + ciphertext);
        // decrypt
        Log.i(TAG, "decrypt the ciphertext using the same algorithm, key and nonce");
        String decryptedtext = "";
        if (ls.cryptoAeadAES256GCMIsAvailable()) {
            Log.i(TAG, "AEAD AES-256 GCM is available");
            try {
                decryptedtext = ls.decrypt(ciphertext, null, nonceByte, key, AEAD.Method.AES256GCM);
            } catch (AEADBadTagException e) {
                e.printStackTrace();
                //Log.e(TAG, e.getMessage());
            }
            Log.i(TAG, "decryptedtext: " + decryptedtext);
            Log.i(TAG, "plaintext equals to decrptedtext: " + decryptedtext.equals(plaintext));
        } else {
            Log.e(TAG, "AEAD AES-256 GCM is not available");
        }

Greetings and Happy new year to everyone
Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant