Java file encryptor (collection)

xiaoxiao2021-03-06  44

Import java.awt. *; import java.awt.event. *; import javax.swing. *; import java.io. *; import java.security. *; import javax.crypto. *; import javax.crypto.spec *; / ** file name: fileEncrypter.java JDK: 1.40 or more description: File encryption encryption method: Triple DES encryption encryption process: Generate an increase in ".tdes" extension under the same folder after encryption of selected files Encrypted file decryption process: Decryption of encrypted files (must have ".tdes" extension) * / public class filencrypter extends jframe {public static final int width = 550; public static final int Height = 200; public static void, PUBLIC main (String args []) {FileEncrypter fe = new FileEncrypter (); fe.show ();} FileEncrypter () {this.setSize (WIDTH, HEIGHT); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); this.setResizable (false ); Toolkit TK = Toolkit.getDefaultToolkit (); Dimension Screensize = Tk.getscreensize (); this.setLocation (Screensize.Width - Width) / 2 (Screensize.Height - Height) / 2); this.Settitle (" File Causes (Trides) "); Container C = this.getContentPane (); C.SetLayout (New FlowLayout ()); Final FilePanel FP = New FilePanel (" File Selection); c.Add (FP); Final KeyPanel PP = New KeyPanel ("Password"); C.ADD (PP); JButton JBE = New JButton; C.Add (Jbe); Jbe.AddActionListener (new actionListener () {pUBlic void ActionPerformed (ActionEvent Event) {file file = new file (fp.getFileName () ); if (file.exists ()) encrypt (file.getabsolute (), pp.getKey ()); Else Joptionpane.showMessageDialog (null, "please select the file! "," Tips ", JOPANE.OK_OPTION;}}); JButton JBD = New JButton (" Decryption "); C.Add (JBD); JBD.AddActionListener (New ActionListener () {Public Void ActionPerformed (ActionEvent Event) { File file = new file (fp.getfilename ());

IF (file.exists ()) Decrypt (file.getabsolute (), pp.getKey (); Else Joptionpane.showMessageDialog (Null, "Please select File!", "Tips", JOPANE.OK_OPTION;}}); } / ** encryption function input: file to be encrypted, the password (0-F of the composition, a total of 48 characters, is represented by three 8-bit code), such as: AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 wherein: AD67EA2F3BE6E5AD DES password is a password D368DFE03120B5DF DES two 92A8FD8FEC2F0746 DES password Three output: After encryption of the input file, save it to the same folder to add ".tdes" extension files. * / private void encrypt (file filein, string skey) {try {if (sKey.Length () == 48) {byte [] ByTK1 = getKeyBystr (SKEY.SUBSTRING (0, 16)); byte [] bytk2 = getKeyBystr (SKEY.SUBSTRING (16, 32)); Byte [] Bytk3 = getKeyBystr (Skey.Substring (32, 48)); fileinputstream fis = new fileinputstream (filein); byte [] bytin = new byte [(int) filein. Length ()]; for (int i = 0; i

* / Private Void Decrypt (File Filein, String Skey) {Try {IF (Skey.Length () == 48) {string strpath = filein.getpath (); if (strpath.substring (StrPath.Length () - 5) .tolowercase (). Equals (". tdes")) StrPath = strpath.substring (0, strpath.length () - 5); else {joptionpane.showMessageDialog (this, "Not a legal encrypted file!", "Tips" , JOPTIONPANE.OK_OPTION; RETURN;} jfilechooser chooser = new jfilechooser (); chooser.SetCurrentDirectory (new file (".")); Chooser.setSelectedFile (new file (strpath)); // Specify the file Int to save Ret = chooser.showsavedialog (this); if (Ret == jfilechooser.Approve_option) {byte [] ByTK1 = getKeyBystr (SKEY.SUBSTRING (0, 16)); byte [] Bytk2 = getKeyBystr (SKEY.SUBSTRING (16,32 )); byte [] bytk3 = getKeyBystr (SKEY.SUBSTRING (32, 48)); fileinputstream fis = new fileinputstream (filein); Byte [] Bytin = new byte [(int) filein.length ()]; for (int) i = 0; i

Encrypted password * / private byte [] encryptByDES (byte [] bytP, byte [] bytKey) throws Exception {DESKeySpec desKS = new DESKeySpec (bytKey); SecretKeyFactory skf = SecretKeyFactory.getInstance ( "DES"); SecretKey sk = skf .GenerateSecret (Desks); Cipher CIP = Cipher.GetInstance ("DES"); CIP.INIT (Cipher.Encrypt_Mode, SK); Return Cip.dofinal (BYTP);} / ** Decrypt the input byte by DES method BYTKEY must be 8 bytes long, is decrypted password * / private byte [] decryptByDES (byte [] bytE, byte [] bytKey) throws Exception {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 (Byte);} / ** Enter password Character form, return to the parameter form.

Such as input string: AD67EA2F3BE6E5AD return byte array: {173, 103, 234, 47, 59, 230, 229, 173} * / private byte [] getKeyBystr (String str) {byte [] Bret = new byte [str.length () / 2]; for (int () / 2]; for i = 0; i

* / Class FilePanel extends JPanel {FilePanel (String str) {JLabel label = new JLabel (str); JTextField fileText = new JTextField (35); JButton chooseButton = new JButton ( "Browse ..."); this.add (label ); this.add (fileText); this.add (chooseButton); clickAction ca = new clickAction (this); chooseButton.addActionListener (ca);} public String getFileName () {JTextField jtf = (JTextField) this.getComponent (1 ); return jtf.getText ();} private class clickAction implements ActionListener {clickAction (Component c) {cmpt = c;} public void actionPerformed (ActionEvent event) {JFileChooser chooser = new JFileChooser (); chooser.setCurrentDirectory (new File ( ".")); int RET = Chooser.Showopendialog (cmpt); if (ret == jfilechooser.Approve_option) {JPANEL JP = (JPANEL) CMPT; JTextField JTF = (jtextfield) jp.getComponent (1); jtf.setText (Chooser.getSelectedFile (). getpath ());}}}} / ** Password generation component.

* / Class KeyPanel extends JPanel {KeyPanel (String str) {JLabel label = new JLabel (str); JTextField fileText = new JTextField (35); JButton chooseButton = new JButton ( "random"); this.add (label); THIS.Add (photo.add); ClickAction Ca = New ClickAction (this); ChooseButton.AddActionListener (CA);} // Return to the generated password (48 characters length) Public string getKey () {jTextField jtf = (JTextField) this.getComponent (1); return jtf.getText ();} private class clickAction implements ActionListener {clickAction (Component c) {cmpt = c;} public void actionPerformed (ActionEvent event) {try {KeyGenerator kg = Keygenerator.GetInstance ("des"); kg.init (56); key ke = kg.generateKey (); byte [] bytk1 = ke.getencoded (); ke = kg.generateKey (); byte [] Bytk2 = ke .getecoded (); ke = kg.generateKey (); byte [] bytk3 = ke.Getencoded (); jPanel JP = (jPanel) cmpt; jtextfield jtf = (jtextfield) jp.getComponent (1); jtf.settext (GetBytestr (BYTK1) GetBytestr (Bytk2) GetBytestr (Bytk3));} catch (exception e) {E.PrintStackTrace ();}} PRI Vate string getBytestr (byte [] byt) {string strret = ""; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-54147.html

New Post(0)