找回密码
 立即注册
首页 编程领域 编程板块 使用I/O流实现文件加密小工具

Java 使用I/O流实现文件加密小工具

2023-3-2 16:25:37 评论(0)
代码展示





  1. package com.xy;


  2. import java.io.*;
  3. import java.util.Objects;
  4. import java.util.Scanner;

  5. /**
  6. * @author 谢阳
  7. * @version 1.8.0_131
  8. * @date 2023/1/15 23:07
  9. * @description
  10. */
  11. public class FileEncryptionApp {
  12.     private static final byte[] IS_CODED = new byte[4]; // 默认解密校验码
  13.     private static Integer mode; // 模式选择 1 加密 2解密
  14.     private static Scanner scanner = new Scanner(System.in);
  15.     private static String destName;

  16.     static {
  17.         IS_CODED[0] = 2;
  18.         IS_CODED[1] = 0;
  19.         IS_CODED[2] = 2;
  20.         IS_CODED[3] = 3;
  21.     }

  22.     public static void main(String[] args) throws IOException {
  23.         showMenu();
  24.     }

  25.     /**
  26.      * 菜单展示
  27.      *
  28.      * @throws IOException
  29.      */
  30.     private static void showMenu() throws IOException {
  31.         boolean loop = true;
  32.         while (loop) {
  33.             System.out.println("----------------文件加密、解密小程序---------------");
  34.             System.out.println("\t\t\t\t\t1 文件加密");
  35.             System.out.println("\t\t\t\t\t2 文件解密");
  36.             System.out.println("\t\t\t\t\t3 退出");
  37.             System.out.println("-----------------------------------------------");
  38.             System.out.print("请输入您的选择:");
  39.             char key = scanner.next().charAt(0);
  40.             switch (key) {
  41.                 case '1':
  42.                     System.out.print("请输入需加密文件路径:");
  43.                     String enCodePath = scanner.next();
  44.                     System.out.print("请输入文件的加密密码:");
  45.                     String enCodePassword = scanner.next();
  46.                     System.out.println(enCode(enCodePath, enCodePassword) ? "加密成功,文件路径为:" + destName : "加密失败");
  47.                     break;
  48.                 case '2':
  49.                     System.out.print("请输入需解密文件路径:");
  50.                     String unCodePath = scanner.next();
  51.                     System.out.print("请输入文件的解密密码:");
  52.                     String unCodePassword = scanner.next();
  53.                     System.out.println(unCode(unCodePath, unCodePassword) ? "解密成功,文件路径为:" + destName : "解密失败");
  54.                     break;
  55.                 case '3':
  56.                     System.out.println("退出程序成功,欢迎您的再次使用~");
  57.                     loop = false;
  58.                     break;
  59.                 default:
  60.                     System.out.println("您的输入有误,请重新输入:)");
  61.             }
  62.         }


  63.     }


  64.     /**
  65.      * 文件加密
  66.      *
  67.      * @param path     文件路径
  68.      * @param password 加密文件密钥
  69.      * @return 是否加密成功 (true/false)
  70.      */
  71.     private static boolean enCode(String path, String password) throws IOException {

  72.         if (!checkPathAndPassword(path, password)) {
  73.             return false;
  74.         }

  75.         // 加密后的文件名
  76.         destName = path.substring(0, path.lastIndexOf(".")) + "-coded" + path.substring(path.lastIndexOf("."));

  77.         try (
  78.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
  79.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destName))
  80.         ) {
  81.             bos.write(IS_CODED);// 写入默认加密密钥
  82.             byte[] passwordBytes = password.getBytes();
  83.             for (int i = 0; i < passwordBytes.length; i++) {
  84.                 passwordBytes[i] ^= password.hashCode();
  85.             }
  86.             bos.write(passwordBytes); // 写入用户加密密钥

  87.             byte[] buf = new byte[1024];
  88.             int len;
  89.             int keyHash = password.hashCode(); // 加密hash值
  90.             long length = new File(path).length() % 10; // 文件长度
  91.             while ((len = bis.read(buf)) != -1) {
  92.                 // 加密
  93.                 for (int i = 0; i < len; i++) {
  94.                     //buf[i] ^= keyHash;
  95.                     buf[i] = (byte) ((buf[i] ^ keyHash) + length);
  96.                 }
  97.                 bos.write(buf, 0, len);
  98.             }
  99.         }

  100.         return true;
  101.     }

  102.     /**
  103.      * 文件解密
  104.      *
  105.      * @param path     文件路径
  106.      * @param password 解密文件密钥
  107.      * @return 是否解密成功 (true/false)
  108.      */
  109.     private static boolean unCode(String path, String password) throws IOException {
  110.         if (!checkPathAndPassword(path, password)) {
  111.             return false;
  112.         }

  113.         if (new File(path).length() < IS_CODED.length + password.getBytes().length) {
  114.             System.out.println("该文件不属于加密文件");
  115.             return false;
  116.         }

  117.         // 解密后的文件名
  118.         destName = path.substring(0, path.lastIndexOf(".")) + "-unCode" + path.substring(path.lastIndexOf("."));

  119.         try (
  120.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
  121.         ) {
  122.             // 验证默认密钥和用户密钥是否正确
  123.             if (!checkCodeAndPassword(bis, password)) {
  124.                 return false;
  125.             }


  126.             byte[] buf = new byte[1024];
  127.             int len;
  128.             int keyHash = password.hashCode(); // 解密hash值
  129.             // 长度为上一次文件长度
  130.             long length = (new File(path).length() - password.getBytes().length - IS_CODED.length) % 10;

  131.             try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destName))) {
  132.                 while ((len = bis.read(buf)) != -1) {
  133.                     // 解密
  134.                     for (int i = 0; i < len; i++) {
  135.                         //buf[i] ^= keyHash;
  136.                         buf[i] = (byte) ((buf[i] - length) ^ keyHash);
  137.                     }

  138.                     bos.write(buf, 0, len);
  139.                 }
  140.             }

  141.         }
  142.         return true;
  143.     }


  144.     /**
  145.      * 验证默认密钥和用户密钥是否正确
  146.      *
  147.      * @param bis
  148.      * @param password 用户密码
  149.      * @return ture/false
  150.      */
  151.     private static boolean checkCodeAndPassword(BufferedInputStream bis, String password) throws IOException {
  152.         byte[] codedKey = new byte[IS_CODED.length]; // 默认加密密钥
  153.         bis.read(codedKey);

  154.         // 验证默认密钥
  155.         for (int i = 0; i < IS_CODED.length; i++) {
  156.             if (codedKey[i] != IS_CODED[i]) {
  157.                 System.out.println("该文件不属于加密文件");
  158.                 return false;
  159.             }
  160.         }

  161.         // 验证用户密钥
  162.         byte[] passwordBytes = password.getBytes();
  163.         byte[] pas = new byte[passwordBytes.length];
  164.         bis.read(pas);
  165.         for (int i = 0; i < passwordBytes.length; i++) {
  166.             if (pas[i] != (byte) (passwordBytes[i] ^ password.hashCode())) {
  167.                 System.out.println("文件解密密码错误");
  168.                 return false;
  169.             }
  170.         }

  171.         return true;
  172.     }

  173.     /**
  174.      * 文件、文件路径、密钥检测
  175.      *
  176.      * @param path     文件路径
  177.      * @param password 密钥
  178.      * @return 文件是否存在(true/false)
  179.      */
  180.     private static boolean checkPathAndPassword(String path, String password) {
  181.         if (Objects.isNull(path)) {
  182.             System.out.println("文件路径不能为空");
  183.             return false;
  184.         }

  185.         if (Objects.isNull(password)) {
  186.             System.out.println("用户密钥不能为空");
  187.             return false;
  188.         }

  189.         File file = new File(path);
  190.         if (!file.exists()) {
  191.             System.out.println("该路径不在在,请检查文件路径是否存在");
  192.             return false;
  193.         }
  194.         if (!file.isFile()) {
  195.             System.out.println("该路径不是文件,请输入文件名");
  196.             return false;
  197.         }

  198.         if (!file.getName().contains(".")) {
  199.             System.out.println("不知此该文件格式");
  200.             return false;
  201.         }

  202.         return true;
  203.     }

  204. }

复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

使用道具 举报

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们(3513994353@qq.com)。
您需要登录后才可以回帖 登录 | 立即注册
楼主
东北老哥谈JAVA

关注0

粉丝0

帖子9

最新动态