Real Data DES encryption using Java (Java source code)

Source: Internet
Author: User

Based on the analysis in the previous article "Introduction to Real Data Encryption Using DES (analysis)", Java can be used to encrypt strings, however, the legacy problem is that the string cannot be decrypted, because after the encrypted byte array is converted into a string, it will be different from the original byte array, and there will be fewer digits, I don't know why. Isn't a one-to-one conversion between a non-byte array and a string? Confused

For specific implementation, You can first look at this figure. Two classes are added, despre and descontrol. Despre mainly implements the basic operations mentioned in the previous article, such as converting byte arrays into binary arrays. Descontrol is mainly used to operate des (core algorithm class) and despre (preprocessing class). It can be regarded as a process control class.

The code for the descontrol class is given below, and the link for other codes is provided at last.

Package mydes; </P> <p>/** <br/> * des control class, which encapsulates the des core and DES and processing class, leave a good excuse for the front-end <br/> * @ author Hu Yang <br/> * blog: http://www.the5fire.net <br/> * copyright statement: reprinted, please keep the complete source code information <br/> */<br/> public class descontrol {</P> <p>/** <br/> * preprocessing operation class <br/> */<br/> private despre = new despre (); </P> <p>/** <br/> * des core algorithm class <br/> */<br/> private Des = new des (); </P> <p> private int [] K = NULL; </P> <p> Public descontrol () {</P> <p >}</P> <p>/** <br/> * Input key <br/> * @ Param keytext <br/> */ <br/> Public descontrol (string keytext) {<br/> byte [] Key = despre. dataformat (keytext. getbytes (); <br/> K = despre. convertbinary (key); <br/>}</P> <p>/** <br/> * encryption: the input plaintext is encrypted. The result is returned in an array of bytes. <br/> * @ Param plaintext <br/> * @ return <br/> */<br/> Public byte [] desentrypt (string plaintext) {<br/> byte [] mtext = NULL; <br/> int [] M = NULL; <br/> byte [] result = NULL; // stores the encrypted result </P> <p> // 1. first, pre-process it <br/> // convert it to a byte array <br/> mtext = despre. dataformat (plaintext. getbytes (); </P> <p> int datalen = mtext. length; <br/> int unitcount = datalen/8; </P> <p> // converts byte to binary. <br/> M = despre. convertbinary (mtext); </P> <p> // instantiate the result based on the converted plaintext. <br/> result = new byte [M. length/8]; </P> <p> // 2. group encryption <br/> for (INT I = 0; I <unitcount; I ++) {<br/> int [] tmpkey = new int [64]; <br/> int [] tmpdata = new int [64]; </P> <p> system. arraycopy (K, 0, tmpkey, 0, 64); <br/> system. arraycopy (M, I * 64, tmpdata, 0, 64); </P> <p> int [] tmpc = des. encrypt (tmpdata, tmpkey); <br/> byte [] tmpresult = despre. convertbytes (tmpc); <br/> system. arraycopy (tmpresult, 0, result, I * 8, 8); <br/>}</P> <p> return result; <br/>}</P> <p>/** <br/> * decryption: imports an encrypted byte array, after decryption, return in byte array form <br/> * @ Param ctext <br/> * @ return <br/> */<br/> Public byte [] desdecryption (byte [] ctext) {</P> <p> int [] C = despre. convertbinary (ctext); </P> <p> int datalen = ctext. length; <br/> int unitcount = datalen/8; </P> <p> // according to the converted plaintext, instantiate result <br/> byte [] result = new byte [C. length/8]; </P> <p> for (INT I = 0; I <unitcount; I ++) {<br/> int [] tmpkey = new int [64]; <br/> int [] tmpdata = new int [64]; </P> <p> system. arraycopy (K, 0, tmpkey, 0, 64); <br/> system. arraycopy (C, I * 64, tmpdata, 0, 64); </P> <p> int [] tmpc = des. decryption (tmpdata, tmpkey); <br/> byte [] tmpresult = despre. convertbytes (tmpc); <br/> system. arraycopy (tmpresult, 0, result, I * 8, 8); <br/>}< br/> byte [] tmip = despre. removeexpand (result); // removes extended bits </P> <p> return tflat; <br/>}< br/>}

Several online source codes are referenced in the implementation process. Thanks to those who are willing to share them.

Source Code address: http://u.115.com/file/f49b8fc342 (valid for 30 days, if expired or cannot download please leave a message to tell me)

The entire code is still written in eclipse, and the package is also an Eclipse project.

If you cannot understand it here, read the previous basic article first.

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.