• 采用密码加密的javascript字符串加密、解密程序


    本程序原取自网络,在加密字符串时可以采用设置密码,密文必须使用相同密码采用解密出明文,否则返回的将会是乱码。
    原文仅可以加密解密E文,密码也只能采用E文,偶稍加修改后,可以加解密中文,密码也可以采用中文密码,比较实用。
    如果不设置密码,则采用默认密码"1234"进行加解密。

    以下是修改后的程序代码:

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    
    function Encrypt(str, pwd) {
        if(str=="")return "";
        str = escape(str);
        if(!pwd || pwd==""){ var pwd="1234"; }
        pwd = escape(pwd);
          if(pwd == null || pwd.length <= 0) {
            alert("Please enter a password with which to encrypt the message.");
              return null;
          }
          var prand = "";
          for(var I=0; I<pwd.length; I++) {
            prand += pwd.charCodeAt(I).toString();
          }
          var sPos = Math.floor(prand.length / 5);
          var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
    
          var incr = Math.ceil(pwd.length / 2);
          var modu = Math.pow(2, 31) - 1;
          if(mult < 2) {
            alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
            return null;
          }
          var salt = Math.round(Math.random() * 1000000000) % 100000000;
          prand += salt;
          while(prand.length > 10) {
            prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
          }
          prand = (mult * prand + incr) % modu;
        var enc_chr = "";
        var enc_str = "";
        for(var I=0; I<str.length; I++) {
            enc_chr = parseInt(str.charCodeAt(I) ^ Math.floor((prand / modu) * 255));
            if(enc_chr < 16) {
                enc_str += "0" + enc_chr.toString(16);
            }else 
                enc_str += enc_chr.toString(16);
            prand = (mult * prand + incr) % modu;
        }
          salt = salt.toString(16);
          while(salt.length < 8)salt = "0" + salt;
        enc_str += salt;
        return enc_str;
    }
    
    function Decrypt(str, pwd) {
        if(str=="")return "";
        if(!pwd || pwd==""){ var pwd="1234"; }
        pwd = escape(pwd);
          if(str == null || str.length < 8) {
            alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
            return;
          }
          if(pwd == null || pwd.length <= 0) {
            alert("Please enter a password with which to decrypt the message.");
            return;
          }
          var prand = "";
          for(var I=0; I<pwd.length; I++) {
            prand += pwd.charCodeAt(I).toString();
          }
          var sPos = Math.floor(prand.length / 5);
          var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
          var incr = Math.round(pwd.length / 2);
          var modu = Math.pow(2, 31) - 1;
          var salt = parseInt(str.substring(str.length - 8, str.length), 16);
          str = str.substring(0, str.length - 8);
          prand += salt;
          while(prand.length > 10) {
            prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
          }
          prand = (mult * prand + incr) % modu;
          var enc_chr = "";
          var enc_str = "";
        for(var I=0; I<str.length; I+=2) {
            enc_chr = parseInt(parseInt(str.substring(I, I+2), 16) ^ Math.floor((prand / modu) * 255));
            enc_str += String.fromCharCode(enc_chr);
            prand = (mult * prand + incr) % modu;
        }
        return unescape(enc_str);
    }
    //  End -->
    </script>
    
  • 相关阅读:
    android的布局xml文件如何添加注释?
    安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?
    Failure [INSTALL_FAILED_OLDER_SDK]
    安装并使用PICT,生成测试用例
    安装并使用Junit
    SourceMonitor的安装
    FindBugs
    pdm的说明
    Checkstyle的安装与使用
    白话决策树
  • 原文地址:https://www.cnblogs.com/suzh/p/1932629.html
Copyright © 2020-2023  润新知