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

Error generating encryption key: [TypeError: Cannot read property 'pbkdf2' of null]

Open shubh-neorox opened this issue 1 year ago • 7 comments

in IOS I am able to genrate key by using this way import Aes from "react-native-aes-crypto"; const generateKey = async (password, salt, cost, length) => Aes.pbkdf2(password, salt, cost, length, "sha256");

but in android I am unable to get it 
getting this error :"Error generating encryption key: [TypeError: Cannot read property 'pbkdf2' of null]"

react native version :0.74

shubh-neorox avatar Aug 13 '24 12:08 shubh-neorox

in IOS I am able to genrate key by using this way import Aes from "react-native-aes-crypto"; const generateKey = async (password, salt, cost, length) => Aes.pbkdf2(password, salt, cost, length, "sha256");

but in android I am unable to get it 
getting this error :"Error generating encryption key: [TypeError: Cannot read property 'pbkdf2' of null]"

react native version :0.74

Did you add include ':react-native-aes-crypto' project(':react-native-aes-crypto').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-aes-crypto/android') in android/settings.gradle ?

And dependencies { implementation project(':react-native-aes-crypto') } in android/app/build.gradle ?

Jonnynsk avatar Sep 24 '24 04:09 Jonnynsk

implementation project(':react-native-aes-crypto')

Yes i did already still the same issue

shubh-neorox avatar Dec 06 '24 08:12 shubh-neorox

Are you still having issues after upgrading to 3.1?

https://github.com/tectiv3/react-native-aes-demo

tectiv3 avatar Dec 06 '24 12:12 tectiv3

Yes , So made One kind of patch file to deal with it .

On Fri, 6 Dec 2024 at 18:13, tectiv3 @.***> wrote:

Are you still having issues after upgrading to 3.1?

— Reply to this email directly, view it on GitHub https://github.com/tectiv3/react-native-aes/issues/89#issuecomment-2523138253, or unsubscribe https://github.com/notifications/unsubscribe-auth/BIJ5LH73CK3BVBDSMHFTMF32EGLX7AVCNFSM6AAAAABMOEH7YCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMRTGEZTQMRVGM . You are receiving this because you authored the thread.Message ID: @.***>

shubh-neorox avatar Dec 06 '24 12:12 shubh-neorox

I came across this issue searching for a solution for a similar error. It appears 'Aes' is null.

Simple example: import Aes from 'react-native-aes-crypto';

try { console.log('init'); console.log(Aes); const test1 = Aes.randomUuid(); console.log(test1); } catch (error) { console.log(error); }

LOG init LOG null LOG [TypeError: null is not an object (evaluating '_reactNativeAesCrypto.default.randomUuid')]

I am on react-native 0.76 and was able to test the DEMO without any issues. However, I noticed that I have the new architecture disabled (since a couple of the modules I am using doesn't fully support it yet). If you add this line to POD file in your DEMO (disabling the new architecture), you can reproduce this issue and the DEMO also fails.

ENV['RCT_NEW_ARCH_ENABLED'] = '0'

jplandry908 avatar Dec 08 '24 19:12 jplandry908

@tectiv3 - FYI

This may not be the best solution, but I found a fix/workaround to my issue after reading through a bunch of react-native new architecture blogs and others complaining about Native Modules being null.

Below is a patch outlining the change I made. I basically had to call out each function in the 'index.js' file, and also change it from 'RTCAes' to just 'Aes.'. However, this breaks Android, so I added a Platform.OS check as well.

Note: This is the comment that had my try this approach ... https://github.com/facebook/react-native/issues/29228#issuecomment-2345062090

diff --git a/node_modules/react-native-aes-crypto/index.js b/node_modules/react-native-aes-crypto/index.js
index cb179dc..8f22f4d 100755
--- a/node_modules/react-native-aes-crypto/index.js
+++ b/node_modules/react-native-aes-crypto/index.js
@@ -1,4 +1,22 @@
 'use strict'
-import { NativeModules } from 'react-native'
+import { NativeModules, Platform } from 'react-native'
 
-export default NativeModules.RCTAes
+const AES =
+  Platform.OS === 'ios'
+    ? {
+        randomKey: NativeModules.Aes.randomKey,
+        pbkdf2: NativeModules.Aes.pbkdf2,
+        pbkdf2Sync: NativeModules.Aes.pbkdf2Sync,
+        encrypt: NativeModules.Aes.encrypt,
+        decrypt: NativeModules.Aes.decrypt,
+        hmac256: NativeModules.Aes.hmac256,
+        hmac512: NativeModules.Aes.hmac512,
+        randomKey: NativeModules.Aes.randomKey,
+        randomUuid: NativeModules.Aes.randomUuid,
+        sha1: NativeModules.Aes.sha1,
+        sha256: NativeModules.Aes.sha256,
+        sha512: NativeModules.Aes.sha512,
+      }
+    : NativeModules.RCTAes;
+
+export default AES
\ No newline at end of file

jplandry908 avatar Dec 09 '24 18:12 jplandry908

I've renamed the package again, making the naming more consistent. Replaced all RCTAes with Aes.

https://github.com/tectiv3/react-native-aes-demo

Screenshot 2024-12-18 at 8 39 34 Screenshot 2024-12-18 at 8 40 30

tectiv3 avatar Dec 18 '24 08:12 tectiv3