terl / lazysodium-android

An Android implementation of the Libsodium cryptography library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
108 stars 25 forks source link

Decryption not working on real device, but on emulator it works fine #53

Open CosminCirlea opened 3 years ago

CosminCirlea commented 3 years ago

Hello! I'm trying to decrypt data and it's working on any emulator, but real devices do not work (tested on API 24 and 30). Any clues? Below is my code snippet

` public String decryptData(String transportKey, String cipher){ lazySodium = new LazySodiumAndroid(new SodiumAndroid()); byte[] cipherBytes = Base64.decode(cipher, Base64.DEFAULT); byte[] transportKeyBytes = Base64.decode(transportKey, Base64.DEFAULT); byte[] nonceBytes = Arrays.copyOfRange(cipherBytes, 0, AEAD.AES256GCM_NPUBBYTES); byte[] cipherTextBytes = Arrays.copyOfRange(cipherBytes, AEAD.AES256GCM_NPUBBYTES, cipherBytes.length);

    long[] outMessageLength = new long[1];
    byte[] outMessage = new byte[1000];

    lazySodium.cryptoAeadAES256GCMDecrypt(outMessage, outMessageLength, null, cipherTextBytes, cipherTextBytes.length, null, 0, nonceBytes, transportKeyBytes);
    outMessage = Arrays.copyOfRange(outMessage, 0, (int)outMessageLength[0]);
    return new String(outMessage, StandardCharsets.UTF_8);
}`
gurpreet- commented 2 years ago

Hi @CosminCirlea,

Thank you for reporting this issue.

I have tests setup so that they can run on emulators or devices. Could you checkout this repo and run the tests in app/androidTest on your devices? It should be a case of right-clicking on the folder androidTest and then clicking Run all tests which should launch on your connected device via adb.

If any of them fail then the problem is something in this library. If they all run successfully then the problem is in your code somewhere.