des加密实例的javascript实现

des加密实例的javascript实现
des.js
复制代码代码如下所示:

这个软件是和提供的。
任何明示或默示保证,包括但不限于
/ /隐含的适销性或针对特定用途的保证
/ /放弃。在任何情况下,作者或贡献者均不应承担责任。
对于任何直接的、间接的、偶然的、特殊的、示例性的或结果性的
损害赔偿(包括但不限于)替代货物的采购
/或服务;使用损失、数据或利润、业务中断)
然而,无论是在合同中,还是在严格的责任理论中,都是由责任理论引起的。
责任或侵权(包括疏忽或以其他方式产生)
在使用该软件时,即使通知了
此类损坏
这取密钥、消息,以及是否加密或解密。
函数des(键、消息、加密、模式、IV、填充){
本地声明这一点可以加快速度
无功spfunction1 =新的数组(0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x100 0000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
无功spfunction2 =新的阵列0x7fef7fe0(-,- 0x7fff8000,0x8000,0x108020,0x100000,0x20,0x7fefffe0 0x7fff7fe0 -,-,- 0x7fffffe0,0x7fef7fe0 0x7fef8000 -,-,- 0x80000000,0x7fff8000,0x100000,0x20 0x7fefffe0,0x108000,0x100020 -,-,- 0x7fff7fe0,0,0x80000000,0x8000,0x108020 0x7ff00000,0x100020 -,-,- 0x7fffffe0,0,0x108000,0x8020,0x7fef8000 0x7ff00000,0x8020,0,0x108020 -,-,- 0x7fefffe0,0x100000,0x7fff7fe0 0x7ff00000 -,-,- 0x7fef8000,0x8000,0x7ff00000 0x7fff8000,0x20 -,-,- 0x7fef7fe0,0x108020,0x20,0x8000,0x80000000,0x8020 0x7fef8000,0x100000 -,-,- 0x7fffffe0,0x100020,0x7fff7fe0 0x7fffffe0,0x100020,0x108000,0 -,-,- 0x7fff8000,0x8020。- 0x80000000,0x7fefffe0 0x7fef7fe0,0x108000 -,-);
无功spfunction3 =新的数组(0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x800 0000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x802000 8,0x20200);
无功spfunction4 =新的数组(0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802 000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
无功spfunction5 =新的数组(0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0 x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x20800 00,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
无功spfunction6 =新的数组(0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4 010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0 x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
无功spfunction7 =新的数组(0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000 000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
无功spfunction8 =新的数组(0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x4100 0,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);

/ /创造16或48的子项我们需要
VAR键= des_createkeys(关键);
var m = 0,I,J,温度,temp2,right1,对左、右,循环;
无功cbcleft,cbcleft2,cbcright,cbcright2
Var endloop,loopinc;
VaR len = message.length;
var块= 0;
为单个和三个DES设置循环
VaR keys.length = = 323:迭代次数= 9; / /单或三重DES
如果(迭代= 3){循环=加密新数组(0, 32, 2(30):新,数组,- 2,- 2);}
否则{循环=加密新数组(0, 32, 2,62, 30,- 2, 64, 96,2):新数组(94, 62,- 2, 32, 64,2, 30,- 2,- 2);}

根据不同的填充参数填充消息。
如果(填充= 2)消息,则用空格填充消息。
如果(填充= = 1){温度= 8(Len % 8);消息= String.fromCharCode(温度,温度,温度,温度,温度,温度,温度,温度);如果(温度= = 8)len + = 8 / / PKCS7填充;}
否则如果(填充!)消息00 0 0 0 0;用空字节填充该消息。。。。。。。。。0。。。。。。。空白。

在这里存储结果
结果;
tempresult = ;

如果(模式= 1){ CBC / CBC模式
cbcleft =(iv.charcodeat(M + +)<24 |(iv.charcodeat)(M + +)(iv.charcodeat<16)|(M + +)<8)| iv.charcodeat(M + +);
cbcright =(iv.charcodeat(M + +)<24 |(iv.charcodeat)(M + +)(iv.charcodeat<16)|(M + +)<8)| iv.charcodeat(M + +);
m=0;
}

循环传递消息的每个64位块。
当(m < <){
左=(message.charcodeat(M + +)<24 |(消息。charcodeat)(M + +)(message.charcodeat<16)|(M + +)<8)| message.charcodeat(M + +);
右=(message.charcodeat(M + +)<24 |(消息。charcodeat)(M + +)(message.charcodeat<16)|(M + +)<8)| message.charcodeat(M + +);

用于密码块链接模式,将消息与前面的结果异或。
如果(模式= 1),{如果(加密){左^右^ cbcleft;cbcright;}人{ cbcleft2 = cbcleft;cbcright2 = cbcright;cbcleft =左;cbcright =权;} }

/ /每一个64块的信息必须被置换的根据
(温度=(左> > > 4)^右^ 0x0f0f0f0f);正确的温度;左^(温度<4);
(温度=(左> > > 16)^右^ 0x0000FFFF);正确的温度;左^(温度<16);
(温度=(右> > > 2)^左^ 0x33333333);左温度;右^(温度<2);
(温度=(右> > > 8)^左^ 0x00ff00ff);左温度;右^(温度<8);
(温度=(左> > > 1)^右^ 0x55555555);正确的温度;左^(温度<1);

左=((左> 31));
右=((右> 31));

为每一个消息块执行1或3次
对于(j = 0;j <迭代;j=3){
ENDLOOP =循环{ J + 1 };
loopinc =循环{ 2 } J +;
现在继续执行加密或解密。
对于(i =循环};i!!= ENDLOOP;我+ = loopinc){ / /效率
^ right1 =右键{我};
(对=(右> 4)|(右<28)){ 1 }我^键+;
通过通过s选择传递这些字节来获得结果。
温度=左;
左=右;
右=温度(spfunction2 { ^(right1 > > > 24){(0x3f } | spfunction4 right1 > 16)0x3F }
| spfunction6 {(right1 0x3F } { } spfunction8 right1 0x3F | > > > 8)
| spfunction1 {(> > >对24)0x3F } | spfunction3 {(对> 16)0x3F }
| spfunction5 {(0x3f } {对对spfunction7 > > > 8 | 0x3F }));
}
温度=左;左=右;右=温度; / /一个不可逆转的左和右
1个或3个迭代

移动每一个位到右边
(左=(左> 1)|(左< 31));
(右=(右> 1)|(右<31));

/ /现在进行IP-1,即IP在相反的方向
(温度=(左> > > 1)^右^ 0x55555555);正确的温度;左^(温度<1);
(温度=(右> > > 8)^左^ 0x00ff00ff);左温度;右^(温度<8);
(温度=(右> > > 2)^左^ 0x33333333);左温度;右^(温度<2);
(温度=(左> > > 16)^右^ 0x0000FFFF);正确的温度;左^(温度<16);
(温度=(左> > > 4)^右^ 0x0f0f0f0f);正确的温度;左^(温度<4);

用于密码块链接模式,将消息与前面的结果异或。
如果(模式= 1),{如果(加密){ cbcleft =左;cbcright =权;} {左右^ ^其他cbcleft2;cbcright2;} }
tempresult =(String.fromCharCode(左> > > 24),(0xFF(左> > > 16)),((左> > > 8)0xff(左),0xFF),(右> > > 24),(0xFF(右> > > 16)),((右> > > 8)0xff)(右,0xFF));

块= 8;
如果(块= = 512){结果= tempresult;tempresult =;块= 0;}
为每个} 8个字符,或消息中的64位

以数组的形式返回结果
返回结果+ tempresult;
DES的结尾
/ / des_createkeys
这需要输入64位密钥(即使只有56位)。
作为2个整数的数组,并返回1648位密钥。
功能des_createkeys(关键){
在本地声明这一点可以加快速度。
pc2bytes0 =新的数组(0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204)。
pc2bytes1 =新的数组(0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101)。
pc2bytes2 =新的数组(0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
pc2bytes3 =新的数组(0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000)。
pc2bytes4 =新的数组(0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
pc2bytes5 =新的数组(0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
pc2bytes6 =新的数组(0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002)。
pc2bytes7 =新的数组(0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800)。
pc2bytes8 =新的数组(0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
pc2bytes9 =新的数组(0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
pc2bytes10 =新的数组(0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
pc2bytes11 =新的数组(0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200)。
pc2bytes12 =新的数组(0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010)。
pc2bytes13 =新的数组(0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);

有多少次迭代(1用于DES,3用于三次DES)
VaR的迭代key.length > 83 = 1; / / /保罗16变6 / 2007使用三重DES
存储返回键
var =新数组(32×迭代);
现在定义需要完成的左移
var =新数组(0, 0, 1、1, 1, 1、1, 1, 0、1, 1, 1、1, 1等);
其他变量
无功lefttemp,righttemp,m = 0,n = 0,温度;

对于(var j = 0;j <迭代;j + +)1或3次迭代
左=(key.charcodeat(M + +)<24 |(关键。charcodeat)(M + +)(key.charcodeat<16)|(M + +)<8)| key.charcodeat(M + +);
右=(key.charcodeat(M + +)<24 |(关键。charcodeat)(M + +)(key.charcodeat<16)|(M + +)<8)| key.charcodeat(M + +);

(温度=(左> > > 4)^右^ 0x0f0f0f0f);正确的温度;左^(温度<4);
(温度=(右> 16)^左^ 0x0000FFFF);左温度;右^(温度<16);
(温度=(左> > > 2)^右^ 0x33333333);正确的温度;左^(温度<2);
(温度=(右> 16)^左^ 0x0000FFFF);左温度;右^(温度<16);
(温度=(左> > > 1)^右^ 0x55555555);正确的温度;左^(温度<1);
(温度=(右> > > 8)^左^ 0x00ff00ff);左温度;右^(温度<8);
(温度=(左> > > 1)^右^ 0x55555555);正确的温度;左^(温度<1);

/右边需要移动并获得最后一个。
温度=(左> 20)0x000000f0);
左/左需要颠倒。
向左(右> 8)(0xff00)|(右> 24)0xf0);
右=温度;

现在转到左、右执行这些移位。
对于(var i = 0;i < shifts.length;i++){
将键移到左边一个或两个位。
如果(偏移{ }){左=(左> 26);右=(右> 26);}
否则{左=(左> 27);右=(右> 27);}
左= - 0xf;右= - 0xf;

/ /现在适用的模型,在这样一种方式,E的时候更容易
/ /这种转换会像PC-2只有最后6位
与48个连续位相比,行的顺序将根据
/ /如何选择功能将应用:S2、S4、S6、S8。
lefttemp = pc2bytes0 {左28 } pc2bytes1 {(左| > > > > > > 24)0xf }
| pc2bytes2 {(左> > > 20)0xf } | pc2bytes3 {(左> 16)0xf }
| pc2bytes4 {(左> > > 12)0xf } | pc2bytes5 {(左> 8)0xf }
| pc2bytes6 {(左> 4)0xf };
righttemp = { 28 } { pc2bytes7权pc2bytes8(右| > > > > > > 24)0xf }
| pc2bytes9 {(右> > > 20)0xf } | pc2bytes10 {(右> 16)0xf }
| pc2bytes11 {(右> > > 12)0xf } | pc2bytes12 {(右> 8)0xf }
| pc2bytes13 {(右> 4)0xf };
(温度=(righttemp > > > 16)^ lefttemp)0x0000FFFF;
钥匙{ N + } = lefttemp ^温度;键{ } = n + righttemp ^(温度<16);
}
对于每个迭代}
返回我们创建的密钥
返回键;
对des_createkeys / /结束}
与之相关的是:
功能stringtohex(s){
VAR r = 0x;
VaR的魔法=新的数组(0
对于(var i = 0;我> 4 } +魔法{ s.charcodeat(我)0xf };}
返回R;
}

功能hextostring(H){
var;
对于(var i =(h.substr(0, 2)= = 0x)2:0;我< h.length;我+ = 2 { R(parseInt)= String.fromCharCode(h.substr(I,2),16));}
返回R;
}

这是一个24字节的键!;
var消息这是一个测试消息;
var密文= des(密钥,消息,1, 0);
document.writeln(DES测试:+ stringtohex(密文));

使用方法
复制代码代码如下所示:
这是一个24字节的键!;
var消息这是一个测试消息;
var密文= des(密钥,消息,1, 0);
document.writeln(DES测试:+ stringtohex(密文));

使用单一DES加密(在CBC模式下使用8字节密钥)
/ /与给定的输入矢量和PKCS7填充
DES(8bytekey
使用三重DES加密(在欧洲央行模式下使用24字节密钥)
des()这是一个24字节的键!!!
在欧洲央行模式下使用单一DES解密
DES(8bytekey

des加密算法在发送用户信息时对其进行加密,非常实用。
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部