'Encrypt CryptoJS without special characters
using nodejs I am trying to generate an unique URL for user to conform email address. From that URL user will be able to verify the email account by decrypting the ciphertext and comparing ciphertext data with database . I am using CryptoJS to generate the url.
let url = 'http://localhost:4000/newUser/get/'+ciphertext ;
Problem is that in ciphertext, it contains forward slash " / " eg:
http://localhost:4000/newUser/get/U2FsdGVkX189ZNKKQrYgqU90DDwkl/W3hRTSGO1yvUMaDilPJmz9YYI3d1/E3i9C
Router is processing " / " on the URL, thus router is searching for the directory that is actually part of ciphertext. If there is any solution for not including " / " or special characters in ciphertext, please help. Thanks in advance.
Solution 1:[1]
You can easily replace the special characters with any of text like:
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Do not forget to replace these strings back with special characters
dataString.toString().replace('xMl3Jk', '+' ).replace('Por21Ld', '/').replace('Ml32', '=');
Hope this will help to solve your problem
Solution 2:[2]
However, .replace() will only replace first occurrence.
To be more precise, you can use something like this :
// 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);
Solution 3:[3]
IMHO, for @JaiKumarRajput's answer,
He encoded the string with,
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Now, I have no idea how xMl3Jk, Por21Ld, Ml32 works. So, i also don't know if it can mess my string somehow.
Plus, As I have to perform this on decoding as well. So, Why wont I use something like this (What already exists),
encodeURIComponent(ciphertext.toString('base64'))
I know it still introduces % char. But as its getting used in URL. In which its a escape char.
How does it matter more than doing something that can mess my code up ??
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.
Solution 4:[4]
There is no option for excluding / while crypto generate a encryped string.
I had face the same issue and then i found 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)
Solution 5:[5]
// 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)
you can use like this. first encode your ciphertext like above and concatenate that encodeText with ulr
let url = 'http://localhost:4000/newUser/get/'+encodeText ;
and then decode that encodeText from ulr
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jai Kumar Rajput |
| Solution 2 | Darshan Gada |
| Solution 3 | Marvell Javoni |
| Solution 4 | piash tanjin |
| Solution 5 | Bilal |
