I want to create a function to encrypt a string which will shorten the string into alphanumeric character and also create a function decrypt which will get back the encrypted string.
Here is what I coded by taking reference online.
JavaScript
x
58
58
1
function compress(string) {
2
string = unescape(encodeURIComponent(string));
3
var newString = '',
4
char, nextChar, combinedCharCode;
5
for (var i = 0; i < string.length; i += 2) {
6
char = string.charCodeAt(i);
7
8
if ((i + 1) < string.length) {
9
10
11
nextChar = string.charCodeAt(i + 1) - 31;
12
13
14
combinedCharCode = char + "" + nextChar.toLocaleString('en', {
15
minimumIntegerDigits: 2
16
});
17
18
newString += String.fromCharCode(parseInt(combinedCharCode, 10));
19
20
} else {
21
22
23
newString += string.charAt(i);
24
}
25
}
26
return newString;
27
}
28
29
function decompress(string) {
30
31
var newString = '',
32
char, codeStr, firstCharCode, lastCharCode;
33
34
for (var i = 0; i < string.length; i++) {
35
char = string.charCodeAt(i);
36
if (char > 132) {
37
codeStr = char.toString(10);
38
39
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
40
41
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
42
43
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
44
} else {
45
newString += string.charAt(i);
46
}
47
}
48
return newString;
49
}
50
51
var stringToCompress = 'awesome';
52
var compressedString = compress(stringToCompress);
53
var decompressedString = decompress(compressedString);
54
55
56
console.log("encrypted :",compressedString);
57
console.log("decrypted :",decompressedString);
58
Currently the output from the sting=”awesome” is
JavaScript
1
3
1
encrypted: ☼⟈⮪e
2
decrypted: awesome
3
I want similar encryption but must be only in alphanumeric values and not symbols.
Advertisement
Answer
I don’t know what is your goal (is it make the string shorter, but binary or encrypted and in ascii range), so if it’s the later, than you could use base64
encoding:
JavaScript
1
57
57
1
function compress(string) {
2
string = unescape(encodeURIComponent(string));
3
var newString = '',
4
char, nextChar, combinedCharCode;
5
for (var i = 0; i < string.length; i += 2) {
6
char = string.charCodeAt(i);
7
8
if ((i + 1) < string.length) {
9
10
11
nextChar = string.charCodeAt(i + 1) - 31;
12
13
14
combinedCharCode = char + "" + nextChar.toLocaleString('en', {
15
minimumIntegerDigits: 2
16
});
17
18
newString += String.fromCharCode(parseInt(combinedCharCode, 10));
19
20
} else {
21
22
23
newString += string.charAt(i);
24
}
25
}
26
return btoa(unescape(encodeURIComponent(newString)));
27
}
28
29
function decompress(string) {
30
31
var newString = '',
32
char, codeStr, firstCharCode, lastCharCode;
33
string = decodeURIComponent(escape(atob(string)));
34
for (var i = 0; i < string.length; i++) {
35
char = string.charCodeAt(i);
36
if (char > 132) {
37
codeStr = char.toString(10);
38
39
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
40
41
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
42
43
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
44
} else {
45
newString += string.charAt(i);
46
}
47
}
48
return newString;
49
}
50
51
var stringToCompress = 'awesome';
52
var compressedString = compress(stringToCompress);
53
var decompressedString = decompress(compressedString);
54
55
56
console.log("encrypted :",compressedString);
57
console.log("decrypted :",decompressedString);
Or if you trully want alphanumerical, than you can simply convert it into HEX:
JavaScript
1
57
57
1
function compress(string) {
2
string = unescape(encodeURIComponent(string));
3
var newString = '',
4
char, nextChar, combinedCharCode;
5
for (var i = 0; i < string.length; i += 2) {
6
char = string.charCodeAt(i);
7
8
if ((i + 1) < string.length) {
9
10
11
nextChar = string.charCodeAt(i + 1) - 31;
12
13
14
combinedCharCode = char + "" + nextChar.toLocaleString('en', {
15
minimumIntegerDigits: 2
16
});
17
18
newString += String.fromCharCode(parseInt(combinedCharCode, 10));
19
20
} else {
21
22
23
newString += string.charAt(i);
24
}
25
}
26
return newString.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(4,"0"),"");
27
}
28
29
function decompress(string) {
30
31
var newString = '',
32
char, codeStr, firstCharCode, lastCharCode;
33
string = string.match(/.{1,4}/g).reduce((acc,char)=>acc+String.fromCharCode(parseInt(char, 16)),"");
34
for (var i = 0; i < string.length; i++) {
35
char = string.charCodeAt(i);
36
if (char > 132) {
37
codeStr = char.toString(10);
38
39
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
40
41
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
42
43
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
44
} else {
45
newString += string.charAt(i);
46
}
47
}
48
return newString;
49
}
50
51
var stringToCompress = 'awesome';
52
var compressedString = compress(stringToCompress);
53
var decompressedString = decompress(compressedString);
54
55
56
console.log("encrypted :",compressedString);
57
console.log("decrypted :",decompressedString);