JavaScript implementation DES encryption algorithm.

zhaozj2021-02-12  159

Reprinted: http://www.shopable.co.uk/des.html

// JavaScript Version

// Paul Tero, July 2001

//Http://www.shopable.co.uk/des.html

//

// Optimised for Performance with Large Blocks by Michael Hayworth, November 2001

//Http://www.netdealing.com

//

// this Software Is Provided "As IS" and

// any express or implied warranties, incruhed, but not limited to, the

// Implied Warranties of Merchantability and FITNESS for a Particular Purpose

// Are Disclaimed. In no Event shall the author or concibutors be Liable

// for Any Direct, Indirect, Incidental, Special, Exemplary, or Consequential

// DAMAGES (INCLUDING, But Not Limited to, Procurement of Substitute Goods

// Or Services; Loss of Use, Data, or Profits; Or Business Interruption

//HoWever Caused and on Any THEORY OF LIABILITY, WHETHER in Contract, STRICT

// Liability, or otherwise ARISING INY WAY

// out of the use of this software, Even if advised of the Possibility of

// Such Damage.

// DES

// this Takes the key, the message, and whather to encrypt or Decrypt

Function DES (Key, Message, Encrypt, Mode, IV) {

// Declaring this locally speseds strings up a bit

var spfunction1 = new Array (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,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404, 0X10404, 0X1010404, 0X1000404, 0X1000004, 0X404, 0X10404, 0X1010400, 0X404, 0X1000400, 0X1000400, 0, 0X10004, 0X10400, 0, 0X1010004);

var spfunction2 = new Array (-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); var spfunction3 = new Array (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,0x8000000,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,0x8020008,0x20200);

var spfunction4 = new Array (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,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081, 0x80, 0X1, 0X2000, 0X800001, 0X2001, 0X802080, 0X800081, 0X2001, 0X2080, 0X800000, 0X802001, 0X80, 0X800000, 0X2000, 0X802080);

var spfunction5 = new Array (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,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000, 0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100); var spfunction6 = new Array (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,0x4010,0x404000 0x20404000, 0X20000000, 0X20004000, 0X10, 0X20400010, 0X404000, 0X20404 010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000, 0x20000000, 0X400010, 0X20004010);

var spfunction7 = new Array (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,0x4000000,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); var spfunction8 = new Array (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, 0X10041000, 0X1000, 0X40, 0X10040040, 0X1000, 0X41040, 0X100040, 0X40, 0X10000040, 0X100400000000, 0X10040040, 0X1000000 0,0X40000, 0X10001040, 0X10040, 0X10000040, 0X10040000, 0X10001000, 0X10001040, 0, 0X10041040, 0X41000, 0X41000, 0X1040, 0X1040, 0X40040, 0X10000000, 0X10041000);

// Create the 16 or 48 Subkeys WE WILL NEED

Var keys = des_createKeys (key);

VAR M = 0, I, J, TEMP, TEMP2, RIGHT1, RIGHT2, LEFT, RIGHT, LOOPING

VAR CBCLEFT, CBCLEFT2, CBCRIGHT, CBCRIGHT2

Var endloop, loopinc;

Var len = message.length;

Var chunk = 0;

// set up the loops for salesle and triple des

Var ity = keys.length == 32? 3: 9; // Single or triple des

IF (Iterations == 3) {looping = encrypt? New Array (0, 32, 2): New Array (30, -2, -2);

Else {looping = encrypt? New Array (0, 32, 2, 62, 30, ---2, 64, 96, 2): New Array (94, 62, -2, 32, 64, 2, 30, -2, -2);} Message = "/ 0/0/0/0/0/0/0/0"; // Pad The Message Out with Null Bytes

// Store the result Here

Result = "";

TempResult = ""

IF (Mode == 1) {// CBC MODE

CBCLEFT = (Iv.Charcodeat (M ) << 24) | (iv.charcodeat (m ) << 16) | (iv.charcodeat (m ) << 8) | iv.charcodeat (m );

Cbcright = (iv.charcodeat (m ) << 24) | (iv.charcodeat (m ) << 16) | (iv.charcodeat (m ) << 8) | iv.charcodeat (m );

m = 0;

}

// loop through each 64 bit chunk of the message

While (m

Left = (M ) << 24) | (Message.Charcodet (M ) << 16) | (Message.Charcodet (M ) << 8) | Message.Charcodeat (M );

Right = (Message.Charcodeat << 24) | (Message.Charcodet (M ) << 16) | (Message.Charcode (M ) << 8) | Message.Charcodeat (M );

// for cipher block chaining mode, xor the message with the previous result

IF (Mode == 1) {if (encrypt) {left ^ = cbcleft; Right ^ = cbcright;} else {cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right;}}}

// First Each 64 But Chunk of The Message Must Be Permute According To IP

Temp = ((Left >>> 4) ^ Right) & 0x

0F

0F

0F

0F

rT ^ = Temp; Left ^ = (Temp << 4);

Temp = ((LEFT >>> 16) ^ Right) & 0x0000FFFF; Right ^ = Temp; Left ^ = (Temp << 16);

Temp = ((Right >>> 2) ^ LEFT) & 0x33333333; Left ^ = Temp; Right ^ = (Temp << 2); Temp = ((Right >>> 8) ^ LEFT) & 0x00FF00FF; left ^ = Temp; Right ^ = (TEMP << 8);

Temp = ((LEFT >>> 1) ^ Right) & 0x5555555; Right ^ = Temp; Left ^ = (Temp << 1);

Left = ((Left << 1) | (LEFT >>> 31));

Right = ((Right << 1) | (Right >>> 31));

// do this Either 1 or 3 Times for Each Chunk of the Message

For (j = 0; j

Endloop = looping [J 1];

Loopinc = looping [J 2];

// Now Go Through and Perform The Encryption Or Decryption

For (i = looping [j]; i! = endloop; i = loopinc) {// for efficiency

Right1 = Right ^ Keys [i];

Right2 = ((Right >>> 4) | (Right << 28)) ^ Keys [i 1];

// The result is attained by passing these bytes through the s selection functions

TEMP = Left;

LEFT = Right;

Right = Temp ^ (SPFunction2 [(Right1 >>> 24) & 0x

3F

] | SPFUNCTION4 [(Right1 >>> 16) & 0x

3F

]

| SPFUNCTION6 [(Right1 >>> 8) & 0x

3F

] | SPFUNCTION8 [Right1 & 0x

3F

]

| SPFUNCTION1 [(Right2 >>> 24) & 0x

3F

] | SPFUNCTION3 [(Right2 >>> 16) & 0x

3F

]

| SPFUNCTION5 [(Right2 >>> 8) & 0x

3F

] | SPFUNCTION7 [Right2 & 0x

3F

]);

}

Temp = left; left = right; right = Temp; // unreverse left and right

} // for Either 1 or 3 Items

//move dam one one bit to the right

Left = ((Left >>> 1) | (Left << 31));

Right = ((Right >>> 1) | (Right << 31));

// Now Perform IP-1, WHICH IS IP in the oppositive directionTemp = ((Left >>> 1) ^ Right) & 0x55555555; Right ^ = Temp; Left = (Temp << 1);

Temp = ((Right >>> 8) ^ left) & 0x00FF00FF; LEFT ^ = Temp; Right ^ = (Temp << 8);

Temp = ((Right >>> 2) ^ left) & 0x33333333; Left ^ = Temp; Right ^ = (Temp << 2);

Temp = ((LEFT >>> 16) ^ Right) & 0x0000FFFF; Right ^ = Temp; Left ^ = (Temp << 16);

Temp = ((Left >>> 4) ^ Right) & 0x

0F

0F

0F

0F

rT ^ = Temp; Left ^ = (Temp << 4);

// for cipher block chaining mode, xor the message with the previous result

IF (Mode == 1) {if (encrypt) {cbcleft = left; cbcright = right;} else {left ^ = cbcleft2; Right ^ = cbcright2;}}

Tempresult = String.Fromcharcode ((Left >>> 24), ((LEFT >>> 16) & 0xFF), ((LEFT >>> 8) & 0xFF), (Left & 0xFF), (Right >>> 24) ((Right >>> 16) & 0xFF), ((Right >>> 8) & 0xFF), (Right & 0xFF));

CHUNK = 8;

IF (chunk == 512) {result = TempResult; TempResult = ""; chunk = 0;

} // for Every 8 Characters, or 64 Bits in The Message

// Return The Result As an Array

Return Result TempResult;

} // end of des

// des_createKeys

// this Takes As INPUT A 64 Bit Key (Even Though Only 56 Bits Are Used)

// as an array of 2 integers, and returns 16 48 bit keys

Function des_createKeys (key) {

// Declaring this locally speseds strings up a bit

pc2bytes0 = new Array (0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204); pc2bytes1 = new Array (0,0x1,0x100000,0x100001 0x4000000, 0X4000001, 0X4100000, 0X4100001, 0X100, 0X101, 0X100100, 0X100101, 0X4000100, 0X4000101, 0X4100100, 0X4100101);

PC2Bytes2 = New Array (0x8, 0x800, 0x808, 0X1000000, 0X1000008, 0X1000800, 0X1000808, 0X8000000, 0X808, 0X1000000, 0X1000008, 0X1000800, 0X1000808);

PC2BYTES3 = New Array (0x200000, 0X800000000, 0X202000, 0X8002000, 0X8202000, 0X20000, 0X2220000, 0X22000, 0X222000, 0X802000, 0X222000, 0X802000, 0X8222000);

PC2BYTES4 = New Array (0,0X40000, 0X10, 0X40010, 0, 0X40000, 0X10, 0X40010, 0X1000, 0X41000, 0X1010, 0X41010, 0X1000, 0X41000, 0X1010, 0X41010);

PC2BYTES5 = New Array (0,0X400, 0x20, 0X420, 0, 0X2000000, 0X2000400, 0X2000020, 0X2000400, 0X2000000, 0X2000400, 0X2000020, 0X2000420);

PC2Bytes6 = New Array (0x10000000, 0x800000000000000002, 0X80002, 0X10080002, 0, 0X10000000, 0X80000, 0X10080000, 0X2, 0X10000002, 0X80002, 0X10080002);

PC2BYTES7 = New Array (0,0x10000, 0x800,000, 0x20010000, 0x20000800, 0X20010800, 0X20000, 0X30000, 0X20800, 0X308000000, 0X200000000, 0X20030800);

PC2Bytes8 = New Array (0x40000, 0, 0X40000, 0X2, 0X40002, 0X2, 0X40002, 0X200000000, 0X2040000, 0X2000002, 0X2040002, 0X2000002, 0X2040002);

PC2Bytes9 = new Array (0x10000000, 0X8, 0X10000008, 0X10000000, 0X8, 0X10000400, 0X408, 0X10000408, 0X400, 0X10000400, 0X408, 0X10000408);

PC2bytes10 = New Array (0,0x20,0,0x20,0 x100000, 0 x1000 20 x 2000, 0x2020, 0x2000, 0X20202020, 0X102000, 0X102020, 0X102000, 0X102020);

pc2bytes11 = new Array (0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200); pc2bytes12 = new Array (0,0x1000,0x8000000,0x8001000 0x80000, 0x81000, 0X8080000, 0X8080, 0X8000010, 0X8001010, 0X80010, 0X81010, 0X8080010, 0X8081010);

PC2bytes13 = New Array (0,0X4, 0X100, 0X104, 0, 0X4, 0X100, 0X104, 0X1, 0X1, 0X101, 0X105, 0X1, 0X5, 0X101, 0X105);

// How Many Items (1 for des, 3 for triple "

Var ity> = 24? 3: 1;

// Stores the return keys

Var keys = new array (32 * ity);

// now define the Left Shifts Which Need to Be Done

Var Shifts = New Array (0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);

// Other variables

Var LeftTemp, RightTemp, M = 0, N = 0, TEMP;

For (var j = 0; j

Left = (Key.Charcodeat << 24) | (Key.Charcodeat (M ) << 16) | (Key.Charcodeat (M ) << 8) | key.charcodeat (m );

Right = (key.charcodeat (m ) << 24) | (Key.Charcodeat (M ) << 16) | (Key.Charcodeat (M ) << 8) | Key.Charcodeat (M );

Temp = ((Left >>> 4) ^ Right) & 0x

0F

0F

0F

0F

rT ^ = Temp; Left ^ = (Temp << 4);

Temp = ((Right >>> -16) ^ left) & 0x0000FFFF; LEFT ^ = Temp; Right ^ = (TEMP << -16);

Temp = ((LEFT >>> 2) ^ Right) & 0x33333333; Right ^ = Temp; Left ^ = (Temp << 2);

Temp = ((Right >>> -16) ^ left) & 0x0000FFFF; LEFT ^ = Temp; Right ^ = (TEMP << -16);

Temp = ((LEFT >>> 1) ^ Right) & 0x55555555; Right ^ = Temp; Left ^ = (Temp << 1); Temp = ((Right >>> 8) ^ LEFT) & 0x00FF00FF; left ^ = Temp; Right ^ = (TEMP << 8);

Temp = ((LEFT >>> 1) ^ Right) & 0x5555555; Right ^ = Temp; Left ^ = (Temp << 1);

// the right side needs to be shifted and to get the last four bits of the left side

Temp = (Left << 8) | ((Right >>> 20) & 0x

000000F

0);

// Left Needs to Be Put Upside Down

Left = (Right << 24) | ((Right << 8) & 0xFF0000) | ((Right >>> 8) & 0xFF00) | ((Right >>> 24) & 0xF0);

Right = TEMP;

// Now Go Through and Perform these Shifts on the left and right keys

For (i = 0; i

// shift the keys each omen or two bits to the left

IF (Shifts [I]) {Left = (Left << 2) | (LEFT >>> 26); Right = (Right << 2) | (Right >>> 26);}

ELSE {Left = (Left << 1) | (Left >>> 27); Right = (Right << 1) | (Right >>> 27);

LEFT & = -0xf; Right & = -0xf;

// Now Apply PC-2, in Such A Way That E Is Easier When Encrypting or Decrypting

// this conversion will look like PC-2 Except Only the Last 6 Bits of Each Byte Are Used

// Rather Than 48 Consecutive Bits and The Order of Lines Will Be According To

//how the s Selection functions Will BE Applied: S2, S4, S6, S8, S1, S3, S5, S7

LEFTTEMP = PC2BYTES0 [Left >>> 28] | PC2Bytes1 [(Left >>> 24) & 0xF]

PC2BYTES2 [(Left >>> 20) & 0xF] | PC2Bytes3 [(Left >>> 16) & 0xF]

| pc2bytes4 [(LEFT >>> 12) & 0xF] | PC2Bytes5 [(Left >>> 8) & 0xF] | PC2Bytes6 [(LEFT >>> 4) & 0xF];

RightTemp = PC2Bytes7 [Right >>> 28] | PC2BYTES8 [(Right >>> 24) & 0xF]

PC2BYTES9 [(Right >>> 20) & 0xF] | PC2BYTES10 [(Right >>> 16) & 0xF]

PC2BYTES11 [(Right >>> 12) & 0xF] | PC2Bytes12 [(Right >>> 8) & 0xF]

| pc2bytes13 [(right >>> 4) & 0xf];

Temp = ((RightTemp >>> 16) ^ leftTemp) & 0x0000FFFF;

Keys [N ] = leftTemp ^ Temp; Keys [N ] = RightTemp ^ (Temp << 16);

}

} // for Each Items

// Return the keys we've created

Return Keys;

} // end of des_createkeys

// Test //

Function StringTohex (s) {

Var r = "0x";

Var Hexes = New Array ("0", "1", "2", "3", "4", "5", "6", "8", "9", "A", "B", "c", "d", "e", "f");

For (var i = 0; i > 4] hexes [s.charcodeat (i) & 0xf];

Return R;

}

Var key = "this is a 24 byte key !!";

Var Message = "this is a test message";

VAR Ciphertext = DES (Key, Message, 1, 0);

Document.writeln ("des Test:" StringToHex (Ciphertext));

转载请注明原文地址:https://www.9cbs.com/read-7065.html

New Post(0)