des解密问题:Given final block not properly padded
我用字节方式读取文件,进行字节数组加密并写入文件,但是从文件读取字节后解密时就出现“Given final block not properly padded”。我知道是最后一个字节数组没有装满。但是怎么解决呢?网上有说用Byte64来做,但是那样就把字节转换为了字符串。而且我试了一下,也没有成功。我上网搜了很多都没有解决,希望大家帮帮我。谢谢。
主要代码如下:
//加密
public byte[] createEncyptor(byte[] buff){
try{
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
}
}
//解密
public byte[] createDecryptor(byte[] buff){
try{
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
}
}
//主程序
public static void main(String [] args){
DES d = new DES();//这是我的主类,这几个函数就在这个类里面
byte tempbytes[] = new byte[1024];//一次读取的字节就放在这里面
int byteread = 0;这是记录读取的字节数量
String fileName;
try {
FileInputStream in=new FileInputStream("D:\\王袁艳\\12.txt");
FileOutputStream out = new FileOutputStream("D:\\王袁艳\\121212.txt");
//读取多个字节到数组中,byteread为一次读取的字节数
/**
* 加密过程
*/
while((byteread=in.read(tempbytes))!=-1){
out.write(d.createEncyptor(tempbytes),0,byteread);
}
System.out.println("文件加密完毕");
/**
* 解密过程
*/
in=new FileInputStream("D:\\王袁艳\\121212.txt");
out = new FileOutputStream("D:\\王袁艳\\1212129999.txt");
while((byteread=in.read(tempbytes))!=-1){
out.write(d.createDecryptor(tempbytes),0,byteread);
}
System.out.println("文件解密完毕");
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这个程序只能加密不能解密,麻烦有谁能帮我解决一下,真的很谢谢你们!