我的问题是这样的 对一个文件进行加密就是将文件的内容读取出来然后进行一个操作
再将这个操作完的字节数组重新写入这个文件 覆盖原内容
解密操作就是 也进行操作 再将这个操作完的字节数组重新写入这个文件 覆盖内容
这样得到的就是文件的原内容
代码如下:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class EndeUtil {
// 加密KEY不能随便改动
static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
.getBytes(); static final int BUFFERLEN = 512;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// debug =false ; String oldFile = new String("D:\\aaa\\aa.txt");
String newFile = new String("D:\\aaa\\aa_en.txt");
String dFile= new String("D:\\aaa\\aa_en_d.txt");
encryptFile(oldFile, "123456");//将原文件加密成新文件 System.out.println("ok");
decryptFile(oldFile);//将加密的文件重新变成一个文件
System.out.println("good");
} catch (Exception e) {
e.printStackTrace();
} }
/**
* 加密方法 将文件内容做加密 加密内容覆盖原内容
* @param file
* @param password
*/
public static void encryptFile(String file,String password)throws Exception{
//String newFile = new String("D:\\aaa\\aa.txt");
FileInputStream in = new FileInputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
//out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
FileOutputStream out = new FileOutputStream(file);
for(int i=0;i<buffer.length;i++){
out.write(buffer[i]);
}
out.close();
} public static void decryptFile(String file)throws Exception{
//String newFile = new String("D:\\aaa\\aa_en.txt");
FileInputStream in = new FileInputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
// out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
FileOutputStream out = new FileOutputStream(file);
for(int i=0;i<buffer.length;i++){
out.write(buffer[i]);
}
out.close();
}
}这个算法是没有问题的 因为如果将文件加密的内容写入另一个文件 就可以 解密时再写入第三个文件
比较第三个文件和原文件 内容是一样的
而我想 全部在一个文件上操作却不可以实现 数据不能完全恢复 不知道这么调整 谁来告诉我
下面再贴出 网上找的源码import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* 加密解密类
*
* @author shaohl
* @version 1.00
*/public class EncryptGen { // static boolean debug =false ; // 加密KEY不能随便改动
static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
.getBytes(); static final int BUFFERLEN = 512; public EncryptGen() {
} /**
* 对文件进行加密
*
* @param String
* oldFile 原始要加密的文件
* @param String
* newFile 加密后的文件
* @return
*/
public static void encryptFile(String oldFile, String newFile)
throws Exception {
FileInputStream in = new FileInputStream(oldFile);
File file = new File(newFile);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
} /**
* 对文件进行解密
*
* @param String
* oldFile 原始要解密的文件
* @param String
* newFile 解密后的文件
* @return
*/
public static void decryptFile(String oldFile, String newFile)
throws Exception {
FileInputStream in = new FileInputStream(oldFile);
File file = new File(newFile);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// debug =false ; String oldFile = new String("D:\\aaa\\w1.doc");
String newFile = new String("D:\\aaa\\w2.doc");
String dFile= new String("D:\\aaa\\w3.doc");
encryptFile(oldFile, newFile);//将原文件加密成新文件 System.out.println("ok");
decryptFile(newFile, dFile);//将加密的文件重新变成一个文件
System.out.println("good");
} catch (Exception e) {
e.printStackTrace();
} }}
再将这个操作完的字节数组重新写入这个文件 覆盖原内容
解密操作就是 也进行操作 再将这个操作完的字节数组重新写入这个文件 覆盖内容
这样得到的就是文件的原内容
代码如下:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class EndeUtil {
// 加密KEY不能随便改动
static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
.getBytes(); static final int BUFFERLEN = 512;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// debug =false ; String oldFile = new String("D:\\aaa\\aa.txt");
String newFile = new String("D:\\aaa\\aa_en.txt");
String dFile= new String("D:\\aaa\\aa_en_d.txt");
encryptFile(oldFile, "123456");//将原文件加密成新文件 System.out.println("ok");
decryptFile(oldFile);//将加密的文件重新变成一个文件
System.out.println("good");
} catch (Exception e) {
e.printStackTrace();
} }
/**
* 加密方法 将文件内容做加密 加密内容覆盖原内容
* @param file
* @param password
*/
public static void encryptFile(String file,String password)throws Exception{
//String newFile = new String("D:\\aaa\\aa.txt");
FileInputStream in = new FileInputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
//out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
FileOutputStream out = new FileOutputStream(file);
for(int i=0;i<buffer.length;i++){
out.write(buffer[i]);
}
out.close();
} public static void decryptFile(String file)throws Exception{
//String newFile = new String("D:\\aaa\\aa_en.txt");
FileInputStream in = new FileInputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
// out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
FileOutputStream out = new FileOutputStream(file);
for(int i=0;i<buffer.length;i++){
out.write(buffer[i]);
}
out.close();
}
}这个算法是没有问题的 因为如果将文件加密的内容写入另一个文件 就可以 解密时再写入第三个文件
比较第三个文件和原文件 内容是一样的
而我想 全部在一个文件上操作却不可以实现 数据不能完全恢复 不知道这么调整 谁来告诉我
下面再贴出 网上找的源码import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* 加密解密类
*
* @author shaohl
* @version 1.00
*/public class EncryptGen { // static boolean debug =false ; // 加密KEY不能随便改动
static final byte[] KEYVALUE = "6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"
.getBytes(); static final int BUFFERLEN = 512; public EncryptGen() {
} /**
* 对文件进行加密
*
* @param String
* oldFile 原始要加密的文件
* @param String
* newFile 加密后的文件
* @return
*/
public static void encryptFile(String oldFile, String newFile)
throws Exception {
FileInputStream in = new FileInputStream(oldFile);
File file = new File(newFile);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
} /**
* 对文件进行解密
*
* @param String
* oldFile 原始要解密的文件
* @param String
* newFile 解密后的文件
* @return
*/
public static void decryptFile(String oldFile, String newFile)
throws Exception {
FileInputStream in = new FileInputStream(oldFile);
File file = new File(newFile);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// debug =false ; String oldFile = new String("D:\\aaa\\w1.doc");
String newFile = new String("D:\\aaa\\w2.doc");
String dFile= new String("D:\\aaa\\w3.doc");
encryptFile(oldFile, newFile);//将原文件加密成新文件 System.out.println("ok");
decryptFile(newFile, dFile);//将加密的文件重新变成一个文件
System.out.println("good");
} catch (Exception e) {
e.printStackTrace();
} }}
解决方案 »
- 收藏 【散分】javase问题
- 求jcreator pro 4.5.010注册码,谢谢
- 关于ArrayList数组输出的一个问题
- 输入数字 如何转化成货币数字
- java怎么调用外部的ppt文件?如果想将ppt内嵌到内容面板上怎么解决?
- [100分]为什么类Student在继承People时老是报如下错误:急等!!!!!!!!!!!!!!!!
- 排序的问题,怎么会多排了几次???!!
- 碰到一题不会做,帮忙!输入字符传入setBackground(Color.white)设定颜色。
- 怎么能让按钮点击时,不出现灰色背景,就象jbuilder中的工具栏那样?
- vb,delphi,java都是不真正的专业和程序员只有C才是,同意者,不同意者说出理由
- 字节数组逐个输出正常,整体输出乱码?
- Java界面布局碰到的一个很诡异的问题,求助各位!!
但是加密的key 有问题了
简单的可以解开 还有一个最最最最复杂的问题 我这个要加密解密的文件时pdf的 读取pdf的字节加密后 不对啊....
4M的内容 加密后变成1KB 明显错误...
怎么pdf 这么特别
这个谁能搞定啊!!!!!
2.加密
是不是有影响?