请教java.io方面文件加密解密的问题

Jeroki 2005-11-03 02:49:32

请教java.io方面文件加密解密的问题:

我用ResFileCipher类对一些doc,avi,rm,jpg等各类文件加密后,rm,jpg能正确显示而
doc,avi不能显示,请问其中原因在哪,是我的这个加密方法有误吗?

其中ResFileCipher的change方法是将文件的头4096字节换位,解密同理。

如果大家有更好的解决方法,敬请提出。

请各位大侠给予指点,谢谢!


package com. xxx.tools;

import java.io.*;
import java.util.*;


public class ResFileCipher {
/**字节换位加密*/
public static byte[] change(byte source[], int start,int count)
{
if(source == null)
return null;
byte rs[] = new byte[source.length];
for(int i = start; i < count; i++){
rs[count - 1 - i] = source[i];
}

for(int i=count;i<source.length;i++) {
rs[i]=source[i];
}
return rs;
}
/**加密文件*/
public static void encrypt(String file1, String file2) {

try {

File f1 = new File(file1);
File f2 = new File(file2);

FileInputStream fi = new FileInputStream(f1);
FileOutputStream fo = new FileOutputStream(f2);
// 创建一个BufferedInputStream:
BufferedInputStream bi = new BufferedInputStream(fi);
BufferedOutputStream bo = new BufferedOutputStream(fo);

byte[] buff = new byte[4096];
int bytesRead;

while (-1 != (bytesRead = (bi.read(buff, 0, buff.length)))) {
buff = ResFileCipher.change(buff, 0, 4096);
bo.write(buff, 0, bytesRead);

}

bi.close();
bo.close();


System.out.println(file1 + "处理成功");

fi.close();
fo.close();

//f2.renameTo(f1);
//f1.delete();


} catch (Exception e) {
}
}
/**加密文件夹*/
public static void encryptPath(String path,String action) {

String file1 = "";
String file2 = "";

if (new File(path).isDirectory()) {
File[] fs = new File(path).listFiles();
for (int i = 0; i < fs.length; i++) {

file1 = fs[i].getAbsoluteFile().toString();
//如果有子目录,继续执行
if (fs[i].isDirectory()) {
ResFileCipher.encryptPath(fs[i].getAbsolutePath(),action);
}
//执行加密
if ("ec".equals(action) && file1.indexOf(".ec") == -1 && file1.indexOf(".dc") == -1) {
file2 = fs[i].getAbsoluteFile().toString() + ".ec";
ResFileCipher.encrypt(file1, file2);
fs[i].delete();//删除所有源文件
}
//执行解密
if ("dc".equals(action) && file1.indexOf(".ec") != -1 && file1.indexOf(".dc") == -1) {
file2 = fs[i].getAbsoluteFile().toString().replaceAll(".ec", ".dc");
ResFileCipher.encrypt(file1, file2);
fs[i].delete();//删除所有.ec文件

}
//解密.DC文件
if ("ec".equals(action) && file1.indexOf(".ec") == -1 && file1.indexOf(".dc") != -1) {
file2 = fs[i].getAbsoluteFile().toString().replaceAll(".dc", ".ec");
ResFileCipher.encrypt(file1, file2);
fs[i].delete();//删除所有.ec文件

}


}
}

}



}

ResFileDecrypter.java
=============================
package com.xxx.tools;

import java.util.*;

public class ResFileDecrypter {
public static void main(String[] args) {
String path = "";
if ("".equals(args[0].toString())) {
path = "D:\\fodder";
}
else {
path = args[0];

}

Calendar cal1 = Calendar.getInstance();
System.out.println("[" + cal1.getTime() + "]开始解密媒体文件.....");
ResFileCipher.encryptPath(path, "dc");
Calendar cal2 = Calendar.getInstance();
System.out.println("[" + cal2.getTime() + "]解密媒体文件全部结束!");
}
}

ResFileEncrypter.java
============================
package com.xxx.tools;

import java.io.*;
import java.util.*;



public class ResFileEncrypter {

public static void main(String[] args) {

String path = "";
if("".equals(args[0].toString())) {
path="D:\\fodder";
}
else {
path=args[0];

}
Calendar cal1=Calendar.getInstance();
System.out.println("["+cal1.getTime()+"]开始加密媒体文件.....");

ResFileCipher.encryptPath(path,"ec");

Calendar cal2=Calendar.getInstance();
System.out.println("["+cal2.getTime()+"]加密媒体文件全部结束!");


}

}

...全文
238 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
treeroot 2005-11-03
  • 打赏
  • 举报
回复
/**字节换位加密*/
public static byte[] change(byte source[], int start,int count)
{
if(source == null)
return null;
byte rs[] = new byte[source.length];
for(int i = start; i < count; i++){
rs[count - 1 - i] = source[i];
//should be rs[count-1-i+start]=source[i];
}

for(int i=count;i<source.length;i++) {
rs[i]=source[i];
}
return rs;
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧