0

I'm using crypto-js inside reactjs and everything works fine on localhost. but on server with chrome I got this error message:

TypeError: Cannot read property 'random' of undefined

on firefox:

TypeError: "r is undefined"

my code:

import CryptoJS from 'crypto-js';

console.log('text',text); //printed on console as well
var p = randomString(10) 
console.log('p',p) //printed on console as well
var c = CryptoJS.AES.encrypt(text,p).toString(); // error line
console.log('crypted',c+p)//not printed !

my function:

  function setWindow(text){
    console.log('text',text);
    var p = randomString(10)
    console.log('p',p)
    var c = CryptoJS.AES.encrypt(text,p).toString();
    console.log('crypted',c+p)
    return c+p;
  }

"crypto-js": "^3.1.9-1",

I don't know where is my problem! I removed node_modules, but I got the same error. my site: http://posweb.ccg24.com/signin

updated

  function randomString(length) {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < length; i++)
      text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
  }
9
  • Try npm install in your project folder on the server. Commented Jan 24, 2019 at 8:18
  • yes, I tested it again and again. Commented Jan 24, 2019 at 8:19
  • Which library are you using for randomString ? Commented Jan 24, 2019 at 8:21
  • I added randomString to question, but the error refers to random function inside the library. I think. Commented Jan 24, 2019 at 8:24
  • see stackoverflow.com/questions/29072744/… Commented Jan 24, 2019 at 8:24

2 Answers 2

0

You can generate random without using Math.random

var _ = require('lodash');
var CryptoJS = require("crypto-js");
var p;
function randomString(length) {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

 var text= _.sampleSize(possible , length).join('');

  return text;
}

function setWindow(text){
    console.log('text',text);
     p = randomString(10)
    console.log('p',p)
    var c = CryptoJS.AES.encrypt(text,p).toString();
    console.log('crypted',c+p)
    return c+p;
  }



var ciphertext=setWindow("plain text");
var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), p);
var plaintext = bytes.toString(CryptoJS.enc.Utf8);

console.log("decrypte",plaintext);

function randomString(length) {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < length; i++)
      text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
  }




function setWindow(text){
 

    console.log('text',text);
    var p = randomString(10)
    console.log('p',p)
    var c =CryptoJS.AES.encrypt(text,p);
    console.log('crypted',c+p)
    return c+p;
}
var text="Plain Text";
setWindow(text);    
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>

The code here:https://repl.it/@ibrahimth/LastSatisfiedBrain

Sign up to request clarification or add additional context in comments.

3 Comments

my problem isn't for my randomString function. because I got the correct response on my function.
@S.M_Emamian try it now.
@S.M_Emamian first check this if you have a problem:stackoverflow.com/questions/38479667/…
0

You can try:

import * as CryptoJS from 'crypto-js';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.