5

My Code working with debug but when I try to run giving this error . Why It's happens.

var CryptoJS = require("crypto-js");
require('dotenv').config();

getText = process.env.USER;

var bytes  = CryptoJS.AES.decrypt(getText, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);
 
console.log(originalText); 

and when I try read from .env giving this error.

safa aytan
  • 431
  • 1
  • 6
  • 16
  • is `process.env.USER` defined? Can you `console.log` it? – eol Jun 29 '20 at 10:40
  • Does this answer your question? [How to decrypt message with CryptoJS AES. I have a working Ruby example](https://stackoverflow.com/questions/14958103/how-to-decrypt-message-with-cryptojs-aes-i-have-a-working-ruby-example) – Nir Alfasi Jun 29 '20 at 10:42
  • @eol yes That is not undefined When ı run with debugger There is no problem. but When ı run giving this err – safa aytan Jun 29 '20 at 10:54

2 Answers2

3

This happens when getText is null or undefined

  • Affirmative. In other words, the error occurs when the _plaintext_ you are trying to encrypt is *undefined*. – Keith OYS Aug 13 '21 at 07:12
0

I have same yours and found my solutions:

Possibly: Check again your data encrypt, is that encrypt in a right way ? same key, same library ? json.stringify and json.parse ? Your .env file set up right ? Cause you don't show that code so we can not give more details.

Tony
  • 259
  • 3
  • 13