Java 给定的最终块未正确填充(BadPaddingException)

Java 给定的最终块未正确填充(BadPaddingException),java,encryption,orm,ftp-client,badpaddingexception,Java,Encryption,Orm,Ftp Client,Badpaddingexception,我正在使用ftpclientforjava,我想在其中加密一个文件并再次解密它 已使用以下代码成功完成加密: String s= EnumerationsQms.ReturnStatus.SUCCESS.getreturnstatus(); int read; FTPConfig objFTP = (FTPConfig)getHibernateTemplate().find(" from FTPConfig where sstatus='A'").get(3); FTPClient ftpc

我正在使用ftpclientforjava,我想在其中加密一个文件并再次解密它

已使用以下代码成功完成加密:

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();
String stQuery=“from FTPConfig,其中sstatus=”+枚举数.MasterStatus_Add+”;
List lstftp=getHibernateTemplate().find(stQuery);
FTPClient ftp=新的FTPClient();
connect(lstftp.get(3.getshost(),lstftp.get(3.getNport());
布尔ftpFile=ftp.login(lstftp.get(3.getsusername(),lstftp.get(3.getspassword());
setFileType(ftp.BINARY文件类型);
ftp.enterLocalPassiveMode();
如果(lstDBfile.size()>0)
{
字符串文件名=nFileImageID+lstDBfile.get(0).getsFileName();
字符串absolutePath1=新文件(“”).getAbsolutePath()+枚举数.UPLOAD\u路径;
字符串uniquefilename=lstDBfile.get(0).getsFileName();
字符串st1=绝对路径1+唯一文件名;
字符串st2=绝对路径1+文件名;
logger.info(“************FTP存储st1*************”+st1);
logger.info(“*********FTP存储文件名*************”+文件名);
stResult=uniquefilename;
文件文件=新文件(st1);
字符串password=“javapapers”;
PBEKeySpec PBEKeySpec=新的PBEKeySpec(password.toCharArray());
SecretKeyFactory SecretKeyFactory=SecretKeyFactory
.getInstance(“pbewithmd5和tripledes”);
SecretKey SecretKey=secretKeyFactory.generateScret(pbeKeySpec);
InputStream InputStream=ftp.retrieveFileStream(文件名);
OutputStream oufil=新文件OutputStream(st2);
int c=0;
而((c=inputStream.read())!=-1)
{
oufil.write(c);
}
oufil.close();
ByteArrayInputStream filein=新的ByteArrayInputStream(oufil.toString().getBytes());
字节[]salt=新字节[8];
读(盐);
PBEParameterSpec PBEParameterSpec=新PBEParameterSpec(salt,100);
Cipher Cipher=Cipher.getInstance(“PBEWithMD5AndTripleDES”);
cipher.init(cipher.DECRYPT_模式,secretKey,pbeParameterSpec);
OutputStream outputStream2=新文件OutputStream(文件);
长启动=System.currentTimeMillis();
字节[]字节数组=新字节[64];
int字节读取=-1;
而((bytesRead=filein.read(bytesrarray))!=-1){
字节[]输出=cipher.update(字节数组,0,字节读取);
if(输出!=null)
outputStream2.写入(输出);
outputStream2.写入(输出);
}
字节[]输出=cipher.doFinal();
if(输出!=null)
outputStream2.写入(输出);
boolean download=ftp.completePendingCommand();
if(下载)
{  
System.out.println(“文件下载成功!”);
logger.info(“使用成功下载的文件”
+(System.currentTimeMillis()-start)+“ms”);
} 
其他的
{  
System.out.println(“下载文件时出错!”);
}  
outputStream2.flush();
outputStream2.close();
inputStream.close();

我在
byte[]output=cipher.doFinal()上得到异常

看看这是否有帮助。它有点笼统,但可能会为您指明正确的方向。

是这门语言?还是其他语言?请在您的问题中添加适当的语言标记。谢谢您的提问,是的,很抱歉没有在前面提到它。这不是我们希望在这里看到的答案。或者让它成为一种语言对问题进行评论或将内容总结到你的答案中。我引用了一个现有的答案。我认为复制文本而不是传递一个引用没有什么意义。为什么不投票以重复的形式结束?如果答案合适,就没有必要复制它。如果答案不完全合适,你仍然应该做出正确的答案或转换为comme新界。