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

Decrpytion returns a blank string

Open hushaudio opened this issue 3 years ago • 1 comments

Hello, I am having some trouble decrypting files using react-native-aes. I am currently using nodejs crpyto to encrypt my files as shown in the code bellow, which works great. I can decrypt and listen to the audio just fine.


const secretKey = Buffer.from("cece3a7dc9cf86aae926fd2ee520a06e5a5b616fa9e381de53600121e8aff095", "hex"); // set random encryption key
const iv = Buffer.from("42b65779303f0bd9ec0fea6275d6f26c", "hex"); // set random initialisation vector
const algorithm = 'aes-256-cbc';

for (let i = 0; i < files.length; i++) {
    const file = files[i];
    if(file == "encrypted") continue
    if(file == ".DS_Store") continue
    await new Promise ((resolve, reject)=>{
                  
        // input file
        const r = fs.createReadStream('./dist/art/sounds/tickleride/'+file);

        // encrypt content
        const encryptCipher = crypto.createCipheriv(algorithm, secretKey, iv);
        
        // decrypt content
        const decryptDecipher = crypto.createDecipheriv(algorithm, secretKey, iv);
        
        // write file
        const w = fs.createWriteStream('./dist/art/sounds/tickleride/encrypted/'+file);

        // start pipe
        r.pipe(encryptCipher)
        .pipe(w).on("close", resolve)
    })
  }

But as soon as I bring my encrypted files and iv/key into react-native-aes I just get blank strings back.

import Aes from 'react-native-aes-crypto'

const algorithm = 'aes-256-cbc';
const key = "cece3a7dc9cf86aae926fd2ee520a06e5a5b616fa9e381de53600121e8aff095"
const iv = "42b65779303f0bd9ec0fea6275d6f26c"

export const decrypt = async (cipher) => {
  const result = await Aes.decrypt(cipher, key, iv, algorithm)
  return result
}

am I missing some conversion of the key and iv? Do I need to make them 16 and 32 char? If so how do I do that, theres no clear path to finding out how to shorten the key effectively.

EDIT: I have tried without using key/iv buffers to encode as well.

hushaudio avatar Jul 17 '22 17:07 hushaudio

For AES-256-CBC, the key length should be 32 bytes, yours is 64 bytes. The IV should also be 16 bytes, but yours is 32. It seems you have forgotten to convert the key and IV from hexadecimal to string representation.

snake302 avatar Jan 30 '23 14:01 snake302