OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

React Native - Converting Node.js Encrypt to crypto-js

  • Thread starter Thread starter Tonespy
  • Start date Start date
T

Tonespy

Guest
I've this code for encryption and decryption written in Node.js, and it makes use of Node's crypto:

Code:
const crypto = require('crypto');

const ENCRYPTION_KEY = 'some-random-key-12345';
const SALT = Buffer.from(ENCRYPTION_KEY, 'ascii');
const keyGenerator = crypto.pbkdf2Sync(ENCRYPTION_KEY, SALT, 1000, 48, 'sha1');
const key = keyGenerator.subarray(0, 32);
const iv = keyGenerator.subarray(32);

function encrypt(text) {
  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  let encrypted = cipher.update(text, 'utf16le', 'base64');
  encrypted += cipher.final('base64');
  return encrypted;
}

function decrypt(text) {
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  let decrypted = decipher.update(text, 'base64', 'utf16le');
  decrypted += decipher.final('utf16le');
  return decrypted;
}

// Example usage:
console.log(encrypt('some text to encrypt'));

I am trying to replicate the same thing in react native expo project's TypeScript using crypto-es, however, the output is always wrong. Here is my TypeScript implementation:

Code:
import { useCallback, useMemo } from 'react';
import CryptoES from 'crypto-es';

export type CryptographyProps = {
  readonly text: string;
};

const ENCRYPTION_KEY = 'some-random-key-12345';
const SALT = CryptoES.enc.Utf8.parse(ENCRYPTION_KEY);
const keySize = 256 / 32;
const iterationCount = 1000;

const getKeyAndIv = () => {
  const key = CryptoES.PBKDF2(ENCRYPTION_KEY, SALT, {
    keySize: keySize + 128 / 32,
    iterations: iterationCount,
    hasher: CryptoES.algo.SHA1,
  });

  return {
    key: CryptoES.enc.Hex.parse(key.toString(CryptoES.enc.Hex).slice(0, 64)),
    iv: CryptoES.enc.Hex.parse(key.toString(CryptoES.enc.Hex).slice(64, 96)),
  };
};

export const useNewCryptography = () => {
  const encrypt = useCallback(({ text }: CryptographyProps): string | undefined => {
    const { key, iv } = getKeyAndIv();
    const encrypted = CryptoES.AES.encrypt(CryptoES.enc.Utf16.parse(text), key, {
      iv: iv,
      padding: CryptoES.pad.Pkcs7,
      mode: CryptoES.mode.CBC,
    });

    return encrypted.toString();
  }, []);

  const decrypt = useCallback(({ text }: CryptographyProps): string | undefined => {
    const { key, iv } = getKeyAndIv();
    const decrypted = CryptoES.AES.decrypt(text, key, {
      iv: iv,
      padding: CryptoES.pad.Pkcs7,
      mode: CryptoES.mode.CBC,
    });

    return CryptoES.enc.Utf16.stringify(decrypted);
  }, []);

  return useMemo(() => ({ encrypt, decrypt }), [encrypt, decrypt]);
};

Any help will be appreciated.

Thanks,

<p>I've this code for encryption and decryption written in <code>Node.js</code>, and it makes use of Node's crypto:</p>
<pre><code>const crypto = require('crypto');

const ENCRYPTION_KEY = 'some-random-key-12345';
const SALT = Buffer.from(ENCRYPTION_KEY, 'ascii');
const keyGenerator = crypto.pbkdf2Sync(ENCRYPTION_KEY, SALT, 1000, 48, 'sha1');
const key = keyGenerator.subarray(0, 32);
const iv = keyGenerator.subarray(32);

function encrypt(text) {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf16le', 'base64');
encrypted += cipher.final('base64');
return encrypted;
}

function decrypt(text) {
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(text, 'base64', 'utf16le');
decrypted += decipher.final('utf16le');
return decrypted;
}

// Example usage:
console.log(encrypt('some text to encrypt'));
</code></pre>
<p>I am trying to replicate the same thing in react native expo project's TypeScript using <code>crypto-es</code>, however, the output is always wrong. Here is my TypeScript implementation:</p>
<pre><code>import { useCallback, useMemo } from 'react';
import CryptoES from 'crypto-es';

export type CryptographyProps = {
readonly text: string;
};

const ENCRYPTION_KEY = 'some-random-key-12345';
const SALT = CryptoES.enc.Utf8.parse(ENCRYPTION_KEY);
const keySize = 256 / 32;
const iterationCount = 1000;

const getKeyAndIv = () => {
const key = CryptoES.PBKDF2(ENCRYPTION_KEY, SALT, {
keySize: keySize + 128 / 32,
iterations: iterationCount,
hasher: CryptoES.algo.SHA1,
});

return {
key: CryptoES.enc.Hex.parse(key.toString(CryptoES.enc.Hex).slice(0, 64)),
iv: CryptoES.enc.Hex.parse(key.toString(CryptoES.enc.Hex).slice(64, 96)),
};
};

export const useNewCryptography = () => {
const encrypt = useCallback(({ text }: CryptographyProps): string | undefined => {
const { key, iv } = getKeyAndIv();
const encrypted = CryptoES.AES.encrypt(CryptoES.enc.Utf16.parse(text), key, {
iv: iv,
padding: CryptoES.pad.Pkcs7,
mode: CryptoES.mode.CBC,
});

return encrypted.toString();
}, []);

const decrypt = useCallback(({ text }: CryptographyProps): string | undefined => {
const { key, iv } = getKeyAndIv();
const decrypted = CryptoES.AES.decrypt(text, key, {
iv: iv,
padding: CryptoES.pad.Pkcs7,
mode: CryptoES.mode.CBC,
});

return CryptoES.enc.Utf16.stringify(decrypted);
}, []);

return useMemo(() => ({ encrypt, decrypt }), [encrypt, decrypt]);
};

</code></pre>
<p>Any help will be appreciated.</p>
<p>Thanks,</p>
 

Latest posts

Top