Java implementation of Triple DES encryption software __java

Source: Internet
Author: User
Tags gettext
Des is a 16-wheeled Feistel structure cipher with a packet length of 64 bits, a 56-bit key to encrypt a 64-bit plaintext string, and a 64-bit cipher string. Where the key is 64 bits, the utility is 56 bits, and the other 8 bits are used as parity. The process of encryption is the first 64-bit plaintext group initial replacement, then the left and right two parts after 16 rounds of iterations, and then the cycle shift and transformation, and finally the inverse transformation to obtain ciphertext. Encryption and decryption use the same key, so it belongs to the symmetric cipher system.
Triple DES encryption algorithm security is high, the software can only encrypt 8-byte multiples of the length of the file, the follow-up will continue to develop. The
effect chart is as follows:


`

Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import java.lang.*;
Import java.io.*;
Import java.security.*;
Import javax.crypto.*;

Import javax.crypto.spec.*; @SuppressWarnings ("Serial") public class Fileencrypter extends JFrame {@SuppressWarnings ("deprecation") public s
        tatic void Main (String args[]) {fileencrypter fe=new fileencrypter ();   
    Fe.show ();
        } fileencrypter () {this.setsize (550,200);
        This.setdefaultcloseoperation (Jframe.exit_on_close);
        This.setlocation (400,300);
        This.settitle ("File encryption Tool (3DES)");
        Container c =this.getcontentpane ();
        C.setlayout (New FlowLayout ());
        JLabel label=new JLabel ("File Selection");
        C.add (label);
        Final JTextField filetext=new JTextField (35);
        C.add (Filetext);
        JButton choosebutton=new JButton ("browsing ..."); Choosebutton.addactionlistener (new ActionListener ()//The following write browse time Listener and event {public void ActiOnperformed (ActionEvent e) {jfilechooser chooser=new jfilechooser ();
                Chooser.setcurrentdirectory (New File ("."));
                int result=chooser.showopendialog (NULL); if (result==jfilechooser.approve_option)//Get the absolute path of the selection file {String path=chooser.getselected
                    File (). GetAbsolutePath (); 
                Filetext.settext (path);
        }
            }   
        });
        C.add (Choosebutton);
        JLabel label2=new JLabel ("key (24 characters):");
        C.add (Label2);
        Final JTextField keytext=new JTextField (35);
        C.add (Keytext);
        JButton jbe=new JButton ("Encryption");
        C.add (JBE); Jbe.addactionlistener (new ActionListener ()//The following writes encrypted monitoring and events {public void actionperformed (actio
                    Nevent event) {String Wenjian,miyao;
                    Wenjian=filetext.gettext (); Miyao=keytext.gettext(); if ("". Equals (Wenjian) | | wenjian==null) Joptionpane.showmessagedialog (NULL, "Please select a file.")
                    "," hint ", joptionpane.ok_option); else if ("". Equals (Miyao) | | miyao==null) Joptionpane.showmessagedialog (null, "Please enter a 24-character Fumi key.")
                        "," hint ", joptionpane.ok_option);
                                else {if (Miyao.length ()!=24) { Joptionpane.showmessagedialog (NULL, "key must be 24 characters.)
                            "," hint ", joptionpane.ok_option); } else {byte[] Key1=miyao.substri
                                Ng (0, 8). GetBytes ();
                                Byte[] Key2=miyao.substring (8,16). GetBytes ();    Byte[] Key3=miyao.substring (16,24). GetBytes (); The three keys are stored in a byte-type array, file File=new file (WenjiAN);
                                Byte[] Plain=bytefromfile (file);
                                    Read clear text and deposit into a byte-array plain, Bytefromfile () method is defined in the after try {
                                            Byte[] Bytout=encryptbydes (Encryptbydes (Encryptbydes (plain,key1), Key2), Key3); Implement encryption, the encrypted ciphertext section is stored in Bytout encryptbydes () method is defined in the back String fileout=we
                                njian+ ". Tdes";
                                FileOutputStream fos=new FileOutputStream (fileout); for (int i=0;i<bytout.length;i++) {fos.write (int) b
                                Ytout[i]); Fos.close ();//Writes the contents of the Bytout array to the new file joptionpane.showmessaged Ialog (NULL, "encryption succeeded.")
                                "," hint ", joptionpane.information_message);

                     }           catch (Exception e) {Joptionpane.showmessag Edialog (NULL, "Encryption failed.") Please check the file or key.
                                "," hint ", joptionpane.ok_option); }}});/write encryption event completed JButton JbD
        =new JButton ("decryption");
        C.add (JbD); Jbd.addactionlistener (new ActionListener () {//write decrypted monitoring and event public void actionperformed (Actioneve
                    NT Event) {String Wenjian,miyao;
                    Wenjian=filetext.gettext ();
                    Miyao=keytext.gettext (); if ("". Equals (Wenjian) | | wenjian==null) Joptionpane.showmessagedialog (NULL, "Please select a file.")
                    "," hint ", joptionpane.ok_option); 
                         if (Wenjian.substring (Wenjian.length ()-5). toLowerCase (). Equals (". Tdes")) if (Miyao.length ()!=24)   {Joptionpane.showmessagedialog (NULL, "key must be 24 characters.)
                            "," hint ", joptionpane.ok_option); else {Wenjian1=wenjian.substrin
                                G (0, Wenjian.length ()-5);
                                JFileChooser chooser=new JFileChooser ();
                                Chooser.setcurrentdirectory (New File ("."));
                                Chooser.setselectedfile (New File (Wenjian1));
                                The user specifies the saved file int ret=chooser.showsavedialog (NULL); if (ret==0) {byte[] key1=miyao.substring (0, 8). GetByte
                                    S ();
                                    Byte[] Key2=miyao.substring (8,16). GetBytes (); Byte[] Key3=miyao.substring (16,24). GetBytes ();
                             Read Decryption key       File File=new file (Wenjian);
                                        Byte[] Miwen=bytefromfile (file);//Read ciphertext try { Byte[] Bytout=decryptbydes (Decryptbydes (Decryptbydes (miwen,key1), Key2), Key3);//decrypted, confidential
                                        The ciphertext is stored in the bytout, and the Decryptbydes () method is followed by File Fileout=chooser.getselectedfile ();
                                        Fileout.createnewfile ();
                                        FileOutputStream fos=new FileOutputStream (fileout);
                                            for (int i=0;i<bytout.length;i++) {
                                        Fos.write ((int) bytout[i]);
                                        } fos.close (); Joptionpane.showmessagedialog (NULL, "decryption complete.")
       "," hint ", joptionpane.information_message);                             catch (Exception e) {Joptionpane.showmessagedialog (null, "decryption failed.) Please check the key or file.
                                    "," hint ", joptionpane.ok_option);
                        }} else { Joptionpane.showmessagedialog (NULL, "is not a legitimate encrypted file.)
                        "," hint ", joptionpane.ok_option);
                    Return }//decryption completed} private byte[] Bytefromfile (File filein) {byt
            E[] textoffile=new byte[(int) filein.length ()];
                try {fileinputstream fin=new fileinputstream (Filein);
                for (int i=0;i<filein.length (); i++) {textoffile[i]= (byte) fin.read ();
            } fin.close ();

}            catch (IOException e) {System.err.println (e);
        return textoffile; }//This method reads bytes from the input file, is stored in the Textoffile array and returns the value of this array private byte[] Encryptbydes (byte[] bytp,byte[] bytkey) throws Exceptio
            n {deskeyspec desks=new deskeyspec (Bytkey);
            Secretkeyfactory skf=secretkeyfactory.getinstance ("DES");
            Secretkey Sk=skf.generatesecret (desks);
            Cipher cip=cipher.getinstance ("DES");
            Cip.init (CIPHER.DECRYPT_MODE,SK);
        Return cip.dofinal (BYTP); }//This method encrypts the text section and key bytes entered, and returns the ciphertext section @SuppressWarnings ("unused") private byte[] Decryptbydes (byte[) byte,by
            Te[] bytkey) throws Exception {Deskeyspec desks=new (DESKEYSPEC);
            Secretkeyfactory skf=secretkeyfactory.getinstance ("DES");
            Secretkey Sk=skf.generatesecret (desks);
            Cipher cip=cipher.getinstance ("DES"); Cip.init (CIpher.
            Decrypt_mode, SK);
        Return cip.dofinal (BytE);






 }//This method decrypts the entered text section and key and returns the plaintext byte}

'

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.