Des encryption algorithm JAVA Implementation (Core Algorithm + Source Code)

Source: Internet
Author: User

In fact, with the previous article "the prelude to learning DES encryption algorithms" as the basis, it is easy to implement the DES algorithm.
However, I found the source code from the Internet, written in Java, and written in C ++, but all the code seems to be the same. Why is it the same? At the beginning, I wrote a class and added all the methods to the class. I guess it would be hard to implement it once if it wasn't the author's skill. In addition, if the reader's skill is not high (for example, I do), it is difficult to understand the call relationship. Another point worth mentioning is that the methods circulating on the Internet seem to be able to encrypt text directly. Therefore, it is a tragedy for people who want to study algorithms, originally You Want To Know 1 + 1 = ?, I will tell you that 1 + 1 + 537674-637674 + 100000 = 2. This is quite tangled.

From the above reasons, we can conclude that I simply wrote the core algorithm here. If you want to use it for encryption and decryption, you need to expand it by yourself. Of course, I will continue to expand.
Okay, let's talk about the algorithm. Because the Java implementation is used, in order not to mess up your hands and feet when writing, first sort out the class diagram, and then let's look at the figure:

As you can see, I have divided three classes into one interface. Replace tables are placed in the iconst interface. The desfunction class is actually an implementation of the previous article. It can be said that it is a basic des method class, which contains the basic algorithms implemented by DES. The des class simply calls the desfunction class and combines the methods for encryption and decryption. In fact, the two methods decryption () and encryp () in des can be written as one, because the code is almost identical, I wrote it separately for convenience of analysis.

There seems to be so much to say about this. It is easy to analyze it. Because the source code is too long, it is directly packaged and uploaded to the space. You can download it by yourself. Here, only the source code of the des class is posted, because this should be more important.

Package mydes; </P> <p>/** <br/> * des class, used to encrypt binary data <br/> * @ author the5fire <br/> * blog: http://www.the5fire.net <br/> */<br/> public class des {</P> <p> private desfunction = new desfunction (); <br/>/** <br/> * transmits the data to the binary plain text, return the encrypted ciphertext of des <br/> * @ Param m <br/> * @ return <br/> */<br/> Public int [] encrypt (INT [] m, int [] Key) {<br/> int [] C = NULL; <br/> int [] m0 = new int [64]; <br/> int [] [] L0 = new int [17] [32]; <br/> int [] [] R0 = new int [17] [32]; <br/> int [] [] subkey = NULL; <br/> int [] result = new int [64]; </P> <p> subkey = desfunction. getsubkey (key); </P> <p> m0 = desfunction. initreplace (m); </P> <p> for (Int J = 0; j <32; j ++) {<br/> l0 [0] [J] = M0 [J]; // Initialization on the left of the plaintext <br/> R0 [0] [J] = M0 [J + 32]; // Initialization on the right of the plaintext <br/>}</P> <p> for (INT I = 1; I <17; I ++) {<br/> int [] tflat = desfunction. divplus (l0 [I-1], desfunction. corefunction (R0 [I-1], subkey [I-1]); <br/> for (Int J = 0; j <32; j ++) {<br/> l0 [I] [J] = R0 [I-1] [J]; <br/> R0 [I] [J] = TPR [J]; <br/>}</P> <p> // assign a value. R16 is placed on the left, that is, the front 32 bits; L16 is placed on the right, that is, the last 32-bit <br/> for (INT I = 0; I <32; I ++) {<br/> result [I] = R0 [16] [I]; <br/>}</P> <p> for (INT I = 0; I <32; I ++) {<br/> result [32 + I] = l0 [16] [I]; <br/>}</P> <p> C = desfunction. endreplace (result); </P> <p> return C; <br/>}</P> <p>/** <br/> * transmits the encrypted ciphertext to DES, returns the binary plaintext <br/> * @ Param C <br/> * @ return <br/> */<br/> Public int [] decryption (INT [] C, int [] K) {<br/> int [] M = NULL; <br/> int [] C0 = new int [64]; <br/> int [] [] L0 = new int [17] [32]; <br/> int [] [] R0 = new int [17] [32]; <br/> int [] [] subkey = NULL; <br/> int [] result = new int [64]; </P> <p> subkey = desfunction. getsubkey (k); </P> <p> C0 = desfunction. initreplace (c); </P> <p> for (Int J = 0; j <32; j ++) {<br/> l0 [0] [J] = C0 [J]; // Initialization on the left of the plaintext <br/> R0 [0] [J] = C0 [J + 32]; // Initialization on the right of the plaintext <br/>}</P> <p> for (INT I = 1; I <17; I ++) {<br/> int [] tflat = desfunction. divplus (l0 [I-1], desfunction. corefunction (R0 [I-1], subkey [16-I]); <br/> for (Int J = 0; j <32; j ++) {<br/> l0 [I] [J] = R0 [I-1] [J]; <br/> R0 [I] [J] = TPR [J]; <br/>}</P> <p> // assign a value. R16 is placed on the left, that is, the first 32 bits; L16 is placed on the right, that is, the last 32-bit <br/> for (INT I = 0; I <32; I ++) {<br/> result [I] = R0 [16] [I]; <br/>}</P> <p> for (INT I = 0; I <32; I ++) {<br/> result [32 + I] = l0 [16] [I]; <br/>}</P> <p> M = desfunction. endreplace (result); <br/> return m; <br/>}< br/>}

Here, l0 [] [] and R0 [] [] are not suitable for the two-dimensional array, which is a waste of space, because l0 [I] and R0 [I] only need to push and export l0 [I + 1] and R [I + 1, l0 [I] and R0 [I] will not be used later. recursion should be better here. However, this is more intuitive.

Finally, I tested it with an example in the book, and it can be encrypted and decrypted normally. If you have any questions, please remind me.

Source Code address: http://u.115.com/file/f465ac9bfe (valid for 30 days, if expired you can leave a message to tell me)

This is the packaging of the entire project. I use eclipse.

 

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.