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.
function press(string) {
string = unescape(encodeURIComponent(string));
var newString = '',
char, nextChar, binedCharCode;
for (var i = 0; i < string.length; i += 2) {
char = string.charCodeAt(i);
if ((i + 1) < string.length) {
nextChar = string.charCodeAt(i + 1) - 31;
binedCharCode = char + "" + nextChar.toLocaleString('en', {
minimumIntegerDigits: 2
});
newString += String.fromCharCode(parseInt(binedCharCode, 10));
} else {
newString += string.charAt(i);
}
}
return newString;
}
function depress(string) {
var newString = '',
char, codeStr, firstCharCode, lastCharCode;
for (var i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char > 132) {
codeStr = char.toString(10);
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
} else {
newString += string.charAt(i);
}
}
return newString;
}
var stringToCompress = 'awesome';
var pressedString = press(stringToCompress);
var depressedString = depress(pressedString);
console.log("encrypted :",pressedString);
console.log("decrypted :",depressedString);
Currently the output from the sting="awesome" is
encrypted: ☼⟈⮪e
decrypted: awesome
I want similar encryption but must be only in alphanumeric values and not symbols.
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.
function press(string) {
string = unescape(encodeURIComponent(string));
var newString = '',
char, nextChar, binedCharCode;
for (var i = 0; i < string.length; i += 2) {
char = string.charCodeAt(i);
if ((i + 1) < string.length) {
nextChar = string.charCodeAt(i + 1) - 31;
binedCharCode = char + "" + nextChar.toLocaleString('en', {
minimumIntegerDigits: 2
});
newString += String.fromCharCode(parseInt(binedCharCode, 10));
} else {
newString += string.charAt(i);
}
}
return newString;
}
function depress(string) {
var newString = '',
char, codeStr, firstCharCode, lastCharCode;
for (var i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char > 132) {
codeStr = char.toString(10);
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
} else {
newString += string.charAt(i);
}
}
return newString;
}
var stringToCompress = 'awesome';
var pressedString = press(stringToCompress);
var depressedString = depress(pressedString);
console.log("encrypted :",pressedString);
console.log("decrypted :",depressedString);
Currently the output from the sting="awesome" is
encrypted: ☼⟈⮪e
decrypted: awesome
I want similar encryption but must be only in alphanumeric values and not symbols.
Share Improve this question asked Jun 6, 2021 at 4:36 Sangye ChangbaSangye Changba 131 silver badge2 bronze badges1 Answer
Reset to default 6I 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:
function press(string) {
string = unescape(encodeURIComponent(string));
var newString = '',
char, nextChar, binedCharCode;
for (var i = 0; i < string.length; i += 2) {
char = string.charCodeAt(i);
if ((i + 1) < string.length) {
nextChar = string.charCodeAt(i + 1) - 31;
binedCharCode = char + "" + nextChar.toLocaleString('en', {
minimumIntegerDigits: 2
});
newString += String.fromCharCode(parseInt(binedCharCode, 10));
} else {
newString += string.charAt(i);
}
}
return btoa(unescape(encodeURIComponent(newString)));
}
function depress(string) {
var newString = '',
char, codeStr, firstCharCode, lastCharCode;
string = decodeURIComponent(escape(atob(string)));
for (var i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char > 132) {
codeStr = char.toString(10);
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
} else {
newString += string.charAt(i);
}
}
return newString;
}
var stringToCompress = 'awesome';
var pressedString = press(stringToCompress);
var depressedString = depress(pressedString);
console.log("encrypted :",pressedString);
console.log("decrypted :",depressedString);
Or if you trully want alphanumerical, than you can simply convert it into HEX:
function press(string) {
string = unescape(encodeURIComponent(string));
var newString = '',
char, nextChar, binedCharCode;
for (var i = 0; i < string.length; i += 2) {
char = string.charCodeAt(i);
if ((i + 1) < string.length) {
nextChar = string.charCodeAt(i + 1) - 31;
binedCharCode = char + "" + nextChar.toLocaleString('en', {
minimumIntegerDigits: 2
});
newString += String.fromCharCode(parseInt(binedCharCode, 10));
} else {
newString += string.charAt(i);
}
}
return newString.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(4,"0"),"");
}
function depress(string) {
var newString = '',
char, codeStr, firstCharCode, lastCharCode;
string = string.match(/.{1,4}/g).reduce((acc,char)=>acc+String.fromCharCode(parseInt(char, 16)),"");
for (var i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char > 132) {
codeStr = char.toString(10);
firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
} else {
newString += string.charAt(i);
}
}
return newString;
}
var stringToCompress = 'awesome';
var pressedString = press(stringToCompress);
var depressedString = depress(pressedString);
console.log("encrypted :",pressedString);
console.log("decrypted :",depressedString);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367032a4624629.html
评论列表(0条)