我正在开发一个项目,我必须加密文本文件并通过电子邮件附件将加密文件作为附件发送。另一端的用户可以在提供与我设置的密码相同的密码后下载并解密。
现在,当我下载加密的.txt文件(即邮件附件)并尝试解码时,它会给我一个不良填充异常。但是当我解密原始文件,即通过加密“生成”(未下载)的文件时,它可以正常工作。
以下是我用于加密的代码:
public class EncryptDecrypt
{
public static String Password;
public static FileInputStream fileIn;
public static FileOutputStream fileOut;
public static boolean a = true;
public static void Encrypt(String FileInName, String FileOutName ) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
{
//There are three methods via which u can give a password. First direct input, second random text;
//third given by user during execution of the program.
//Password = "ThisShoouldWork"; // to Use the first method, un-comment this line and comment the rest of the methods.
Password = JOptionPane.showInputDialog("Set The Password"); //To use second method , un-comment this line and comment the rest of the methods.
/*while(a) //to use third method, un-comment this "block" and comment the rest of the methods.
{
String setPassword = JOptionPane.showInputDialog(null, "Enter Your Password");
String confirmPassword = JOptionPane.showInputDialog(null, "Re-Enter Your Password");
if(setPassword.equals(confirmPassword))
{
Password = confirmPassword;
a = false;
}
else
JOptionPane.showMessageDialog(null, "Passwords Dont Match !!");
}*/
fileIn = new FileInputStream(FileInName);
fileOut = new FileOutputStream(FileOutName);
PBEKeySpec keySpec = new PBEKeySpec(Password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
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("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);
fileOut.write(salt);
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = fileIn.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null) fileOut.write(output);
}
byte[] output = cipher.doFinal();
if (output != null) fileOut.write(output);
fileIn.close();
fileOut.flush();
fileOut.close();
}
}
这是我用于De-Cryption的代码:
public class Decrpytion {
public static void Decrypt(String FileInName, String FileOutName ) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
{
FileOutName = FileOutName+"/D.txt";
String password = JOptionPane.showInputDialog("Enter The Password"); //Un comment this line if u are usng either 1st or 2nd method of password for encryption
FileInputStream fileIn = new FileInputStream(FileInName);
FileOutputStream fileOut = new FileOutputStream(FileOutName);
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
byte[] salt = new byte[8];
fileIn.read(salt);
int iterations = 100;
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);
byte[] input = new byte[128];
int bytesRead;
while((bytesRead = fileIn.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
fileOut.write(output);
}
byte[] output = cipher.doFinal();
if (output != null)
fileOut.write(output);
fileIn.close();
fileOut.flush();
fileOut.close();
}
最后但并非最不重要:感谢周日工作:D