【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)

大家都在看

  • shell判断文件是否存在

    shell判断文件,目录是否存在或者具有权限 !/bin/sh 3. myPath=”/var/log/httpd/” myFile=”/var…

    Linux 2023年5月28日
    096
  • Linux安装宝塔,OCI8,连接ORACLE数据库

    基础环境 centos7 安装BT宝塔 &#x7F51;&#x5740;&#xFF1A;https://www.bt.cn/download/linux.h…

    Linux 2023年6月6日
    0112
  • Linux中的RCU机制

    什么是RCU? RCU(Read-Copy Update),顾名思义就是读-拷贝-修改,它是基于其原理命名的。对于被RCU保护的共享数据结构,读者不需要获得任何锁就可以访问它,但写…

    Linux 2023年6月13日
    0112
  • CentOS 文本编辑器

    Linux 终端的文本编辑器中,较著名的有:Nano、Vim、Emacs。其它文本编辑器还有 Gedit、Sublime,Atom 等等。 1.1、基础命令 nano:打开 nan…

    Linux 2023年6月8日
    0110
  • 真正的mybatis_redis二级缓存

    网上流传的代码缓存失效存在严重问题。 思路….以后再细说 目前的方案还不够完美,失效力度控制不够细。 主要代码 java;gutter:true; import jav…

    Linux 2023年5月28日
    0119
  • Linux下创建桌面快捷方式

    建立一个文本文件,文件名必须以.desktop结尾,.desktop前面的作为快捷方式的名称 添加如下内容 [Desktop Entry]Encoding=UTF-8Name=Po…

    Linux 2023年6月6日
    092
  • Zookeeper集群搭建及原理

    1 概述 1.1 简介 ZooKeeper 是 Apache 的一个顶级项目,为分布式应用提供高效、高可用的分布式协调服务,提供了诸如数据发布/订阅、负载均衡、命名服务、分布式协调…

    Linux 2023年6月13日
    089
  • 杨辉三角的变形—牛客网

    杨辉三角的变形_牛客题霸_牛客网 (nowcoder.com) #include using namespace std; int main() { //这个树的偶数规律为 -1 …

    Linux 2023年6月13日
    0122
  • Red Hat Enterprise Linux (RHEL) 9 更新了什么,即 Rocky Linux 9 和 AlmaLinux 9 展望

    请访问原文链接:https://sysin.org/blog/rhel-9-vision/,查看最新版。原创作品,转载请保留出处。 作者:gc(at)sysin.org,主页:ww…

    Linux 2023年5月27日
    0108
  • MySQL之外键、表关系及SQL查询关键字

    一、外键 假设现在我们有一张员工信息表,表的字段如下: id # 主键 name # 姓名 age # 年龄 dep_name # 部门名称 dep_desc # 部门描述 单从数…

    Linux 2023年6月14日
    076
  • Linux动静分离与Rewrite

    一、动静分离 1.1 单台机器动静分离 1、创建NFS挂载点(NFS服务端) mkdir /static vim /etc/exports /static 172.16.1.0/2…

    Linux 2023年6月14日
    085
  • MSSQL·CONCAT函数的基础使用

    | 0.47分钟 | 752.8字符 | 1、应用场景 2、基础使用 3、声明与参考资料 | SCscHero | 2022/5/13 PM10:18 | 系列 | 已完成 每一个…

    Linux 2023年6月14日
    086
  • 27. rz与sz上传下载文件

    工作中需要在 Linux 和 Windows 之间传输文件,一般使用xftp等gui工具完成。但是有些第三方linux命令也可以完成上传下载操作 从Linux下载文件到本机 , 在…

    Linux 2023年5月27日
    080
  • gem 更换源及目前能用的源

    1.查看gem源 gem source -l 2.删除源 gem source -r 3.添加源 gem sources –add Original: https://…

    Linux 2023年6月8日
    0107
  • DDoS攻击–TCP攻击概述

    https://blog.csdn.net/qq_34777600/article/details/81945594 posted @2020-12-10 18:07 珠峰之梦 阅…

    Linux 2023年6月7日
    0100
  • 解决 Docker Push Skipped foreign layer 的错误

    引言当Docker推送基于Windows镜像到私有仓库的时候会遇到 Skipped foreign layer的问题。 docker push 192.168.2.30:5000/…

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