2.3.6文件的加密/解密

Paste_Image.png
import java.io.*;

public class MyEncryp {
    public static void main(String[] args){
        try {
            File inFile = new File("D:/work/test.txt");
            File outFile = new File("D:/test1.txt");
            
            FileInputStream fis = new FileInputStream(inFile);
            FileOutputStream fos = new FileOutputStream(outFile);
            
            int length = fis.available();
            for (int i = 0; i < length; i++) {
                fos.write(fis.read()+100);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
test.txt
abcdefghijklmnopqrstuvwxyz
test1.txt
牌侨墒颂臀闲岩釉罩棕仝圮蒉
Paste_Image.png
import java.io.*;

public class MyDecrypt {
    public static void main(String[] args){
        try {
            File f = new File("D:/test1.txt");
            
            
            FileInputStream fis = new FileInputStream(f);
            
            int length = fis.available();
            for (int i = 0; i < length; i++) {
                System.out.print((char)(fis.read()-100));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
import java.io.*;

public class MyEncryp {
    public static void main(String[] args){
        try {
            File inFile = new File("D:/work/test.txt");
            File outFile = new File("D:/test1.txt");
            
            FileInputStream fis = new FileInputStream(inFile);
            FileOutputStream fos = new FileOutputStream(outFile);
            
            int length = fis.available();
            for (int i = 0; i < length; i++) {
                fos.write(fis.read()+i%100);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

运行结果:

acegikmoqsuwy{}�亙厙墜崗憮
Paste_Image.png
/**
 * 生成密钥文件
 * @author lx
 *
 */
import java.io.*;

public class MyKey {
    public static void main(String[] args){
        try {
            File file = new File("D:/key.key");
            
            FileOutputStream fos = new FileOutputStream(file);
            
            for (int i = 0; i < 128; i++) {
                fos.write((int)(Math.random()*128));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

Paste_Image.png
/**
 * 加密
 */
import java.io.*;

public class MyEncryp {
    public static void main(String[] args){
        try {
            //读密钥文件
            int key[] = new int[128];
            File keyFile = new File("D:/key.key");
                    
            FileInputStream keyFis = new FileInputStream(keyFile);
            
            for (int i = 0; i < 128; i++) {
                key[i] = keyFis.read();
            }
            
            //加密
            File inFile = new File("D:/work/test.txt");
            File outFile = new File("D:/test1.txt");
            
            FileInputStream fis = new FileInputStream(inFile);
            FileOutputStream fos =new FileOutputStream(outFile);
            
            int length = fis.available();
            for (int i = 0; i < length; i++) {
                fos.write(fis.read()+key[i%128]);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
Paste_Image.png
/**
 * 解密
 */
import java.io.*;

public class MyDecrypt {
    public static void main(String[] args){
        try {
            //读密钥文件
            int key[] = new int[128];
            File keyFile = new File("D:/key.key");
            
            FileInputStream keyFis = new FileInputStream(keyFile);
            
            for (int i = 0; i < 128; i++) {
                key[i] = keyFis.read();
            }
            
            //解密
            File f = new File("D:/test1.txt");
            
            
            FileInputStream fis = new FileInputStream(f);
            
            int length = fis.available();
            for (int i = 0; i < length; i++) {
                System.out.print((char)(fis.read()-key[i%128]));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文是自己阅读了网上的高人们的文章之后,实现功能后总结出来的,方便自己方便他人,不喜勿喷 加密解密需要生成公钥、私...
    精神病患者link常阅读 6,793评论 0 0
  • 请让我成长为我自己 从我出生那天起 就不会有人和我一模一样 所以 请让我成长为我自己 不是妈妈企盼的比谁都完美 不...
    柴子恒阅读 148评论 0 2
  • 那年我12岁。 小学六年级下半学期,为了能考上对口的重点中学,我转到一个完全陌生的学校。离家有点远,我要自己走大约...
    linda_816f阅读 337评论 1 5