我正在使用 java 的 ftpclient,我想在其中加密一个文件并再次解密。
使用以下代码成功完成加密:
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();
看看解密
图像时,给出了javax.crypto.BadPaddingException:pad块损坏的Android,看看是否有帮助。 这有点笼统,但可能会为您指明正确的方向。