react-native-aes icon indicating copy to clipboard operation
react-native-aes copied to clipboard

Decryption always fails.

Open exotexot opened this issue 5 years ago • 10 comments

Im using the example from the README. Encryption works without issues.

Decryption always fails.

Im using RNFS to read a File, then passing the base64 encoded contents to the decrypt function. Console information arent really revealing (to me)

Screenshot 2020-03-12 at 15 39 50

exotexot avatar Mar 12 '20 14:03 exotexot

Same for me.

gitmazzio avatar Mar 18 '20 08:03 gitmazzio

This is how it was used by me: https://github.com/tectiv3/amanothi/blob/master/src/Storage.js#L229 and this is how it is being used by Standard Notes: https://github.com/standardnotes/mobile/blob/master/src/lib/sfjs/sfjs.js

tectiv3 avatar Mar 19 '20 05:03 tectiv3

Same for me. When I log the error I have this:

03-23 12:44:09.708 4645 4801 I ReactNativeJS:[Error: bad base-64] 03-23 12:44:09.711 4645 4801 I ReactNativeJS: [Error: bad base-64]

and sometimes :

[Error: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT]

MarvynORourke avatar Mar 23 '20 12:03 MarvynORourke

Don't forget to save iv used for encrypting, you'll need it for decryption. Also you should check HMAC to make sure data isn't corrupted. Look for examples by following the links I posted above.

tectiv3 avatar Mar 23 '20 12:03 tectiv3

I solved my problem by following the examples provided by tectiv3. Thanks!

exotexot avatar Apr 02 '20 10:04 exotexot

Hello,

are there some constraints regarding the keys and iv?

For example, I can use keys and ivs like these let Akey = "cece3a7dc9cf86aae926fd2ee520a06e5a5b616fa9e381de53600121e8aff095" let Aiv = "4e58cafb7b6ccbbef797c2761b233a58"

But If I change a the key to test, encryption works like normal. But the decryption fails with those parameters. let Akey = "test" let Aiv = "4e58cafb7b6ccbbef797c2761b233a58"

I'm using to encrypt const encrypted = await NativeModules.Aes.encrypt(data, Akey, Aiv)

I'm using to decrypt: const decrypted = await NativeModules.Aes.decrypt(data, Akey, Aiv)

But it still throws me the error that I posted initially.

exotexot avatar Apr 07 '20 11:04 exotexot

Yes,

If I create a key like so: await NativeModules.Aes.pbkdf2(ref, startTime.toString(), 1000, 256)

I need to make sure that the last value, is at least 256. Otherwise the decryption process fails for me.

exotexot avatar Apr 07 '20 11:04 exotexot

yes, you need to derive a key from your password phrase

tectiv3 avatar Apr 07 '20 11:04 tectiv3

Yes,

If I create a key like so: await NativeModules.Aes.pbkdf2(ref, startTime.toString(), 1000, 256)

I need to make sure that the last value, is at least 256. Otherwise the decryption process fails for me.

I agree with the > 256 length idea. I copied code directly from readme.md and it consistently fails if length was set to 128. When set to 256, it works. Please someone look into this.

vilmes21 avatar Jul 13 '20 18:07 vilmes21

yes, you need to derive a key from your password phrase

I don't think this is the point the other user was hinting at. The issue was with the length argument. When set to 256, things work fine. When set to, for example, 128, decrytion fails (encryption always works). You can even try with the readme.md sample code. Behaves very consistenly.

vilmes21 avatar Jul 13 '20 18:07 vilmes21