How to properly encrypt AES256 method using CryptoJs


encourage

Hi I'm new to React Native, I can encrypt data with PHP but not with React Native using Crypto JS. (results that lead to JS are always different, the correct one is from PHP)

Here is an example in PHP:

<?php

$data = 'my1234567';
$iv = 'yourivare1234567';
$key = '356d9abc7532ceb0945b615a622c3370';

$abc = openssl_encrypt($data, 'aes-256-cbc', $key, 0,  $iv);
var_dump($abc);
// result is : string(24) "9EF/QLpR+o/KrVueiI4L0g=="

Now I try to replicate it in my React Native app using Crypto JS. But the result is always different, I expected the result with hardcoded data and iv like above: "9EF/QLpR+o/KrVueiI4L0g=="

Here is the source code in JS:

const data = 'my1234567';
const iv = 'yourivare1234567';
const key = '356d9abc7532ceb0945b615a622c3370';

const fkey = CryptoJS.enc.Hex.parse(key);
const fiv = CryptoJS.enc.Hex.parse(iv);

const enc = CryptoJS.AES.encrypt(data, md5key, {
        iv: fiv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
});

const final = enc.ciphertext.toString(CryptoJS.enc.Base64);
console.log('encrypted password: ' , final) // result is kYLFiwI1IDZcFfsKsbrbzg==

Can someone help?

thank you before

Topaco

fkeyAnd fivmust be parsed with UTF8 encoder. md5keyundefined, must be replaced with fkey:

const data = 'my1234567';
const iv = 'yourivare1234567';
const key = '356d9abc7532ceb0945b615a622c3370';

const fkey = CryptoJS.enc.Utf8.parse(key);
const fiv = CryptoJS.enc.Utf8.parse(iv);

const enc = CryptoJS.AES.encrypt(data, fkey, {
        iv: fiv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
});

const final = enc.ciphertext.toString(CryptoJS.enc.Base64);
console.log('encrypted password: ' , final) // result is 9EF/QLpR+o/KrVueiI4L0g==
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

Note (for testing purposes) that static IVs must not be used for security reasons.

Related


How to properly encrypt AES256 method using CryptoJs

encourage Hi I'm new to React Native, I can encrypt data with PHP but not with React Native using Crypto JS. (results that lead to JS are always different, the correct one is from PHP) Here is an example in PHP: <?php $data = 'my1234567'; $iv = 'yourivare1234

How to properly encrypt AES256 method using CryptoJs

encourage Hi I'm new to React Native, I can encrypt data with PHP but not with React Native using Crypto JS. (results that lead to JS are always different, the correct one is from PHP) Here is an example in PHP: <?php $data = 'my1234567'; $iv = 'yourivare1234

How to properly encrypt AES256 method using CryptoJs

encourage Hi I'm new to React Native, I can encrypt data with PHP but not with React Native using Crypto JS. (results that lead to JS are always different, the correct one is from PHP) Here is an example in PHP: <?php $data = 'my1234567'; $iv = 'yourivare1234

How to properly encrypt AES256 method using CryptoJs

encourage Hi I'm new to React Native, I can encrypt data with PHP but not with React Native using Crypto JS. (results that lead to JS are always different, the correct one is from PHP) Here is an example in PHP: <?php $data = 'my1234567'; $iv = 'yourivare1234

How to encrypt "sam package" with AES256?

overtrading From here I see the syntax: sam package \ --template-file /path_to_template/template.yaml \ --s3-bucket bucket-name \ --s3-prefix appname/branchname/version --output-template-file packaged-template.yaml or aws cloudformation pac

How to encrypt "sam package" with AES256?

overtrading From here I see the syntax: sam package \ --template-file /path_to_template/template.yaml \ --s3-bucket bucket-name \ --s3-prefix appname/branchname/version --output-template-file packaged-template.yaml or aws cloudformation pac

Encrypt/decrypt image using AES256 in Matlab

Joe or I have to encrypt and decrypt an image using AES256. I am working on the below program which encrypts plain text. AES is an algorithm with a fixed-length 128-bit input. Each round is divided into four distinct steps. The program shows another algorithm,

Encrypt/decrypt image using AES256 in Matlab

Joe or I have to encrypt and decrypt an image using AES256. I am working on the below program which encrypts plain text. AES is an algorithm with a fixed-length 128-bit input. Each round is divided into four distinct steps. The program shows another algorithm,

How to encrypt in AES with CryptoJS with key size 128?

Wu I searched and found an example of AES with default 256 key size and found that it already works. However, when I want to use 128 key size, there is very little information. I have extracted the code from CryptoJS's aes test: var C = CryptoJS;

How to encrypt in AES with CryptoJS with key size 128?

Wu I searched and found an example of AES with default 256 key size and found that it already works. However, when I want to use 128 key size, there is very little information. I have extracted the code from CryptoJS's aes test: var C = CryptoJS;

How to encrypt in AES with CryptoJS with key size 128?

Wu I searched and found an example of AES with default 256 key size and found that it already works. However, when I want to use 128 key size, there is very little information. I have extracted the code from CryptoJS's aes test: var C = CryptoJS;

How to encrypt in AES with CryptoJS with key size 128?

Wu I searched and found an example of AES with default 256 key size and found that it already works. However, when I want to use 128 key size, there is very little information. I have extracted the code from CryptoJS's aes test: var C = CryptoJS;

CryptoJS AES256 decryption problem

Toharawk I'm having some problems decrypting an AES256 encoded URL. For the decryption process using CryptoJS, but the output throws the following exception: Malformed UTF-8 data What did I miss? code: router.post("/signin/:eoLogin2", async (req, res) => {

CryptoJS AES256 decryption problem

Toharawk I'm having some problems decrypting an AES256 encoded URL. For the decryption process using CryptoJS, but the output throws the following exception: Malformed UTF-8 data What did I miss? code: router.post("/signin/:eoLogin2", async (req, res) => {

CryptoJS AES256 decryption problem

Toharawk I'm having some problems decrypting an AES256 encoded URL. For the decryption process using CryptoJS, but the output throws the following exception: Malformed UTF-8 data What did I miss? code: router.post("/signin/:eoLogin2", async (req, res) => {

CryptoJS AES256 decryption problem

Toharawk I'm having some problems decrypting an AES256 encoded URL. For the decryption process using CryptoJS, but the output throws the following exception: Malformed UTF-8 data What did I miss? code: router.post("/signin/:eoLogin2", async (req, res) => {

How to decrypt with CryptoJS using AES?

User 3197788 With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right. I am encrypting like this: var ENC_KEY = "bXlrZXk="; //"mykey" var encrypted = CryptoJS.AES.encrypt("hello", Crypto

How to decrypt with CryptoJS using AES?

User 3197788 With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right. I am encrypting like this: var ENC_KEY = "bXlrZXk="; //"mykey" var encrypted = CryptoJS.AES.encrypt("hello", Crypto

How to implement CryptoJS.AES.encrypt function in Java?

Rizeski I am trying to implement the following code for encryption using crypto-js in java let toEncrypt= "my data"; cryptoJs.AES.encrypt(toEncrypt,"apasswordblabla").toString(); Here is my implementation (AES/CBC/PKCS7Padding): public String encrypt(Map<Stri

Encrypt with AES with phpseclib and decrypt with CryptoJS

Roberto 14 I'm trying to encrypt a string using phpseclib AES in CBC mode (the library's default): $cipher = new Crypt_AES(); $cipher->setKey('abcdefghijklmnop'); $cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3)); $cipher->encrypt("hello worl

Encrypt with AES with phpseclib and decrypt with CryptoJS

Roberto 14 I'm trying to encrypt a string using phpseclib AES in CBC mode (the library's default): $cipher = new Crypt_AES(); $cipher->setKey('abcdefghijklmnop'); $cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3)); $cipher->encrypt("hello worl

Java encrypt file using aes256/CBC/PKCS7Padding

Majlena I am trying to encrypt a file using aes256-CBC-PKCS7Padding. I am using bouncy castle library and I am getting exception java.lang.IllegalArgumentException: invalid parameter passed to AES init - org.bouncycastle.crypto.params.ParametersWithIV at org.b

Java encrypt file using aes256/CBC/PKCS7Padding

Majlena I am trying to encrypt a file using aes256-CBC-PKCS7Padding. I am using bouncy castle library and I am getting exception java.lang.IllegalArgumentException: invalid parameter passed to AES init - org.bouncycastle.crypto.params.ParametersWithIV at org.b

Java Encrypt a file using aes256/ CBC/PKCS7Padding

Majlena I'm trying to encrypt a file using aes256- CBC-PKCS7Padding . I'm using bouncy castle library , but I get exception java.lang.IllegalArgumentException: invalid parameter passed to AES init - org.bouncycastle.crypto.params.ParametersWithIV at org.bouncy