---
title: "DES加密解密（JavaScript）"
author: "thirtysix"
date: "2007-09-27T09:38:00Z"
tags: []
slug: "des-javascript"
draft: false
lastmod: "2007-09-27T09:38:00Z"
id: 202076
url: "https://thirtysix.iteye.com/blog/202076"
---

加密

 

//str 是想加密的字符串  
//pwd 是key  
function encrypt(str, pwd) {  
    if (pwd == null || pwd.length &lt;= 0) {  
        alert("Please enter a password with which to encrypt the message.");  
        return null;  
    }  
    var prand = "";  
    for (var i = 0; i &lt; 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 &lt; 2) {  
        alert("Algorithm cannot find a suitable hash. Please choose a different password.  Possible 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 &gt; 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 &lt; str.length; i++) {  
        enc\_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));  
        if (enc\_chr &lt; 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 &lt; 8) {  
        salt = "0" + salt;  
    }  
    enc\_str += salt;  
    return enc\_str;  
}

 解密实现

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)function decrypt(str, pwd) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if (str == null || str.length &lt; 8) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if (pwd == null || pwd.length &lt;= 0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert("Please enter a password with which to decrypt the message.");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var prand = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    for (var i = 0; i &lt; pwd.length; i++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        prand += pwd.charCodeAt(i).toString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var sPos = Math.floor(prand.length / 5);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var incr = Math.round(pwd.length / 2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var modu = Math.pow(2, 31) - 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var salt = parseInt(str.substring(str.length - 8, str.length), 16);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    str = str.substring(0, str.length - 8);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    prand += salt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    while (prand.length &gt; 10) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    prand = (mult * prand + incr) % modu;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var enc\_chr = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var enc\_str = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    for (var i = 0; i &lt; str.length; i += 2) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        enc\_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        enc\_str += String.fromCharCode(enc\_chr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        prand = (mult * prand + incr) % modu;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return enc\_str;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)