一个JavaScript(伪)加密解密的脚本

前些天看到邮件登录的程序,密码用明文传递,感觉太过危险,所以写了这个加密解密的脚本,当然,这个脚本能做的事情有限,但可以降低一些风险。其中,字典(DICTIONAY)和密码长度(DIGIT)可以自由设定,但密码长度不能超过64位且字典中的最后两位不能出现在密码中。


<script type="text/javascript">
var DICTIONARY = new Array("a", "b", "M", "L", "c", "d", "K", "J", "e", "I", "f", "g", "H", "T", "h", "F",
                           "i", "j", "E", "D", "k", "l", "B", "C", "m", "A", "n", "o", "Z", "Y", "p", "X",
                           "q", "r", "W", "V", "s", "t", "U", "G", "u", "S", "v", "w", "R", "Q", "x", "P",
                           "y", "z", "O", "N", "9", "5", "7", "3", "1", "0", "2", "6", "4", "8", "#", "%");
var DIGIT = 24;

function encrypt(value) {
    var password = null;
    if (value != null && value != "") {
        password = "";
        for (var i=0; i<value.length; i++)
            password += divide(value.charAt(i), true);
        password = converge(password, true);
    }
    return password;
}

function unencrypt(value) {
    var password = null;
    if (value != null && value != "") {
        password = "";
        value = converge(value, false);
        for (var i=0; i<value.length; i++)
            password += divide(value.charAt(i), false);
    }
    return password;
}

function divide(value, mode) {
    var password = null;   
    if (value != null && value != "") {
        password = value;
        if (DICTIONARY != null && DICTIONARY.length > 0) {
            for (var i=0; i<DICTIONARY.length; i++) {
                if (value == DICTIONARY[i]) {
                    password = (mode)?DICTIONARY[i+2]:DICTIONARY[i-2];
                    break;
                }
            }
        }           
    }
    return password;
}

function converge(value, mode) {
    var password = null;
    var number = new Array(DIGIT);
    for (var i=0; i<DIGIT; i++)
        number[i] = i;
    if (value != null && value != "") {
        password = value;
        if (DICTIONARY != null && number != null) {
            var length = value.length;
            if (DIGIT - length < 0) {   
                if (DIGIT - length == -1) {
                    if (!mode && value.lastIndexOf(DICTIONARY[0]) != -1)
                        password = password.substring(0, DIGIT);
                }
            } else if (DIGIT - length == 0) {
                if (mode) {
                    password += DICTIONARY[0];
                } else {
                    var location;
                    var terminal = value.substring(DIGIT-1, DIGIT);
                    for (var i=0; i<DICTIONARY.length; i++) {
                        if (DICTIONARY[i] == terminal) {
                            location = i;
                            break;
                        }
                    }
                    length = number[location];
                    password = password.substring(length, DIGIT-1);
                }
            } else {
                if (mode) {
                    var location;
                    var fill = "";
                    length = DIGIT - length - 1;
                    for (var i=0; i<length; i++)                          
                        fill += DICTIONARY[random()+random()];                    
                    for (var i=0; i<number.length; i++) {
                        if (number[i] == length) {
                            location = i;
                            break;
                        }
                    }
                    password = fill + password + DICTIONARY[location];                  
                }
            }
        }
    }
    return password;
}

function random() {
    var digit = 0;
    var random = Math.random().toString();
    var index = random.indexOf(".");
    if (index != -1 && (index+2) <= random.length)
        digit = parseInt(random.substring(index+1, index+2));
    digit = isNaN(digit)?0:digit;
    return digit;
}
</script>


<input type="text" name="encrypt" size="30">&nbsp;<input type="button" value="&nbsp;加密&nbsp;" onclick="document.all.unencrypt.value = encrypt(document.all.encrypt.value)" onfocus="this.blur()"><br/>
<input type="text" name="unencrypt" size="30">&nbsp;<input type="button" value="&nbsp;解密&nbsp;" onclick="alert(unencrypt(document.all.unencrypt.value))" onfocus="this.blur()"><br/>

posted @ 2005-07-19 15:43  吴铁衣  阅读(230)  评论(0编辑  收藏  举报