encryption - Exporting/Import Encrypted Code Java -
i trying export encoded text txt file. somewhere in export , import goes wrong. during same application can export , import fine. encode , decode. when close application , run again import encoded text, input changes wasn't, , throws off decryption.
i believe may have cipher being randomly generated every time application run. not know how fix it.
warning. application vary big. i'm sorry that.
package crypt; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.filenotfoundexception; import java.io.printwriter; import java.io.unsupportedencodingexception; import java.security.invalidkeyexception; import java.security.key; import java.security.nosuchalgorithmexception; import java.security.securerandom; import java.util.scanner; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.nosuchpaddingexception; import javax.crypto.spec.secretkeyspec; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.jtextfield; /** * program imports exports encrypts , decrypts text file. * known bugs can't reuse exported , imported files * outside of current application session. * may cause cipher having different random values in place * when restarts program. 2 ciphers never same. * encryption application impossible decrypt. * yet lack of being able reuse same cipher on 2 applications * program useless. * @author qward * */ @suppresswarnings("serial") public class crypt extends jframe { final private cipher cipher = cipher.getinstance("aes"); /** * @throws nosuchalgorithmexception * @throws nosuchpaddingexception */ public crypt() throws nosuchalgorithmexception, nosuchpaddingexception { super("cypt"); setdefaultcloseoperation(jframe.exit_on_close); setsize(600, 600); setlocationrelativeto(null); setresizable(false); // sets 1 cipher encode , decode // initiating components jfilechooser filechooser = new jfilechooser(); filechooser.setcurrentdirectory(new file(system .getproperty("user.home"))); jpanel left = new jpanel(); jpanel right = new jpanel(); jpanel bottom = new jpanel(); jpanel top = new jpanel(); left.setlayout(new borderlayout()); right.setlayout(new borderlayout()); bottom.setlayout(new flowlayout()); top.setlayout(new flowlayout()); jlabel inputlabel = new jlabel("input"); jlabel outputlabel = new jlabel("output"); jbutton generatekey = new jbutton("generate key"); final jtextarea input = new jtextarea(20, 25); final jtextarea output = new jtextarea(20, 25); jscrollpane inputscroll = new jscrollpane(input); jscrollpane outputscroll = new jscrollpane(output); final jtextfield key = new jtextfield(20); jbutton encode = new jbutton("encode"); jbutton decode = new jbutton("decode"); jbutton swap = new jbutton("swap"); jmenubar bar = new jmenubar(); jmenu file = new jmenu("file"); jmenuitem export = export(filechooser, output); jmenuitem imports = imports(filechooser, input); file.add(imports); file.add(export); bar.add(file); // position components bottom.add(generatekey); bottom.add(key); bottom.add(encode); bottom.add(decode); top.add(swap); left.add(inputscroll, borderlayout.center); left.add(inputlabel, borderlayout.north); right.add(outputscroll, borderlayout.center); right.add(outputlabel, borderlayout.north); add(left, borderlayout.west); add(right, borderlayout.east); add(bottom, borderlayout.south); add(top, borderlayout.north); setjmenubar(bar); // add events buttons encode.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { if (key.gettext().length() == 16) { output.settext(encrypt(input.gettext(), new secretkeyspec( key.gettext().getbytes(), "aes"), cipher)); } else { joptionpane.showmessagedialog(null, "please input valid 16 character key"); } } }); generatekey.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { key.settext(generatekey()); } }); decode.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { if (key.gettext().length() == 16) { output.settext(decrypt(input.gettext(), new secretkeyspec( key.gettext().getbytes(), "aes"), cipher)); } else { joptionpane.showmessagedialog(null, "please input valid 16 character key"); } } }); swap.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { string temp = input.gettext(); input.settext(output.gettext()); output.settext(temp); } }); // begin pack(); setvisible(true); } /** * @param filechooser * @param output * @return */ private jmenuitem export(final jfilechooser filechooser, final jtextarea output) { jmenuitem export = new jmenuitem("export"); export.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { int result = filechooser.showopendialog(crypt.this); if (result == jfilechooser.approve_option) { file selectedfile = filechooser.getselectedfile(); if (selectedfile.getname().tolowercase().endswith(".txt")) { printwriter out = null; try { out = new printwriter(selectedfile .getabsolutepath()); out.print(new string(cipher.dofinal(output .gettext().getbytes()))); } catch (filenotfoundexception | illegalblocksizeexception | badpaddingexception e) { out.print(output.gettext()); } { out.close(); } } else { joptionpane.showmessagedialog(null, "please select .txt file"); } } } }); return export; } /** * @param filechooser * @param input * @return */ private jmenuitem imports(final jfilechooser filechooser, final jtextarea input) { jmenuitem imports = new jmenuitem("import"); imports.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { int result = filechooser.showopendialog(crypt.this); if (result == jfilechooser.approve_option) { file selectedfile = filechooser.getselectedfile(); if (selectedfile.getname().tolowercase().endswith(".txt")) { scanner in = null; try { in = new scanner(new file(selectedfile .getabsolutepath())); string str = ""; while (in.hasnextline()) { str += in.nextline() + "\n"; } byte[] encrypted = cipher.dofinal(input.gettext() .getbytes()); input.settext(new string(encrypted, "iso-8859-1")); } catch (exception e) { try { in = new scanner(new file(selectedfile .getabsolutepath())); string str = ""; while (in.hasnextline()) { str += in.nextline() + "\n"; } input.settext(new string(str)); } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } { in.close(); } } else { joptionpane.showmessagedialog(null, "please select .txt file"); } } } }); return imports; } /** * @return */ private string generatekey() { try { securerandom random = new securerandom(); return new string(random.generateseed(16), "iso-8859-1"); } catch (unsupportedencodingexception u) { } return ""; } /** * @param input * @param key * @param cipher * @return */ private string encrypt(string input, key key, cipher cipher) { try { // encrypt text cipher.init(cipher.encrypt_mode, key); byte[] encrypted = cipher.dofinal(input.getbytes()); return new string(encrypted, "iso-8859-1"); } catch (exception e) { e.printstacktrace(); } return ""; } /** * @param input * @param key * @param cipher * @return */ private string decrypt(string input, key key, cipher cipher) { try { // decrypt text cipher.init(cipher.decrypt_mode, key); string decrypted = new string(cipher.dofinal(input .getbytes("iso-8859-1"))); return decrypted; } catch (exception e) { e.printstacktrace(); } return ""; } /** * @param args * @throws nosuchalgorithmexception * @throws nosuchpaddingexception * @throws invalidkeyexception */ public static void main(string[] args) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception { new crypt(); } }
here randomness comes from. may want save
whatever method returns , use on next run of app.
/** * @return */ private string generatekey() { try { securerandom random = new securerandom(); return new string(random.generateseed(16), "iso-8859-1"); } catch (unsupportedencodingexception u) { } return ""; }
so think how rework if want app behave ok
1 run other. also, don't use empty catch blocks ever.
Comments
Post a Comment