【java课设+源代码+注释+文档】文件加密

项目文档见我上传的资源

点击这里下载资源

package xyz.slienceme;
/*
 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * @Author slience_me
 * @Time : 2021/5/5  19:36
 * @File : Encrypt.java
 * @Software : IntelliJ IDEA
 */
public class Encrypt extends javax.swing.JFrame {

    /**
     * Creates new form Encrypt
     */
    public Encrypt() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.

     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        savePathTxt = new javax.swing.JTextField();
        savePathBtn = new javax.swing.JButton();
        filePathBtn = new javax.swing.JButton();
        SimEncryBtn = new javax.swing.JButton();
        ComDecryBtn = new javax.swing.JButton();
        SimDecryBtn = new javax.swing.JButton();
        ComEncryBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        filePathTxt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        savePathTxt.setToolTipText("");
        savePathTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePathTxtActionPerformed(evt);
            }
        });

        savePathBtn.setText("保存路径:");
        savePathBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePathBtnActionPerformed(evt);
            }
        });

        filePathBtn.setText("文件路径:");
        filePathBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filePathBtnActionPerformed(evt);
            }
        });

        SimEncryBtn.setText("简单加密");
        SimEncryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimEncryBtnActionPerformed(evt);
            }
        });

        ComDecryBtn.setText("复杂解密");
        ComDecryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComDecryBtnActionPerformed(evt);
            }
        });

        SimDecryBtn.setText("简单解密");
        SimDecryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimDecryBtnActionPerformed(evt);
            }
        });

        ComEncryBtn.setText("复杂加密");
        ComEncryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComEncryBtnActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        jLabel1.setText("文件加密小助手");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(filePathBtn)
                            .addComponent(savePathBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(filePathTxt, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(savePathTxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(209, 209, 209)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(SimEncryBtn)
                        .addGap(27, 27, 27)
                        .addComponent(SimDecryBtn)
                        .addGap(18, 18, 18)
                        .addComponent(ComEncryBtn)
                        .addGap(18, 18, 18)
                        .addComponent(ComDecryBtn)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(savePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(savePathBtn))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(filePathBtn)
                    .addComponent(filePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SimEncryBtn)
                    .addComponent(SimDecryBtn)
                    .addComponent(ComEncryBtn)
                    .addComponent(ComDecryBtn))
                .addContainerGap(89, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    private void savePathTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePathTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_savePathTxtActionPerformed

    private void filePathBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filePathBtnActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();// 文件选择器
        // 设定只能选择到文件夹
        jfc.setFileSelectionMode(0);
        // 此句是打开文件选择器界面的触发语句
        jfc.setCurrentDirectory(new File("D://1Amycode//Java//CourseDesign"));
        int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
            if (state == 1) {
                return;// 撤销则返回
            } else {
                File f = jfc.getSelectedFile();//
                filePathTxt.setText(f.getAbsolutePath());
            }
    }//GEN-LAST:event_filePathBtnActionPerformed

    private void SimEncryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimEncryBtnActionPerformed
        // TODO add your handling code here:

            String newpath = savePathTxt.getText().trim();
            System.out.println("SimEncryBtnActionPerformed=" + newpath);
            String oldpath = filePathTxt.getText().trim();
            System.out.println("SimEncryBtnActionPerformed=" + oldpath);

            if (newpath.equals("")) {
                JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
            } else if (filePathTxt.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
            } else {
                File file = new File(oldpath);
                //文件类型
                String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
                System.out.println("filetype=" + filetype);
                //文件前缀
                String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
                System.out.println("fileprefix=" + fileprefix);
                String newFilepath = newpath + "\\"+ "SimEncrypt" + fileprefix + filetype;
                System.out.println("newFilepath=" + newFilepath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
                    }
                }
//                creatPath(newpath);//检查路径是否存在,不存在创建新路径
                try {
                    SimEncryption(oldpath, newFilepath);
                } catch (IOException ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }

                JOptionPane.showMessageDialog(null, "简单加密完成!", "提示", 2);
            }
    }//GEN-LAST:event_SimEncryBtnActionPerformed

    private void savePathBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePathBtnActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();// 文件选择器
        // 设定只能选择到文件夹
        jfc.setFileSelectionMode(1);
        // 此句是打开文件选择器界面的触发语句
        jfc.setCurrentDirectory(new File("D://1Amycode//Java//CourseDesign"));
        int state = jfc.showOpenDialog(null);
            if (state == 1) {
                return;
            } else {
                File f = jfc.getSelectedFile();// f为选择到的目录
                savePathTxt.setText(f.getAbsolutePath());
            }
    }//GEN-LAST:event_savePathBtnActionPerformed

    private void ComDecryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComDecryBtnActionPerformed

            String newpath = savePathTxt.getText().trim();
            System.out.println("ComEncryBtnActionPerformed=" + newpath);
            String oldpath = filePathTxt.getText().trim();
            System.out.println("ComEncryBtnActionPerformed=" + oldpath);

            if (newpath.equals("")) {
                JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
            } else if (filePathTxt.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
            } else {
                File file = new File(oldpath);
                //文件类型
                String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
                System.out.println("filetype=" + filetype);
                //文件前缀
                String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
                System.out.println("fileprefix=" + fileprefix);
                String newFilepath = newpath + "\\"+ "ComDe_" + fileprefix + filetype;
                System.out.println("newFilepath=" + newFilepath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
                    }
                }
//                creatPath(newpath);//检查路径是否存在,不存在创建新路径
                try {
                    ComDecryption(oldpath, newFilepath);
                } catch (IOException ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }

                JOptionPane.showMessageDialog(null, "复杂解密完成!", "提示", 2);
            }
    }//GEN-LAST:event_ComDecryBtnActionPerformed

    private void SimDecryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimDecryBtnActionPerformed
        // TODO add your handling code here:
        // 存储的位置
        String newpath = savePathTxt.getText().trim();
        System.out.println("SimEncryBtnActionPerformed=" + newpath);
        // 加密的文件
        String oldpath = filePathTxt.getText().trim();
        System.out.println("SimEncryBtnActionPerformed=" + oldpath);

        if (newpath.equals("")) {
                JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
            } else if (filePathTxt.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
            } else {
                File file = new File(oldpath);
                //文件类型
                String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
                System.out.println("filetype=" + filetype);
                //文件前缀
                String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
                System.out.println("fileprefix=" + fileprefix);
                String newFilepath = newpath + "\\"+ "SimDe_" + fileprefix+ filetype;
                System.out.println("newFilepath=" + newFilepath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
                    }
                }
                creatPath(newpath);//检查路径是否存在,不存在创建新路径
                try {
                    SimDecryption(oldpath, newFilepath);
                } catch (IOException ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }
            JOptionPane.showMessageDialog(null, "简单解密完成!", "提示", 2);
        }
    }//GEN-LAST:event_SimDecryBtnActionPerformed

    private void ComEncryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComEncryBtnActionPerformed

        // 存储的位置
        String newpath = savePathTxt.getText().trim();
        System.out.println("ComEncryBtnActionPerformed=" + newpath);
        // 加密的文件
        String oldpath = filePathTxt.getText().trim();
        System.out.println("ComEncryBtnActionPerformed=" + oldpath);

        if (newpath.equals("")) {
                JOptionPane.showMessageDialog(null, "请选择输出路径!", "提示", 2);
            } else if (filePathTxt.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "请选择文件!", "提示", 2);
            } else {
                File file = new File(oldpath);
                //文件类型
                String filetype=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
//                System.out.println("filetype=" + filetype);
                //文件前缀
                String fileprefix=file.getName().substring(0,file.getName().lastIndexOf("."));
//                System.out.println("fileprefix=" + fileprefix);
                String newFilepath = newpath + "\\"+ "ComEncrypt" + fileprefix + filetype;
//                System.out.println("newFilepath=" + newFilepath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        JOptionPane.showMessageDialog(null, "路径不正确!", "提示", 2);
                    }
                }
                creatPath(newpath);//检查路径是否存在,不存在创建新路径
                try {
                    ComEncryption(oldpath, newFilepath);
                } catch (IOException ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }
            JOptionPane.showMessageDialog(null, "复杂加密完成!", "提示", 2);
        }
    }//GEN-LAST:event_ComEncryBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Encrypt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Encrypt().setVisible(true);
            }
        });
    }

    //根据路径处理,如果不存在创建路径
    public void creatPath(String oldpath) {
        String temppath = oldpath.replace(oldpath.substring(oldpath.lastIndexOf("\\"), oldpath.length()), "");
        File file = new File(temppath);
        if (!file.exists()) {
            file.mkdir();
            System.out.println("路径不存在,但是已经成功创建了" + temppath);
        }
    }

    public void SimEncryption(String oldpath, String newpath) throws IOException {
        // 文件路径
        File file = new File(oldpath);
        // 输入流
        FileInputStream inputFile = new FileInputStream(file);
        // 输出流
        FileWriter fw = new FileWriter(newpath);
        byte[] buffer = new byte[(int) file.length()];
        // read(byte[] b)方法:从此输入流中将最多b.length个字节的数据读入一个字节数组中
        inputFile.read(buffer);
        inputFile.close();
        for (int i = 0; i < buffer.length; i++) {
//            System.out.println(buffer[i]);
            buffer[i] += 3;
            fw.write(buffer[i]);
        }
        fw.close();
    }

    public void SimDecryption(String oldpath, String newpath) throws IOException {
        // 文件路径
        File file = new File(oldpath);
        // 输入流
        FileInputStream inputFile = new FileInputStream(file);
        // 输出流
        FileWriter fw = new FileWriter(newpath);
        byte[] buffer = new byte[(int) file.length()];
        // read(byte[] b)方法:从此输入流中将最多b.length个字节的数据读入一个字节数组中
        inputFile.read(buffer);
        inputFile.close();
        for (int i = 0; i < buffer.length; i++) {
//            System.out.println(buffer[i]);
            buffer[i] -= 3;
            fw.write(buffer[i]);
        }
        fw.close();
    }

    public void ComEncryption(String oldpath, String newpath) throws IOException {
        // 文件路径
        File file = new File(oldpath);
        // 输入流
        FileInputStream inputFile = new FileInputStream(file);
        // 输出流
        FileWriter fw = new FileWriter(newpath);
        byte[] buffer = new byte[(int) file.length()];
        // read(byte[] b)方法:从此输入流中将最多b.length个字节的数据读入一个字节数组中
        inputFile.read(buffer);
        inputFile.close();
        String string = new String(buffer);
        String result = "";
        System.out.println("string="+ string.toString());

        for (int i = 0; i < string.length(); i++) {
            char x = string.toString().charAt(i);
            if(x >= 'a' && x = 'n' && x = 'A' && x = 'N' && x ='0' && x='6' && x

Original: https://www.cnblogs.com/Slience-me/p/16341903.html
Author: Slience_me
Title: 【java课设+源代码+注释+文档】文件加密

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/585855/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 提升git clone速度

    查看github.global.ssl.fastly.Net的ip地址 nslookup github.global.ssl.fastly.Net Original: https:…

    Linux 2023年6月7日
    0145
  • fastdfs单节点部署

    安装依赖 ​ yum -y install gcc gcc-c++ libevent 安装libfastcommon 下载地址: https://github.com/happyf…

    Linux 2023年6月8日
    0127
  • Docker centos7,宝塔

    拉取一个centos镜像 docker pull centos:centos7 运行一个容器 docker run -i -t -d –restart=always –name…

    Linux 2023年6月6日
    0117
  • 用powershell脚本,检测cpu100%,并触发sqlserver主备切换。

    我为园子设计的脚本大致如下:1慢检测:任务计划每分钟运行。发现cpu高于95%后,触发2。2快检测:每隔5秒钟运行,发现cpu高于95%,连续6次后,触发3。3报警n次。超出5分钟…

    Linux 2023年6月14日
    0132
  • CH9344 Windows驱动安装与GPIO使用教程

    USB 转四串口芯片 CH9344 用于为 USB 主机扩展 4 路高速异步串口,支持串口波特率高达 12Mbps。芯片内部高度集成,外围精简,提供 VIO 电源引脚,部分串口 I…

    Linux 2023年6月7日
    0115
  • HTTP状态码1XX深入理解

    前段时间看了《御赐小仵作》,里面有很多细节很有心。看了一些评论都是:终于在剧里能够看到真正在搞事业、发了工资第一时间还钱的正常人了。我印象比较深的是王府才能吃上的葡萄。觉得非常合理…

    Linux 2023年6月13日
    0119
  • redis导致的错误错误

    ==========双预防系统启动成功========== 14:42:39.821 [http-nio-9217-exec-1] INFO o.a.c.c.C.[.[.[/] -…

    Linux 2023年5月28日
    0179
  • springboot整合redis的发布订阅模式

    所谓发布订阅模式,就是发布了消息丢到通道中,消费者进行消费 看代码: 1 首先需要配置这个连接工厂 ,这地方可能有人有疑问,为什么需要单独做一个类呢,是由与这个连接工厂其他地方需要…

    Linux 2023年5月28日
    0128
  • 读软件测试艺术感悟

    断断续续的看,总觉得好像是漏掉了什么,而且一直 想着还有什么事情未做,却静不下心来去细品 开篇用自测评价测试,举例三角形例子写出等价类边界值的方法给测试人员举一个例子,更好的使测试…

    Linux 2023年6月8日
    0123
  • Dockerfile

    Docker可以通过Dockerfile构建镜像。Dockerfile是一个文本文档,它包含用户可以在命令行上调用的所有命令来组装镜像。使用 docker build用户可以创建一…

    Linux 2023年6月13日
    0106
  • 利用numpy实现list降维

    python读取数据库得到的事一个类似二维数组的list,有时候需要降维操作,numpy提供一个很有用的函数,可以直接使用 import numpy as np a = np.ar…

    Linux 2023年6月14日
    0155
  • docker-部署jumpserver

    Docker 部署 jumpserver 堡垒机 容器部署 jumpserver-1.4.10 服务端 #最好单一个节点 容器运行Mysql 5.6.46 #myql , redi…

    Linux 2023年6月14日
    0122
  • 【滚动更新】C++ 八股文选集(没代码,纯应试)

    1,变量的声明和定义有什么区别。 声明不分配内存,定义分配内存。变量只能被定义一次,但可以被声明多次。 2,#ifdef、#else、#endif和#ifndef的作用。 预处理条…

    Linux 2023年6月13日
    0121
  • tomcat 9 搭建文件服务器 失败

    场景 服务器上某个目录,想开放给别人浏览权限,图省事儿用Python开了个SimpleHTTPServer,但总是断断续续的,没太找到原因。 想到有tomcat,就搜了一下用tom…

    Linux 2023年6月8日
    0117
  • SpringBoot学习笔记——Redis Template

    Springboot可以通过redis template和redis进行交互,使用方法如下 可以参考这个系列的文章: 【快学springboot】11.整合redis实现sessi…

    Linux 2023年5月28日
    0126
  • 深入理解linux内核-内存寻址

    逻辑地址:由一个段和偏移量组成的地址线性地址(虚拟地址):物理地址:CPU的物理地址线相对应的地址32或36位 多处理器系统中每个CPU对应一个GDT 局部线程存储:用于线程内部的…

    Linux 2023年6月6日
    099
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球