To convert an IV created from Crypto.randomBytes(16) to string & then convert it back to array buffer (which Crypto.createDecipheriv() can consume). Basically, we are sending IV in string format from the frontend & the backend will consume it to decipher data.
Problem
When we convert this IV to string & then convert it back to ArrayBuffer, they aren't equal/same. This is causing issues in decrypting the encrypted data.
import Crypto from 'react-native-quick-crypto';
import {Buffer} from '@craftzdog/react-native-buffer';
const IV = Crypto.randomBytes(IV_LENGTH_IN_BYTES);
// convert IV to UTF-8 string
const stringIv = IV.toString(); // Buffer.from(IV).toString('utf8') can also be used, produces same result
// convert string back to buffer to consume it in Crypto.createDecipheriv
const ivBuffer = Buffer.from(iv, 'utf-8');
Expectation
IV & ivArrayBuffer above should be equal, but they aren't. The length & data both are different.
Goal
To convert an IV created from
Crypto.randomBytes(16)
to string & then convert it back to array buffer (whichCrypto.createDecipheriv()
can consume). Basically, we are sending IV in string format from the frontend & the backend will consume it to decipher data.Problem
When we convert this IV to string & then convert it back to ArrayBuffer, they aren't equal/same. This is causing issues in decrypting the encrypted data.
Expectation
IV
&ivArrayBuffer
above should be equal, but they aren't. The length & data both are different.Sharing an example of values here:
Reproduction
You can run the minimal sample app here where this issue is reproduced - https://github.com/gupta-ji6/react-native-test-app/blob/issue/utf8/cipher.ts
P.S. Let me know if I'm converting them incorrectly, I'm not hands-on with Node APIs.
Environment
same issue on RN 0.72.5 as well.