java - AES File Encryption/Decryption -
objective: pick file jfilechooser take file pass encryption method, encrypt file encrypted file upload file server via. upload method have or more particular line of code 'uploadedfile = client.uploadfile( "/" + nameof ,dbxwritemode.add(), inputfile.length(),his);'
problem: normal upload works fine without encryption involved when trying encrypt file not go through along "key" not being able put inputfile , server not accept file please see code.
question: how integrate encryption code work appropriately , combining uploadfile method , encryption methods have
working code below has no encryption
public void uploadfile() throws dbxexception, fileloadexception, ioexception, exception { try{ //auth method phonehome(); }catch(ioexception e){ system.out.println("not saving accesstoken"); joptionpane.showmessagedialog(null, "your access information not exist,\n please login"+ "please login clicking 'ok'"); drop(); // run auth method user login } fc = new jfilechooser(); fc.setmultiselectionenabled(true); fc.setfileselectionmode(jfilechooser.files_and_directories); int dialog = fc.showsavedialog(this); if (dialog == jfilechooser.approve_option) { inputfile = fc.getselectedfile(); inputfile.getname(); inputfile.getabsolutefile(); string nameof = inputfile.getname(); system.out.println(" file: " + inputfile); fis = new fileinputstream(inputfile); // file being sent upload dropbox uploadedfile = client.uploadfile( "/" + nameof ,dbxwritemode.add(), inputfile.length(),fis); } communication.append(""); system.out.println("uploaded: " + uploadedfile.tostring()); communication.append("uploaded: " + uploadedfile.tostring()); joptionpane.showmessagedialog(null,"file upload:" + uploadedfile.tostring(), "success!", joptionpane.warning_message); }
methods have encryption need implemented above
private byte[] getkeybytes(final byte[] key) throws exception { byte[] keybytes = new byte[16]; system.arraycopy(key, 0, keybytes, 0, math.min(key.length, keybytes.length)); return keybytes; } public cipher getcipherencrypt(final byte[] key) throws exception { byte[] keybytes = getkeybytes(key); cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); secretkeyspec secretkeyspec = new secretkeyspec(keybytes, "aes"); ivparameterspec ivparameterspec = new ivparameterspec(keybytes); cipher.init(cipher.encrypt_mode, secretkeyspec, ivparameterspec); return cipher; } public void encrypt() throws exception { cipher cipher = getcipherencrypt(key); fileoutputstream fos = null; cipheroutputstream cos = null; fileinputstream fis = null; try { fis = new fileinputstream(inputfile); fos = new fileoutputstream(outputfile); cos = new cipheroutputstream(fos, cipher); byte[] data = new byte[1024]; int read = fis.read(data); while (read != -1) { cos.write(data, 0, read); read = fis.read(data); system.out.println(new string(data, "utf-8").trim()); } cos.flush(); } { cos.close(); fos.close(); fis.close(); } }
ideas putting these guys together?
first check: make original file zip , upload it
if works make encrypted file zip , send via dropbox
Comments
Post a Comment