Introduction to 3DES:
3DES (or Triple DES) is the general term for the block password of the Triple Data Encryption Algorithm. It is equivalent to applying a DES encryption algorithm three times to each data block. Due to the enhanced computing power of computers, the key length of the original DES password has become easily brute-forced; 3DES is designed to provide a relatively simple method, namely, to avoid similar attacks by increasing the key length of the DES, rather than designing a brand new block cipher algorithm.
Today I need to implement des on the client side. I quickly found http://www.tero.co.uk/des/code.php
Application example
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));Of course, it was found that this was not what we needed. Our server uses des-ede, which seems to be called triple des in java
There is a small pit here that supports 3des
//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
But there is a small error in using the key
If it is 64 bits, it is the positive solution of des, but 3des may be 128 bits or 192 bits.
The process of 3des is en(k3, de(k2, en(k1, P))))
When k1=k3, a simplified 128-bit key can be used.
key="k1k1k1k1k2k2k2k2k2" In fact, it should be equivalent to k1k1k1k1k2k2k2k2k1k1k1k1k1. The author did not implement it
Another one is padding mode. In short, when block encryption, how should insufficient space be filled? Below is the source code
if (padding == 2) message += " "; //pad the message with spaceselse if (padding == 1) {temp = 8-(len%8); message += String.fromCharCode (temp,temp,temp,temp,temp,temp,temp,temp); if (temp==8) len+=8;} //PKCS7 paddingelse if (!padding) message += "/0/0/0/0/0/0/0/0/0/0/0"; //pad the message out with null bytesNow PKCS5/7 is generally used
Pay attention to these two points when calling, in fact des-ede is implemented.
Example
var key ="b964b7c58e99b59"+"b964b7c5";var message = "This is a test message";var ciphertext = des(key, message, 1, 0,0,1);document.writeln("<br/>DES3 Test: " + stringToHex (ciphertext));The above is the 3DES in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!