美文网首页
2.3.6文件的加密/解密

2.3.6文件的加密/解密

作者: linhaoyou | 来源:发表于2017-07-12 10:16 被阅读0次
    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
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:2.3.6文件的加密/解密

          本文链接:https://www.haomeiwen.com/subject/rmamhxtx.html