Encrytservice
PackageCom.my.service;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream; Public classEncryptservice {//default key Path Private StaticString Default_key_url = ".//key"; //Temporary file path Private StaticString Default_temp_url = ".//temp"; //Read Key Private intKey[] =New int[128]; Private voidReadKey () {File keyfile=NewFile (Default_key_url); Try{FileInputStream Localkey=NewFileInputStream (keyfile); for(inti = 0; I < 128; ++i) {Key[i]=Localkey.read (); } } Catch(Exception e) {e.printstacktrace (); } } //generate a new key Public voidMakekey () {Try{File keyfile=NewFile (Default_key_url); FileOutputStream Fos=NewFileOutputStream (keyfile); for(inti = 0; I < 128; ++i) {fos.write (int) (Math.random () *128)); } readKey (); Fos.close (); } Catch(Exception e) {e.printstacktrace (); } } //Encrypting Files Public voidencryptfile (file file) {Try{FileInputStream fis=Newfileinputstream (file); FileOutputStream Fos=NewFileOutputStream (NewFile (Default_temp_url)); intLength =fis.available (); for(inti = 0; i < length; ++i) {fos.write (Fis.read ()+ key[i%128]); } fis.close (); Fos.close (); FileInputStream FileInputStream=NewFileInputStream (NewFile (Default_temp_url)); FileOutputStream FileOutputStream=Newfileoutputstream (file); intLength2 =fileinputstream.available (); for(inti = 0; i < length2; ++i) {fileoutputstream.write (Fileinputstream.read ()); } fileinputstream.close (); Fileoutputstream.close (); } Catch(Exception e) {e.printstacktrace (); } } //Decrypt Files Public voiddecryptfile (file file) {Try{FileInputStream fis=Newfileinputstream (file); FileOutputStream Fos=NewFileOutputStream (NewFile (Default_temp_url)); intLength =fis.available (); for(inti = 0; i < length; ++i) {fos.write (Fis.read ()-key[i%128]); } fis.close (); Fos.close (); FileInputStream FileInputStream=NewFileInputStream (NewFile (Default_temp_url)); FileOutputStream FileOutputStream=Newfileoutputstream (file); intLength2 =fileinputstream.available (); for(inti = 0; i < length2; ++i) {fileoutputstream.write (Fileinputstream.read ()); } fileinputstream.close (); Fileoutputstream.close (); } Catch(Exception e) {e.printstacktrace (); } }}
Main
PackageCom.my.ui;ImportCom.my.service.EncryptService;Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.File; Public classMainextendsJFrameImplementsActionListener {PrivateEncryptservice Encryptservice =NewEncryptservice (); //Set Default Size Private Static Final intDefault_width = 396; Private Static Final intDefault_height = 145; //Components PrivateJFileChooser Chooser; PrivateJButton Buttonencrypt; PrivateJButton Buttondecrypt; PrivateJButton Buttonmakekey; JTextField FileText; JTextField Keytext; //file path PrivateString FilePath; PrivateString KeyPath; //Initialize encrypted pages PublicMain () {Settitle ("File encryption Program"); Setdefaultcloseoperation (Jframe.exit_on_close); SetSize (Default_width, default_height); Setresizable (false); Setlocationrelativeto (NULL); JPanel Panuser=NewJPanel (); //Creating ComponentsFileText =NewJTextField (); Filetext.seteditable (false); Keytext=NewJTextField (); Keytext.seteditable (false); JButton Btnfile=NewJButton ("....")); Btnfile.setfocuspainted (false); JButton Btnkey=NewJButton ("..."); Btnkey.setfocuspainted (false); Btnkey.setenabled (false); //LayoutPanuser.setlayout (NewGridLayout (2, 3)); Panuser.add (NewJLabel ("Source File path:")); Panuser.add (FileText); Panuser.add (Btnfile); Panuser.add (NewJLabel ("Key path:")); Panuser.add (Keytext); Panuser.add (Btnkey); Buttonencrypt=NewJButton ("Encrypt"); Buttonencrypt.setfocuspainted (false); Buttondecrypt=NewJButton ("Decrypt"); Buttondecrypt.setfocuspainted (false); Buttonmakekey=NewJButton ("Generate New Key"); Buttonmakekey.setfocuspainted (false); JPanel panbtn=NewJPanel (); Panbtn.setlayout (NewFlowLayout ()); Panbtn.add (Buttonencrypt); Panbtn.add (Buttondecrypt); Panbtn.add (Buttonmakekey); SetLayout (NewBorderLayout ()); Add (Panuser, borderlayout.center); Add (panbtn, Borderlayout.south); //registering for event monitoringBtnfile.addactionlistener ( This); Btnkey.addactionlistener ( This); Buttonmakekey.addactionlistener ( This); Buttonencrypt.addactionlistener ( This); Buttondecrypt.addactionlistener ( This); Chooser=NewJFileChooser (); Chooser.setcurrentdirectory (NewFile (".")); } Public Static voidMain (string[] args) {JFrame frame=NewMain (); Frame.setvisible (true); } @Override Public voidactionperformed (ActionEvent e) {if(E.getactioncommand (). Equals ("...."))) { intresult = Chooser.showopendialog (NULL); if(Result = =jfilechooser.approve_option) {FilePath=chooser.getselectedfile (). GetPath (); Filetext.settext (FilePath); } } if(E.getactioncommand (). Equals ("...")) { intresult = Chooser.showopendialog (NULL); if(Result = =jfilechooser.approve_option) {KeyPath=chooser.getselectedfile (). GetPath (); Keytext.settext (KeyPath); } } if(E.getactioncommand (). Equals ("Encryption") {encryptservice.encryptfile (NewFile (FilePath)); System.out.println ("Encryption succeeded"); } if(E.getactioncommand (). Equals ("decryption") {encryptservice.decryptfile (NewFile (FilePath)); System.out.println ("Decryption succeeded"); } if(E.getactioncommand (). Equals ("Generate new Key") ) {Encryptservice.makekey (); Keytext.settext (NewFile (""). GetAbsolutePath () + "\\KEY"); System.out.println ("Successful generation of new keys"); } }}
"Java" Swing+io stream implements a simple file encryption program