java - 在Java中解密文件并将其导出到文件而不进入无限循环?

标签 java encryption fileinputstream

如果您有多个用户和密码,如何在 java 中解密文件并将其导出到文件,而不必陷入无限循环?这是我的代码,最后是我的测试文件:

import java.io.*;
import java.security.*;
import java.util.ArrayList;
import javax.crypto.*;

public class Checker {
    private ArrayList<String> usersList = new ArrayList<String>();
    private ArrayList<String> passwordList = new ArrayList<String>();
    private Cipher cipher = null;
    private KeyGenerator keyGen = null;
    private Key key = null;
    private PrintStream output = System.out;
    private FileOutputStream fos = null;
    Checker() {
        try {
            cipher = Cipher.getInstance("AES");
            keyGen = KeyGenerator.getInstance("AES");
            key = keyGen.generateKey();
            output = new PrintStream(new FileOutputStream("data.txt"), true);
            fos = new FileOutputStream(new File("data.txt"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void check() {
        try {
            CipherInputStream cipherIn = new CipherInputStream(new FileInputStream(new File("data.txt")), cipher);
            cipher.init(Cipher.DECRYPT_MODE, key);

            int i; 
            while((i = cipherIn.read()) != -1){
                fos.write(i);
            }
            output.close();
        } catch (FileNotFoundException e) {
            System.err.println("filepath not found!");
        } catch (IOException e) {
            System.err.println("IOException: " + e);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

    }

    public void add(String user, String password) {
        if ( !(usersList.contains(user) || passwordList.contains(password))) {
            if(usersList.isEmpty() || passwordList.isEmpty()) {
                usersList.clear();
                passwordList.clear();
                usersList.add(user);
                passwordList.add(password);
            } else {
                usersList.add(usersList.size(), user);
                passwordList.add(usersList.size() - 1, password);
            }
        }
    }

    public void display() {
        System.out.println(usersList);
        System.out.println(passwordList);
    }

    public void save() {
        try {
            for (int x = 0; x < usersList.size(); x++) {
                output.print(usersList.get(x));
                output.print("|");
                output.println(passwordList.get(x));
            }
            CipherInputStream cipherIn = new CipherInputStream(new FileInputStream(new File("data.txt")), cipher);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            int i; 
            while ((i = cipherIn.read()) != -1) {
                fos.write(i);
            }

            output.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }
}

public class CheckerTest {
    public static void main(String[] args) {
        Checker checker = new Checker();
        checker.add("peter", "12345");
        checker.add("mike", "67890");
        checker.display();
        checker.save();
        checker.check();
    }
}

我知道我的 check() 方法不能完全工作(它实际上并不检查它们是否在列表中),但我只需要解密文件,而不是将加密数据与解密数据混合在一起。

最佳答案

对您的代码的一些提示:

  • 插入描述您这样做的原因的注释(以帮助您稍后维护/理解它)
  • 定义private final像“AES”和“data.txt”这样的神奇字符串的常量现在被复制到各处(拼写错误的可能性较小,并且如果需要的话更容易更改)
  • 尽可能使用隐藏实现细节的基本类型,即 List<String> usersList
  • 您也可以创建 Map<<String>,<String>>,而不是尝试使 usersList 和 passwordsList 保持同步。用于存储用户名密码。

关于java - 在Java中解密文件并将其导出到文件而不进入无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1962852/

相关文章:

java - 如何将字符串格式的xml解析为数组?

java - 如何将Java中的图片保存到多个blob中?

wordpress - 为我的 WordPress 网站强制使用 https...特定子域除外

java - 如何锁定、打开文本文件以供阅读?

java - 从千兆字节文件中读取

java - 使用 VisualVM 分析内存泄漏

java - 如何更新不显示 fragment

java - 将 Vaadin 与谷歌应用引擎一起使用

Powershell SecureString 加密/解密到纯文本不起作用

java - 为什么要同步Java方法Provider.getService(String type,String algorithm)?