在Java中解密文件并将其导出到文件而不进入无限循环?
如果您有多个用户和密码,如何在 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()
方法不能完全工作(它实际上并不检查它们是否在列表中),但我只需要解密文件,不要将加密数据与解密数据混合在一起。
How do you decrypt a file in java and export it to a file without having to end up in an infinite loop if you have more than one user and password? Here is my code and at the end is my test file:
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();
}
}
I know that my check()
method does not work fully (it doesn't actually check if they are in the list) but I just need to decrypt the file as well as, not to have the encrypted with the decrypted data all mixed up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对您的代码的一些提示:
private final
常量现在到处复制(打字错误的可能性较小,并且如果需要的话更容易更改)List; usersList
Map<,>
来存储用户名的密码,而不是尝试使 usersList 和 passwordsList 保持同步。A few hints towards your code:
private final
constants for the magic strings like "AES" and "data.txt" that are copied all over now (less chance of typoes and easier to change if need be)List<String> usersList
Map<<String>,<String>>
for storing passwords for usernames.关于代码的一些注释:
你为什么要这样做:
usersList.add(usersList.size(), user);
passwordList.add(userList.size()-1, 密码);
说实话,我不太明白。为什么不直接 .add() 添加到相应的列表中?
当您之前检查过列表已经为空时,为什么还要 .clear() 列表?
对于您的导出问题(我认为适应它不会太难):
从这里: http://www.java2s.com/Tutorial/Java/0490__Security/UsingCipherOutputStream.htm
Some remarks on your code:
Why are you doing this:
usersList.add(usersList.size(), user);
passwordList.add(usersList.size()-1, password);
Honestly, I don't get it. Why don't you just .add() to the according list?
Why do you .clear() the lists when you have checked before that they are already empty?
For your exporting problem (I think it won't be too hard to adapt it):
From here: http://www.java2s.com/Tutorial/Java/0490__Security/UsingCipherOutputStream.htm