TypeError:无法读取未定义的属性“random”-crypto-js
2019-01-24
3276
我在
reactjs
中使用
crypto-js
,在 localhost 上一切正常。
但在使用 chrome 的服务器上,我收到此错误消息:
TypeError: Cannot read property 'random' of undefined
在 Firefox 上:
TypeError: "r is undefined"
我的代码:
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 !
我的函数:
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",
我不知道问题出在哪里!我删除了
node_modules
,但仍然收到相同的错误。
我的网站:
http://posweb.ccg24.com/signin
已更新
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;
}
2个回答
无需使用
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>
I_Al-thamary
2019-01-24
您可以尝试:
import * as CryptoJS from 'crypto-js';
Luan Persini
2023-01-05