IDF实验室-简单的js解密

根据加密方法推算解密方法,补全如下

<script>
/**
 * Pseudo md5 hash function
 * @param {string} string
 * @param {string} method The function method, can be 'ENCRYPT' or 'DECRYPT'
 * @return {string}
 */
function pseudoHash(string, method) {
  // Default method is encryption
  if (!('ENCRYPT' == method || 'DECRYPT' == method)) {
    method = 'ENCRYPT';
  }
  // Run algorithm with the right method
  if ('ENCRYPT' == method) {
    // Variable for output string
    var output = '';
    // Algorithm to encrypt
    for (var x = 0, y = string.length, charCode, hexCode; x < y; ++x) {
      charCode = string.charCodeAt(x);
      if (128 > charCode) {
        charCode += 128;
      } else if (127 < charCode) {
        charCode -= 128;
      }
      charCode = 255 - charCode;
      hexCode = charCode.toString(16);
      if (2 > hexCode.length) {
        hexCode = '0' + hexCode;
      }
      
      output += hexCode;
    }
    // Return output
    return output;
  } else if ('DECRYPT' == method) {
      var output = '';
    for(var x=0,y=string.length,charcode,hexcode;x<y;x=x+2){
        hexcode=string.substr(x,2);
        charcode=parseInt(hexcode,16);
        charcode=255-charcode;
        if (charcode<128) {
        charcode += 128;
          }  if (charcode>127) {
        charcode -= 128;
          }
        output+=String.fromCharCode(charcode);
    }
    return output;
    document.write(output);
  }
}
document.write(pseudoHash('461c4a4f461d484e194d471a1b4f4a4b4c4b4f1e1d4d4c491d1a4d19474c1d1b', 'DECRYPT'));
</script>

 

posted @ 2015-06-02 16:18  荆书凝  阅读(605)  评论(0编辑  收藏  举报