Example of DES encryption implemented by javascript

Source: Internet
Author: User
Tags pkcs7

Des. js
Copy codeThe Code is as follows:
//
// This software is provided "as is" AND
// Any express or implied warranties, INCLUDING, but not limited to,
// Implied warranties of merchantability and fitness for a participant PURPOSE
// Are disclaimed. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 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 tort (including negligence or otherwise) ARISING IN ANY WAY
// Out of the use of this software, EVEN IF ADVISED OF THE POSSIBILITY
// Such damage.

// Des
// This takes the key, the message, and whether to encrypt or decrypt
Function des (key, message, encrypt, mode, iv, padding ){
// Declaring this locally speeds things up a bit
Var spfunction1 = new Array (0x10400,0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0, 0x1010004, 0 x );
Var spfunction2 = new Array (-linear,-0x7fff8000, 0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-linear,-0x7fffffe0,-linear,-0x7fef8000, -0x80000000,-0x7fff8000, 0x100000,0x20,-0x7fefffe0, 0x0000000,0x100020,-0000, 0,-0x80000000,0x8000,0x108020,-0x7ff00000, 0x100020,-0x7fffffe0, 8020x0000000,0x108020,-0x7fef8000,-0x7ff00000, 0x8020,0, 0x100000,-0x7fefffe0, 0 x,-limit,-0x7ff00000, -0x7fef8000, 0x8000,-0x7ff00000,-0x7fff8000, 0x20,-0x7fef7fe0, 0x108020,0x8000,-0x80000000,0x8020,-0x7fef8000, 0x100000,-0x7fffffe0, 0x100020,-0x7fff7fe0,-0x7fffffe0, 0x100020,0x0000000,0,-0x7fff8000, 0x8020,-0x80000000,-0x7fefffe0, -0x7fef7fe0, 0x108000 );
Var spfunction3 = new Array (0x208,0x8020200,0, 0x8020008,0x8000200,0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0, 0 x, 0 x, 0 x, 0 x, 0x20200 x, 0 x );
Var spfunction4 = new Array (0x802001,0x2081,0x2081,0x80, 0x802080,0x800001,0x2001,0, 0x802000,0x802000,0x802081,0x81,0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x 800001,0x802000,0x802081,0x81,0, 0, 0 x, 0 x, 0 x 802080, 0 x, 0 x, 0 x, 0 x );
Var spfunction5 = new Array (0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x 2000000,0x40080000,0x40080000,0, 0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x 2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0, 0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0, 0x40080000,0x2080100,0x40000100 );
Var spfunction6 = new Array (0x20400000,0 x, 0x20400000,0x10, 0 x, 0 x, 0 x, 0 x, 0 x, 0, 0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0, 0 x 20404010,0x404000,0x20400000,0x404010,0x20404000,0, 0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0x20404000,0x200010000,0x400010,0x20004010 );
Var spfunction7 = new Array (0x200000,0x4200002,0x4000802,0, 0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0, 0x4000002,0 x, 0 x records, 0 x records, 0 x, 0 x, 0 x 4200802,0x4200000,0x200800,0, 0x200002x4200802,0, 0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0 x );
Var spfunction8 = new Array (0 x, 0 x 10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0, x, 0 x, 0 x, 0 x, 0 x 40000,0x10001040,0, 0x10041000 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );

// 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 single and triple des
Var iterations = 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 );}

// Pad the message depending on the padding parameter
If (padding = 2) message + = ""; // pad the message with spaces
Else if (padding = 1) {temp = 8-(len % 8); message + = String. fromCharCode (temp, temp); if (temp = 8) len + = 8;} // PKCS7 padding
Else if (! Padding) 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 <len ){
Left = (message. charCodeAt (m ++) <24) | (message. charCodeAt (m ++) <16) | (message. charCodeAt (m ++) <8) | message. charCodeAt (m ++ );
Right = (message. charCodeAt (m ++) <24) | (message. charCodeAt (m ++) <16) | (message. charCodeAt (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 permuted according to IP
Temp = (left> 4) ^ right) & 0x0f0f0f; right ^ = 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) & 0x55555555; 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 <iterations; j + = 3 ){
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) & 0x3f] | spfunction4 [(right1> 16) & 0x3f]
| Spfunction6 [(right1 >>> 8) & 0x3f] | spfunction8 [right1 & 0x3f]
| Spfunction1 [(right2> 24) & 0x3f] | spfunction3 [(right2> 16) & 0x3f]
| Spfunction5 [(right2> 8) & 0x3f] | spfunction7 [right2 & 0x3f]);
}
Temp = left; left = right; right = temp; // unreverse left and right
} // For either 1 or 3 iterations

// Move then each one bit to the right
Left = (left >>> 1) | (left <31 ));
Right = (right >>> 1) | (right <31 ));

// Now perform IP-1, which is IP in the opposite direction
Temp = (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) & 0x0f0f0f; right ^ = 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 speeds things up a bit
Struct = new Array (20010204 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Records = new Array (4100101 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Rows = new Array (1000808 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Struct = new Array (8222000 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Rows = new Array (41010 x, 0x10, 0 x, 0, 0 x, 0x10, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Sums = new Array (2000420 x, 0x20, 0 x, 0 x, 0x20, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Arrays = new Array (10080002 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Rows = new Array (20030800 x rows, 0 x, 0 x, 0 x rows, 0 x rows, 0 x, 0 x, 0 x, 0 x, 0 x );
Struct = new Array (2040002 x, 0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Arrays = new Array (10000408 x, 0 x, 0 x, 0 x );
Struct = new Array (102020 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Rows = new Array (5200200 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Records = new Array (8081010 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x );
Pc2bytes13 = new Array (105 x, 0 x, 0 x, 0 x, 0 x );

// How many iterations (1 for des, 3 for triple des)
Var iterations = key. length> 8? 3: 1; // changed by Paul 16/6/2007 to use Triple DES for 9 + byte keys
// Stores the return keys
Var keys = new Array (32 * iterations );
// 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, 0 );
// Other variables
Var lefttemp, righttemp, m = 0, n = 0, temp;

For (var j = 0; j <iterations; j ++) {// either 1 or 3 iterations
Left = (key. charCodeAt (m ++) <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) & 0x0f0f0f; right ^ = 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) & 0x55555555; 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) & 0x000000f0 );
// 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 (var I = 0; I <shifts. length; I ++ ){
// Shift the keys either one 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 doesn't only the last 6 bits of each byte are used
// Rather than 48 consecutive bits and the order of lines will be according
// 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 iterations
// 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", "7 ", "8", "9", "a", "B", "c", "d", "e", "f ");
For (var I = 0; I <s. length; I ++) {r + = hexes [s. charCodeAt (I)> 4] + hexes [s. charCodeAt (I) & 0xf];}
Return r;
}

Function hexToString (h ){
Var r = "";
For (var I = (h. substr (0, 2) = "0x ")? 2: 0; I 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 ));

Usage
Copy codeThe Code is as follows:
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 ));

// Encrypt using single DES (with an 8 byte key) in CBC mode
// With the given input vector and PKCS7 padding
Des ("8 bytekey", "This is the message.", 1, 1, "inputvec", 1 );
// Encrypt using triple DES (with a 24 byte key) in ECB mode
Des ("this is a 24 byte key !! "," This is the message. ", 1 );
// Decrypt using single DES in ECB mode
Des ("8 bytekey", "2384lf & * £90 LSdsf", 0 );

The DES encryption algorithm encrypts user information when it is sent. Very practical

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.