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

Issue when encrypt

Open celciusjj opened this issue 4 years ago • 5 comments

Error: exception decoding Hex string: invalid characters encountered in Hex string

celciusjj avatar Jan 15 '21 16:01 celciusjj

Getting same error, have you found any solutions?

arunahuja94 avatar Mar 05 '21 08:03 arunahuja94

I also had this issue, my key + IV used had special characters in, i.e an =

If you just a normal alphanumeric key + IV, i.e say 591825e3a4f2c9b8f73eb963c77ad160d4802ad7aadc179b066275bcb9d9cfd2 then it seems to work.. seems rather counter intuitive, particularly if you're generating random keys. const cipherText = await Aes.encrypt(data, "591825e3a4f2c9b8f73eb963c77ad160d4802ad7aadc179b066275bcb9d9cfd2", "0123456789abcdef0123456789abcde");

const randomKey = await Aes.randomKey(32);
const randomIv = await Aes.randomKey(16);

const cipherText = await Aes.encrypt(data, randomKey, randomIv);

iJimmyWei avatar Mar 10 '21 12:03 iJimmyWei

Im getting the same error.

i let my code here, if you found any solution please let me know.

function buildSign(data, config) {
  try {
    encryptData(data.toString(), config)
    .then(({ cipher, iv }) => {
        console.log('Encrypted:', cipher)

        Aes.hmac256(cipher, config).then(hash => {
            console.log('HMAC', hash)
        })
    })
    .catch(error => {
        console.log(error)
    })

  } catch (e) {
    console.error(e)
  }
  //return crypto.createHmac('sha256', config.API_SECRET).update(data).digest('hex');
}

image

Thank you so much! Sorry for my english. 😋

joan17cast avatar Mar 16 '21 17:03 joan17cast

same issue, but apparently you should try to generate a random key rather then passing your own key, that was the issue for me i added my own key that had apparently invalid caracters, use this instead

const handleSubmitForm = async (values, hash) => { const randomIv = await Aes.randomKey(16);

try {
  console.log('Key:', randomIv);
  encryptData('k]]xM2uPFpkLWPBas&}46^FN.H9738', randomIv)
    .then(({cipher, iv}) => {
      console.log('Encrypted:', cipher);
    })
    .catch(error => {
      console.log('error', error);
    });
} catch (e) {
  console.error(e);
}

};

MedRaid avatar Mar 17 '22 16:03 MedRaid