加密没有特殊字符的CryptoJS
Encrypt CryptoJS without special characters
使用 nodejs
我正在尝试为用户生成一个唯一的 URL 以 符合电子邮件地址 。从那个 URL 用户将能够 通过解密密文和 将密文数据与数据库 进行比较来 验证电子邮件帐户 。我正在使用 CryptoJS 来生成 url。
let url = 'http://localhost:4000/newUser/get/'+ciphertext ;
问题是在密文中,它包含正斜杠“/”例如:
http://localhost:4000/newUser/get/U2FsdGVkX189ZNKKQrYgqU90DDwkl/W3hRTSGO1yvUMaDilPJmz9YYI3d1/E3i9C
路由器正在 处理 URL 上的“/”,因此路由器正在搜索实际上是密文[=31]一部分的目录 =]. 密文中不包含“/”或特殊字符,如有解决方法,请帮忙解决。 提前致谢。
您可以轻松地将特殊字符替换为任何文本,例如:
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
不要忘记用特殊字符替换这些字符串
dataString.toString().replace('xMl3Jk', '+' ).replace('Por21Ld', '/').replace('Ml32', '=');
希望这对解决您的问题有所帮助
但是,.replace()
只会替换第一个出现的地方。
更准确地说,你可以使用这样的东西:
// Original Text
var ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
// Replaced Text
var dataString = ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
console.log(dataString);
// Back to Original Text
ciphertext = dataString.replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
console.log(ciphertext);
恕我直言,对于@JaiKumarRajput 的回答,
他用
对字符串进行了编码
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
现在,我不知道 xMl3Jk
、Por21Ld
、Ml32
是如何工作的。所以,我也不知道它是否会以某种方式弄乱我的字符串。
另外,因为我也必须在解码时执行此操作。那么,为什么我不使用这样的东西(已经存在的东西),
encodeURIComponent(ciphertext.toString('base64'))
我知道它仍然引入了 %
字符。但随着它在 URL 中的使用。其中它是一个转义字符。
这比做一些会弄乱我的代码的事情更重要吗??
NOTE: I used it and had no issue, It doesn't mean I either had found any issue with the top one. That didn't feel neat. Its only my humble opinion, So if u don't like it? Ignore it.
当 crypto 生成加密字符串时,没有排除 / 的选项。
我遇到了同样的问题,然后我发现 urlencode
const urlencode = require('urlencode');
let xx = {
chatRoomId: 37,
userId: 1,
doctorId: 2
}
xx = JSON.stringify(xx)
//encode
let x = (urlencode(xx, 'gbk')); // '%CB%D5%C7%A7'
// decode gbk
let y = urlencode.decode(x, 'gbk'); // original plain text
console.log(y)
// Original Text
let ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
let encodeText = encodeURIComponent(ciphertext);
console.log(encodeText)
let decodeText = decodeURIComponent(encodeText);
console.log(decodeText)
你可以这样使用。
首先像上面那样对你的密文进行编码,然后将该 encodeText 与 ulr
连接起来
let url = 'http://localhost:4000/newUser/get/'+encodeText ;
然后从 ulr
解码 encodeText
使用 nodejs 我正在尝试为用户生成一个唯一的 URL 以 符合电子邮件地址 。从那个 URL 用户将能够 通过解密密文和 将密文数据与数据库 进行比较来 验证电子邮件帐户 。我正在使用 CryptoJS 来生成 url。
let url = 'http://localhost:4000/newUser/get/'+ciphertext ;
问题是在密文中,它包含正斜杠“/”例如:
http://localhost:4000/newUser/get/U2FsdGVkX189ZNKKQrYgqU90DDwkl/W3hRTSGO1yvUMaDilPJmz9YYI3d1/E3i9C
路由器正在 处理 URL 上的“/”,因此路由器正在搜索实际上是密文[=31]一部分的目录 =]. 密文中不包含“/”或特殊字符,如有解决方法,请帮忙解决。 提前致谢。
您可以轻松地将特殊字符替换为任何文本,例如:
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
不要忘记用特殊字符替换这些字符串
dataString.toString().replace('xMl3Jk', '+' ).replace('Por21Ld', '/').replace('Ml32', '=');
希望这对解决您的问题有所帮助
但是,.replace()
只会替换第一个出现的地方。
更准确地说,你可以使用这样的东西:
// Original Text
var ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
// Replaced Text
var dataString = ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
console.log(dataString);
// Back to Original Text
ciphertext = dataString.replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
console.log(ciphertext);
恕我直言,对于@JaiKumarRajput 的回答,
他用
对字符串进行了编码ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
现在,我不知道 xMl3Jk
、Por21Ld
、Ml32
是如何工作的。所以,我也不知道它是否会以某种方式弄乱我的字符串。
另外,因为我也必须在解码时执行此操作。那么,为什么我不使用这样的东西(已经存在的东西),
encodeURIComponent(ciphertext.toString('base64'))
我知道它仍然引入了 %
字符。但随着它在 URL 中的使用。其中它是一个转义字符。
这比做一些会弄乱我的代码的事情更重要吗??
NOTE: I used it and had no issue, It doesn't mean I either had found any issue with the top one. That didn't feel neat. Its only my humble opinion, So if u don't like it? Ignore it.
当 crypto 生成加密字符串时,没有排除 / 的选项。
我遇到了同样的问题,然后我发现 urlencode
const urlencode = require('urlencode');
let xx = {
chatRoomId: 37,
userId: 1,
doctorId: 2
}
xx = JSON.stringify(xx)
//encode
let x = (urlencode(xx, 'gbk')); // '%CB%D5%C7%A7'
// decode gbk
let y = urlencode.decode(x, 'gbk'); // original plain text
console.log(y)
// Original Text
let ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
let encodeText = encodeURIComponent(ciphertext);
console.log(encodeText)
let decodeText = decodeURIComponent(encodeText);
console.log(decodeText)
你可以这样使用。 首先像上面那样对你的密文进行编码,然后将该 encodeText 与 ulr
连接起来let url = 'http://localhost:4000/newUser/get/'+encodeText ;
然后从 ulr
解码 encodeText