Java File Encryption AES -


problem: upload method working problem i'm presenting **encrypt(inputfile, new file("users/myname/desktop/example.txt"),key);** instead of writing desktop want stored file or var can use can upload dropbox server. not know how code part , not know put execute appropriately.

    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(file in, file output, byte[] key) throws exception {              cipher cipher = getcipherencrypt(key);             fileoutputstream fos = null;             cipheroutputstream cos = null;             fileinputstream fis = null;             try {                 fis = new fileinputstream(in);                 fos = new fileoutputstream(output);                 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();                 system.out.println("name"+ output);               } {                 system.out.println("performed encrypt method closing streams");                 cos.close();                 fos.close();                 fis.close();             }         }         // working upload method dropbox cloud     public void uploadfile () throws dbxexception, ioexception, fileloadexception, nosuchalgorithmexception, invalidkeyexception, nosuchpaddingexception, illegalblocksizeexception, badpaddingexception, invalidkeyspecexception, invalidparameterspecexception, classnotfoundexception, nosuchproviderexception, invalidalgorithmparameterexception, unsupportedaudiofileexception, lineunavailableexception, interruptedexception, shortbufferexception, exception {      // autheticate if there accesstoken here if not prompt login activating drop method re-auth..     try{       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     }      // user picks file upload jfilechooser      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);       /* calling out inputfile out jfilechooser , passing         our method , encrytping , outputing it.         */       encrypt(inputfile, new  file("users/myname/desktop/example.txt"),key);        file selected = new file ("?");     inputstream = new fileinputstream (selected);     uploadedfile = client.uploadfile( "/" +selected ,dbxwritemode.add(), inputfile.length(), inputstream);      /* original works without encryption      file selectedfile = new file(nameof+inputfile);     inputstream = new fileinputstream(inputfile);      */       // original  uploadedfile = client.uploadfile( "/" +selectedfile ,dbxwritemode.add(), inputfile.length(), inputstream);        settitle("uploading file..");     system.out.println("uploaded: " + uploadedfile.tostring());     joptionpane.showmessagedialog(null,"file upload:" + uploadedfile.tostring(),             "success!", joptionpane.warning_message);     }     } 

solved... on complicating didn't need complicated furthermore , have settled more simplicit way.

original:    encrypt(inputfile, new  file("users/myname/desktop/example.txt"),key);      working:    encrypt(inputfile, new  file(inputfile.getname),key); 

eazy peezy... if has other suggestions hear them, thanks.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -