您的位置:首页 > 其它

简单的加密解密文件

2009-10-12 23:11 330 查看
      昨天写了一个非常简单的文件加密解密的代码,不带界面,也没有涉及算法。输入文件的时候记得输入名称的时候类似:E://1.txt这样才行。RandomAccessFile类是一个可读也可写的流!

 
import java.io.*;

public class EncryptionDecryption {

static File sourceFile;

public void encryption(int code) throws IOException {
RandomAccessFile fra = new RandomAccessFile(sourceFile, "rw");
int a;
while ((a = fra.read()) != -1) {
fra.seek(fra.getFilePointer() - 1);
a = a + code;
fra.write(a);
}

}

public void decryption(int code) throws IOException {
RandomAccessFile fra = new RandomAccessFile(sourceFile, "rw");
int a;
while ((a = fra.read()) != -1) {
fra.seek(fra.getFilePointer() - 1);
a = a - code;
fra.write(a);
}
}

public static void main(String args[]) throws IOException {
System.out.println("welcome to the encryption and decryption function");
System.out.println("please input the filename");
DataInputStream ins = new DataInputStream(System.in);
String fileName = ins.readLine();
sourceFile = new File(fileName);
System.out.println("please choose :");
System.out.println("1 encryption");
System.out.println("2 decryption");
String choice = ins.readLine();
System.out.println("please input operate code from 1 to 10");
int code = Integer.parseInt(ins.readLine());
EncryptionDecryption ed = new EncryptionDecryption();
if (choice.equals("1")) {
ed.decryption(code);
} else if (choice.equals("2")) {
ed.encryption(code);
} else {
System.out.println("your input is wrong");
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息