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: After encryption of selected files, generate an encrypted file that adds ".tdes" extension below with the folder. Decryption process: Decryption of the selected encrypted file (must have ".TDES extension) * / public class filencrypter extends JFrame {public static final int width = 550; public static final int.com = 200;
Public static void 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)"); 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 ("Encrypted"); C.Add (JBE); Jbe.AddActionListener (New ActionListener () {file file = new file (fp.getFileName ()); if ( File.exists ()) encrypt (file.getabsolute (), pp.getKey (); else joptionpane.showMessageDialog (Null, "Please select File!", "Tips", JOPANE.OK_OPTION;}}); JButton JBD = New JButton; 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 (the 0-F composed of a total of 48 characters, represents three 8-bit code), such as: AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 wherein: three output AD67EA2F3BE6E5AD DES password a D368DFE03120B5DF DES password two 92A8FD8FEC2F0746 DES code: for After the input file is encrypted, save it to the same folder to add the ".tdes" extension file. * / 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 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 by DES method Bytkey is 8-bytes long, is a crypto password * / private byte [] encryptbydes (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.ENCRYPT_MODE, sk); return cip .dofinal (BYTP); / ** Decrypted the byte by DES method Bytkey to be 8-bytes long, is the decryption password * / private byte [] decryptbydes (byte [] Byte, byte [] Bytkey) throws = 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 the character form of the password, 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 / ** File Select Components. * / 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); returnjtf.gettext (); Private class clickaction imports 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. settelectedfile (). getPath ());}} PRIVATECIT CMPT;}} / ** 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 (FileText); this.add (chaoosebutton); 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 imports 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.Getencoded (); 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 ();}} Private string getBytestr (Byte [] BYT) {string strret = ""; for (int i = 0; i Private string getHexValue (int S) {string sret = null; switch (s) {case 0: SRET = "0"; Break; case 1: sret = "1"; break; case 2: sret = "2"; BREAK Case 3: SRET = "3"; Break; Case 4: SRet = "4"; Break; Case 5: SRET = "5"; Break; Case 6: SRet = "6"; Break; Case 7: SRet = "7"; breth; sret = "8"; break; case 9: sret = "9"; break; case 10: sret = "a"; break; case 11: sret = "b"; BREAK; Case 12: SRet = "C"; Break; Case 13: SRet = "D"; Break; Case 14: SRET = "E"; Break; Case 15: SRet = "f";} Return SRet; PRIVATECIT CMPT;}}