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

"Invalid iv" error from crypto.createDecipheriv() with IV extracted by Buffer.from() #214

Open
gupta-ji6 opened this issue Dec 15, 2023 · 1 comment

Comments

@gupta-ji6
Copy link

gupta-ji6 commented Dec 15, 2023

Goal

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.

Sharing an example of values here:

IV: {"data": [222, 112, 138, 17, 67, 95, 56, 53, 205, 113, 76, 30, 193, 161, 78, 59], "type": "Buffer"}

stringIv: �p�C_85�qL��N;

ivBuffer: {"data": [239, 191, 189, 112, 239, 191, 189, 17, 67, 95, 56, 53, 239, 191, 189, 113, 76, 30, 239, 191, 189, 239, 191, 189, 78, 59], "type": "Buffer"}

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.

	"@craftzdog/react-native-buffer": "6.0.5",
    "react": "18.2.0",
    "react-native": "0.73.0",
    "react-native-quick-base64": "2.0.8",
    "react-native-quick-crypto": "0.6.1"
@shamilovtim
Copy link
Contributor

Fixed by #264 i believe. make sure to pass the authTagLength you're going for

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

2 participants