密钥加工读取,文件加密解密

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq2090466040/article/details/79096180

明确字节流与字符流的区别

//密钥加工读取,文件加密解密

import java.io.*;



public class MyEncryp {
public static void main(String[] args) {
try {
//密钥
File k = new File("f:/work/key.key");
FileOutputStream keyFos = new FileOutputStream(k);
for(int i=0;i<128;i++){
keyFos.write((int)(Math.random()*128));
}
//读取密钥
int[] key = new int[128];
File keyFile = new File("f:/work/key.key");
FileInputStream keyFis = new FileInputStream(keyFile);
for(int i=0;i<128;i++){
key[i] = keyFis.read();
}
//文件加密
File f = new File("f:/work/text.txt");
File o = new File("f:/work/text1.txt");


FileInputStream textFis = new FileInputStream(f);
FileOutputStream textFos = new FileOutputStream(o);


int length = textFis.available();
for (int i = 0; i < length; i++) {
textFos.write(textFis.read()+key[i%128]);
}
//解密
FileInputStream fis = new FileInputStream(o);
for(int i=0;i<length;i++){
System.out.print((char)(fis.read()-key[i%128]));
}


} catch (Exception e) {
System.out.println(e);//检测异常
}
}
}
展开阅读全文

没有更多推荐了,返回首页