java - Given final block not properly padded(BadPaddingException) -


i using ftpclient java in want encrypt file , decrypt again.

the encryption done using below code:

string s= enumerationsqms.returnstatus.success.getreturnstatus(); int read;  ftpconfig objftp = (ftpconfig)gethibernatetemplate().find(" ftpconfig sstatus='a'").get(3);  ftpclient ftpclient = new ftpclient();  ftpclient.connect(objftp.getshost(),objftp.getnport());  logger.info("objftp.getsip()"+objftp.getsip());   boolean strue = ftpclient.login(objftp.getsusername(),objftp.getspassword());  logger.info("objftp.getsusername()"+objftp.getsusername());  logger.info("objftp.getspassword()"+objftp.getspassword()); ftpclient.enterlocalpassivemode();  ftpclient.setfiletype(ftp.binary_file_type);  if(strue) {     string st =  generalfunctiondaoimpl.getabsolutepath() + objdbfilestorage.getsfilename();      logger.info("file name--->"+st);      file firstlocalfile = new file(st);                string uniquefilename =objdbfilestorage.getnfileimageid() + objdbfilestorage.getsfilename();     logger.info("uniquefilename"+uniquefilename);      inputstream inputstream = new fileinputstream(st);     logger.info("st"+st);      outputstream outputstream = ftpclient.storefilestream(uniquefilename);      string password = "javapapers";        pbekeyspec keyspec = new pbekeyspec(password.tochararray());        secretkeyfactory keyfactory = secretkeyfactory.getinstance("pbewithmd5andtripledes");        secretkey passwordkey = keyfactory.generatesecret(keyspec);      byte[] salt = new byte[8];     random rnd = new random();     rnd.nextbytes(salt);     int iterations = 100;      pbeparameterspec parameterspec = new pbeparameterspec(salt, iterations);      cipher cipher = cipher.getinstance("pbewithmd5andtripledes");     cipher.init(cipher.encrypt_mode, passwordkey, parameterspec);     outputstream.write(salt);      byte[] input = new byte[64];      while ((read = inputstream.read(input)) != -1)      {                            byte[] output = cipher.update(input, 0, read);         if (output != null)             outputstream.write(output);       }         byte[] output = cipher.dofinal();     if (output != null)         outputstream.write(output);      inputstream.close();     outputstream.flush();     outputstream.close();      if(ftpclient.isconnected()){         ftpclient.logout();         ftpclient.disconnect();     } } return s; 

but while decryption gives badpaddingexception on code:

string stquery="from ftpconfig sstatus='"+enumerations.masterstatus_add+"'"; list<ftpconfig> lstftp=gethibernatetemplate().find(stquery);  ftpclient ftp=new ftpclient();  ftp.connect(lstftp.get(3).getshost(),lstftp.get(3).getnport()); boolean ftpfile=ftp.login(lstftp.get(3).getsusername(), lstftp.get(3).getspassword());  ftp.setfiletype(ftp.binary_file_type); ftp.enterlocalpassivemode();   if(lstdbfile.size()>0) {     string filename =nfileimageid+lstdbfile.get(0).getsfilename();       string absolutepath1 = new file("").getabsolutepath() + enumerations.upload_path;     string uniquefilename =  lstdbfile.get(0).getsfilename();     string st1 = absolutepath1 + uniquefilename;     string st2 = absolutepath1 + filename;      logger.info("**********ftp storage st1*************"+st1);      logger.info("**********ftp storage filename *************"+filename);       stresult = uniquefilename;     file file = new file(st1);      string password = "javapapers";     pbekeyspec pbekeyspec = new pbekeyspec(password.tochararray());     secretkeyfactory secretkeyfactory = secretkeyfactory             .getinstance("pbewithmd5andtripledes");     secretkey secretkey = secretkeyfactory.generatesecret(pbekeyspec);      inputstream inputstream = ftp.retrievefilestream(filename);      outputstream oufil= new fileoutputstream(st2);     int c=0;     while((c=inputstream.read())!=-1)     {         oufil.write(c);     }     oufil.close();     bytearrayinputstream filein = new bytearrayinputstream(oufil.tostring().getbytes());       byte[] salt = new byte[8];     filein.read(salt);      pbeparameterspec pbeparameterspec = new pbeparameterspec(salt, 100);      cipher cipher = cipher.getinstance("pbewithmd5andtripledes");       cipher.init(cipher.decrypt_mode, secretkey, pbeparameterspec);      outputstream outputstream2 = new fileoutputstream(file);     long start = system.currenttimemillis();      byte[] bytesarray = new byte[64];     int bytesread = -1;     while ((bytesread = filein.read(bytesarray)) != -1) {          byte[] output = cipher.update(bytesarray, 0, bytesread);         if (output != null)             outputstream2.write(output);         outputstream2.write(output);     }     byte[] output = cipher.dofinal();       if (output != null)         outputstream2.write(output);         boolean download  = ftp.completependingcommand();     if (download)     {           system.out.println("file downloaded !");           logger.info("file downloaded "                 + (system.currenttimemillis() - start) + "ms");     }      else      {           system.out.println("error in downloading file !");       }       outputstream2.flush();     outputstream2.close();     inputstream.close(); 

i exception on byte[] output = cipher.dofinal();

have @ when decrypting image, gives javax.crypto.badpaddingexception: pad block corrupted android see if helps. bit general, may point in right direction.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -