使用简单的 Javascript 加密和解密字符串,无需使用任何外部库

0 javascript encryption algorithm hash

我想创建一个函数来加密字符串,它将字符串缩短为字母数字字符,并创建一个解密函数,它将取回加密的字符串。

\n

这是我参考网上的代码编写的。

\n
function compress(string) {\n  string = unescape(encodeURIComponent(string));\n  var newString = '',\n    char, nextChar, combinedCharCode;\n  for (var i = 0; i < string.length; i += 2) {\n    char = string.charCodeAt(i);\n\n    if ((i + 1) < string.length) {\n\n      \n      nextChar = string.charCodeAt(i + 1) - 31;\n\n      \n      combinedCharCode = char + "" + nextChar.toLocaleString('en', {\n        minimumIntegerDigits: 2\n      });\n\n      newString += String.fromCharCode(parseInt(combinedCharCode, 10));\n\n    } else {\n\n     \n      newString += string.charAt(i);\n    }\n  }\n  return newString;\n}\n\nfunction decompress(string) {\n\n  var newString = '',\n    char, codeStr, firstCharCode, lastCharCode;\n\n  for (var i = 0; i < string.length; i++) {\n    char = string.charCodeAt(i);\n    if (char > 132) {\n      codeStr = char.toString(10);\n\n      firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);\n\n      lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;\n\n      newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);\n    } else {\n      newString += string.charAt(i);\n    }\n  }\n  return newString;\n}\n\nvar stringToCompress = 'awesome';\nvar compressedString = compress(stringToCompress);\nvar decompressedString = decompress(compressedString);\n\n\nconsole.log("encrypted :",compressedString);\nconsole.log("decrypted :",decompressedString);\n
Run Code Online (Sandbox Code Playgroud)\n

目前, sting="awesome" 的输出是

\n
encrypted: \xe2\x98\xbc\xe2\x9f\x88\xe2\xae\xaae\ndecrypted: awesome\n
Run Code Online (Sandbox Code Playgroud)\n

我想要类似的加密,但只能是字母数字值而不是符号。

\n

van*_*owm 6

我不知道你的目标是什么(是让字符串更短,但是是二进制的还是加密的并且在 ascii 范围内),所以如果是后者,那么你可以使用base64编码:

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);
Run Code Online (Sandbox Code Playgroud)

或者,如果您确实想要字母数字,那么您可以简单地将其转换为十六进制:

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);
Run Code Online (Sandbox Code Playgroud)