JS标准DES加解密

 

 

Javascript写的DES算法,目前只能使用8位的密钥.

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>DES算法-Code by 梅雪香</title>
<style type="text/css">
textarea
{600px; height:150px;}
body
{font-size:12px}
input
{font-size:12px}
</style>
<script language="JavaScript">
<!--
var DES = {
    
// initial permutation IP
    IP_Table : [
        
585042342618102605244362820124,
        
625446383022146645648403224168,
        
574941332517,  91595143352719113,
        
615345372921135635547393123157
    ],
    
// final permutation IP^-1 
    IPR_Table :  [
        
408481656246432397471555236331,
        
386461454226230375451353216129,
        
364441252206028353431151195927,
        
34242105018582633141,  949175725
    ],
    
// permuted choice table (key) 
    PC1_Table :  [
        
574941332517,  9,  1585042342618,
        
10,  259514335271911,  360524436,
        
63554739312315,  7625446383022,
        
14,  661534537292113,  5282012,  4
    ],
    
// permuted choice key (table) 
    PC2_Table : [
        
14171124,  1,  5,  32815,  62110,
        
231912,  426,  816,  7272013,  2,
        
415231374755304051453348,
        
444939563453464250362932
    ],
    
// number left rotations of pc1 
    LOOP_Table : [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1],
    
// expansion operation matrix
    E_Table :  [
        
32,  1,  2,  3,  4,  5,  4,  5,  6,  7,  8,  9,
         
8,  910111213121314151617,
        
161718192021202122232425,
        
2425262728292829303132,  1
    ],
    
// 32-bit permutation function P used on the output of the S-boxes 
    P_Table : [    1672021291228171,  1523265,  183110,
                     
2,  8241432273,  9,  1913306,  22114,  25 ],

    
// The (in)famous S-boxes 
    S_Box : [
        
// S1 
        [[14,  413,  1,  21511,  8,  310,  612,  5,  9,  0,  7],
         [ 
015,  7,  414,  213,  110,  61211,  9,  5,  3,  8],
         [ 
4,  114,  813,  6,  2111512,  9,  7,  310,  5,  0],
         [
1512,  8,  2,  4,  9,  1,  7,  511,  31410,  0,  613]],
        
// S2 
        [[15,  1,  814,  611,  3,  4,  9,  7,  21312,  0,  510],
         [ 
313,  4,  715,  2,  81412,  0,  110,  6,  911,  5],
         [ 
014,  71110,  413,  1,  5,  812,  6,  9,  3,  215],
         [
13,  810,  1,  315,  4,  211,  6,  712,  0,  514,  9]],
        
// S3 
        [[10,  0,  914,  6,  315,  5,  11312,  711,  4,  2,  8],
         [
13,  7,  0,  9,  3,  4,  610,  2,  8,  514121115,  1],
         [
13,  6,  4,  9,  815,  3,  011,  1,  212,  51014,  7],
         [ 
11013,  0,  6,  9,  8,  7,  41514,  311,  5,  212]],
        
// S4 
        [[ 71314,  3,  0,  6,  910,  1,  2,  8,  51112,  415],
         [
13,  811,  5,  615,  0,  3,  4,  7,  212,  11014,  9],
         [
10,  6,  9,  01211,  71315,  1,  314,  5,  2,  8,  4],
         [ 
315,  0,  610,  113,  8,  9,  4,  51112,  7,  214]],
        
// S5 
        [[ 212,  4,  1,  71011,  6,  8,  5,  31513,  014,  9],
         [
1411,  212,  4,  713,  1,  5,  01510,  3,  9,  8,  6],
         [ 
4,  2,  1111013,  7,  815,  912,  5,  6,  3,  014],
         [
11,  812,  7,  114,  213,  615,  0,  910,  4,  5,  3]],
        
// S6 
        [[12,  11015,  9,  2,  6,  8,  013,  3,  414,  7,  511],
         [
1015,  4,  2,  712,  9,  5,  6,  11314,  011,  3,  8],
         [ 
91415,  5,  2,  812,  3,  7,  0,  410,  11311,  6],
         [ 
4,  3,  212,  9,  515101114,  1,  7,  6,  0,  813]],
        
// S7 
        [[ 411,  21415,  0,  813,  312,  9,  7,  510,  6,  1],
         [
13,  011,  7,  4,  9,  11014,  3,  512,  215,  8,  6],
         [ 
1,  4111312,  3,  7141015,  6,  8,  0,  5,  9,  2],
         [ 
61113,  8,  1,  410,  7,  9,  5,  01514,  2,  312]],
        
// S8 
        [[13,  2,  8,  4,  61511,  110,  9,  314,  5,  012,  7],
         [ 
11513,  810,  3,  7,  412,  5,  611,  014,  9,  2],
         [ 
711,  4,  1,  91214,  2,  0,  6101315,  3,  5,  8],
         [ 
2,  114,  7,  410,  8131512,  9,  0,  3,  5,  611]]
    ],
    Oct2Bin : [
"0000","0001","0010","0011","0100","0101","0110","0111",
               
"1000","1001","1010","1011","1100","1101","1110","1111" ],
    
//str 为八位的字符
    subKeys : new Array(16),
    key : 
"",
    text : 
""
}
;

DES.init 
= function(key,text){
    
if(key != this.key){
        
this.key=key; 
        
this.GenSubKey();
    }

    
this.text= text + "        ".substring(0,parseInt("07654321".charAt( text.length%8 ),10));
}
;

DES.GenSubKey 
= function(){
    
var arr = this.Permute( this.Byte2Bit(this.key) , this.PC1_Table);
    
var AL = arr.slice(0,28);
    
var AR = arr.slice(28,56);
    
for(var i=0; i<16; i++{
        
for(var j=0,k=this.LOOP_Table[i];j<k;j++){
            AL.push(AL.shift());
            AR.push(AR.shift());
        }

        
this.subKeys[i] = this.Permute( AL.concat(AR) , this.PC2_Table );
    }

}
;
//数组ar存储二进制数据,该函数将从头开始的每八位转换成对应的字符
DES.Bit2Byte = function(ar,fCh){
    
var str="";
    
if(fCh == "byte"){
        
var tmpAr = ar.join("").match(/.{8}/g);
        
for(var i=0,j=tmpAr.length; i<j; i++)
            str 
+= String.fromCharCode(parseInt(tmpAr[i],2));
     }

    
else if(fCh == "hex"){
        
var tmpAr = ar.join("").match(/.{4}/g);
        
for(var i=0,j=tmpAr.length; i<j; i++)
            str 
+= "0123456789abcdef".charAt(parseInt(tmpAr[i],2));
    }

    
else return "Error:Second param is wrong.";
    
return str;
}
;
//将字符串转换成二进制数组
DES.Byte2Bit = function(str){
    
for(var i=0,j=8*str.length,ar = [],ch=""; i<j; i++){
        
var k = 7 - i%8;
        
if(k == 7) ch = str.charCodeAt(parseInt(i/8,10));
        ar.push( (ch 
>> k) & 1 );
    }

    
return ar;
}
;

//将16进制字符串转变成二进制数组
DES.Hex2Bin = function(str){
    
for(var i=0,j=str.length,s=""; i<j; i++)
        s 
+= this.Oct2Bin[ parseInt(str.charAt(i),16)];
    
return s.split("");
}
;

DES.Xor 
= function(A,B){
    
for(var i=0,j=B.length,rtn=new Array(j); i<j; i++)
        rtn[i] 
= A[i] ^ B[i];
    
return rtn;
}


DES.Permute 
= function(ar,tb){
    
for(var i=0,j=tb.length,rtn=new Array(j);i<j;i++)
        rtn[i] 
= ar[tb[i]-1];
    
return rtn;
}
;

DES.F_func 
= function(Ri,Ki)
{
    
return this.Permute( this.S_func( this.Xor( this.Permute( Ri , this.E_Table ) , Ki) ) , this.P_Table);
}
;
//ar为输入48位串数组
DES.S_func = function(ar){
    
for(var i=0,arRtn = [];i<8;i++){
        
var x = i*6;
        
var j = parseInt(""+ar[x]+ar[x+5],2);
        
var k = parseInt(ar.slice(x+1,x+5).join(""),2);
        arRtn 
= arRtn.concat( this.Oct2Bin[ this.S_Box[i][j][k] ].split("") );
    }

    
return arRtn;
}


//mode参数为处理模式."Encrypt":加密 "Decrypt":解密,默认为加密
DES.Encrypt = function(mode){
    mode 
= mode?mode:"Encrypt";
    
if(mode=="Decrypt")
        
var plainTextAr = this.Hex2Bin(this.text).join("").match(/.{64}/g);
    
else
        
var plainTextAr = this.Byte2Bit(this.text).join("").match(/.{64}/g);
    
for(var i=0,j=plainTextAr.length;i<j;i++){
        
var arr = this.Permute(plainTextAr[i].split(""),this.IP_Table)
        
var AL = arr.slice(0,32);
        
var AR = arr.slice(32,64);
        
if(mode == "Decrypt"){
            
for(var k=15;k>-1;k--){
                
var tmp = AR.slice(0,32);
                AR 
= this.Xor(this.F_func(AR,this.subKeys[k]) , AL);
                AL 
= tmp;
            }

        }

        
else{
            
for(var k=0;k<16;k++){
                
var tmp = AR.slice(0,32);
                AR 
= this.Xor(this.F_func(AR,this.subKeys[k]) , AL);
                AL 
= tmp;
            }

        }

        plainTextAr[i] 
= this.Bit2Byte(this.Permute(AR.concat(AL), this.IPR_Table),(mode=="Decrypt"?"byte":"hex"));
    }

    
return plainTextAr.join("").trim();
}

String.prototype.trim 
= function()return this.replace(/\s+$/g,"");}
//-->
</script>

<script language="JavaScript">
<!--
var $ = document.getElementById;
function jiami(){
   DES.init($(
"txtKey").value,$("taText").value );
   $(
"taCipher").value = DES.Encrypt();
}

function jiemi(){
   DES.init($(
"txtKey").value,$("taCipher").value );
   $(
"taText").value = DES.Encrypt("Decrypt");
}

//-->
</script>
</head>

<body>
请输入加(解)密密钥:
<input id="txtKey" type="text" onfocus="this.select()" value="47944980" size="10">
<font color="red">(*目前密钥只能是八位)</font><br>

未加密文本:
<br>
<textarea id="taText">Hi,I'm meixuexiang.</textarea>
<input type="button" value="clear" onclick="document.all[this.sourceIndex-1].value=''"><br>
<input type="button" value="Encrypt" onclick="jiami()">&nbsp;&nbsp;&nbsp;
<input type="button" value="Decrypt" onclick="jiemi()"><br><br>
加密后文本:
<br>
<textarea id="taCipher"></textarea>
<input type="button" value="clear" onclick="document.all[this.sourceIndex-1].value=''"><br>
<p>code by meixx(<a href="http://www.blogjava.net/mxx">梅雪香</a>2006-10-18 23:00 <a href="mailto:wy_hd@163.com">信息反馈</a></p>
</body>
</html>

http://www.blogjava.net/mxx/archive/2006/10/19/DES_in_Javascript.html

原文地址:https://www.cnblogs.com/drolepeng/p/1646332.html