0

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 compress(string) {
  string = unescape(encodeURIComponent(string));
  var newString = '',
    char, nextChar, combinedCharCode;
  for (var i = 0; i < string.length; i += 2) {
    char = string.charCodeAt(i);

    if ((i + 1) < string.length) {

      
      nextChar = string.charCodeAt(i + 1) - 31;

      
      combinedCharCode = char + "" + nextChar.toLocaleString('en', {
        minimumIntegerDigits: 2
      });

      newString += String.fromCharCode(parseInt(combinedCharCode, 10));

    } else {

     
      newString += string.charAt(i);
    }
  }
  return newString;
}

function decompress(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 compressedString = compress(stringToCompress);
var decompressedString = decompress(compressedString);


console.log("encrypted :",compressedString);
console.log("decrypted :",decompressedString);

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.

1 Answer 1

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 compress(string) {
  string = unescape(encodeURIComponent(string));
  var newString = '',
    char, nextChar, combinedCharCode;
  for (var i = 0; i < string.length; i += 2) {
    char = string.charCodeAt(i);

    if ((i + 1) < string.length) {

      
      nextChar = string.charCodeAt(i + 1) - 31;

      
      combinedCharCode = char + "" + nextChar.toLocaleString('en', {
        minimumIntegerDigits: 2
      });

      newString += String.fromCharCode(parseInt(combinedCharCode, 10));

    } else {

     
      newString += string.charAt(i);
    }
  }
  return btoa(unescape(encodeURIComponent(newString)));
}

function decompress(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 compressedString = compress(stringToCompress);
var decompressedString = decompress(compressedString);


console.log("encrypted :",compressedString);
console.log("decrypted :",decompressedString);

Or if you trully want alphanumerical, than you can simply convert it into HEX:

function compress(string) {
  string = unescape(encodeURIComponent(string));
  var newString = '',
    char, nextChar, combinedCharCode;
  for (var i = 0; i < string.length; i += 2) {
    char = string.charCodeAt(i);

    if ((i + 1) < string.length) {

      
      nextChar = string.charCodeAt(i + 1) - 31;

      
      combinedCharCode = char + "" + nextChar.toLocaleString('en', {
        minimumIntegerDigits: 2
      });

      newString += String.fromCharCode(parseInt(combinedCharCode, 10));

    } else {

     
      newString += string.charAt(i);
    }
  }
  return newString.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(4,"0"),"");
}

function decompress(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 compressedString = compress(stringToCompress);
var decompressedString = decompress(compressedString);


console.log("encrypted :",compressedString);
console.log("decrypted :",decompressedString);

Sign up to request clarification or add additional context in comments.

3 Comments

I want it to be similar to this in alphanumeric but the encrypted string must be shorter in length than the actual input string.
I don't believe you'd be able to do so, you can't have both.
Yeah I think its not possible to achieve shorten encryption smaller than the input string. Thanks for your help @vanowm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.