2015-04-23 29 views
0

我正在使用ftpclient for java在其中我想要加密文件並再次解密它。鑑於最後的塊沒有正確填充(BadPaddingException)

加密做成功使用下面的代碼:

String s= EnumerationsQms.ReturnStatus.SUCCESS.getreturnstatus(); 
int read; 

FTPConfig objFTP = (FTPConfig)getHibernateTemplate().find(" from FTPConfig where 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; 

不過,雖然解密它提供了有關代碼BadPaddingException:

String stQuery="from FTPConfig where 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 successfully !"); 
     logger.info("file downloaded successfully with " 
       + (System.currentTimeMillis() - start) + "ms"); 
    } 
    else 
    { 
     System.out.println("Error in downloading file !"); 
    } 
    outputStream2.flush(); 
    outputStream2.close(); 
    inputStream.close(); 

我得到的異常上byte[] output = cipher.doFinal();

+0

這是[標籤:爪哇]?或者其他一些語言?請爲您的問題添加適當的語言標記。 –

+0

謝謝你的提問,是的,這是java的抱歉,不提前提及它。 –

回答

0

有一個看看When Decrypting Image, gives javax.crypto.BadPaddingException: pad block corrupted Android看看是否有幫助。這有點一般,但可能會指向正確的方向。

+0

這不完全是一個答案,因爲我們喜歡在這裏看到。要麼將其作爲對問題的評論,要麼將其內容概括爲您的答案。 –

+0

我引用了現有的答案。我沒有看到複製文本的意思,而沒有通過參考。 – rossum

+0

爲什麼不投票結束重複?如果答案合適,則不需要複製它。如果它不完全適合,你仍然應該做出正確的答案或轉換爲評論。 –

相關問題