algorithm - Encrypt and decrypt a string using simple Javascript without using any external library - Stack Overflow

I want to create a function to encrypt a string which will shorten the string into alphanumeric charact

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

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:

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信