【发布时间】:2023-03-12 09:43:01
【问题描述】:
有人知道如何在我的代码中包含加密和解密吗?我将 FileInput 和 FileOutput Stream 用于序列化文件。我有一个学生数组列表,我有一个书籍数组列表。我可以从他们各自的文件中保存和读取它们。但为了安全起见,我想对它们进行加密和解密。我该怎么做?
private static void ReadBook() {
try {
FileInputStream fi = new FileInputStream("bookData.ser");
ObjectInputStream oi = new ObjectInputStream(fi);
bookList = (ArrayList<Book>) oi.readObject();
oi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
protected static void SaveBook(ArrayList<Book> books) {
ArrayList<Book> tempbookList = books;
try {
FileOutputStream fs = new FileOutputStream("bookData.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.reset();
os.writeObject(tempbookList);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void ReadStudent() {
try {
FileInputStream fi = new FileInputStream("studentData.ser");
ObjectInputStream oi = new ObjectInputStream(fi);
studentList = (ArrayList<Student>) oi.readObject();
oi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
protected static void SaveStudent(ArrayList<Student> students) {
ArrayList<Student> tempstudentList = students;
try {
FileOutputStream fs = new FileOutputStream("studentData.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.reset();
os.writeObject(tempstudentList);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
【问题讨论】:
-
这能回答你的问题吗? En/Decrypting an In/Output stream in Java?
-
A) 请了解 java 命名约定。方法名称也应该是 camelCase()。 B) 定义“安全”。当该代码仅在您的计算机上运行时,有什么可担心的。如果该代码在某些学校计算机上运行……那么,任何人都可以运行您的程序,然后查看解密的信息?!所以真正的答案是:这在很大程度上取决于您的要求。因为“我想要安全”有很多不同的答案......基本上整本书都是关于这个主题的。
-
这能回答你的问题吗? How to encrypt String in Java
-
我的意思是:如果你想了解加密,那就找一本好书或教程,开始做研究(有很多东西要学对这个)。但如果这只是你的一些爱好项目:不要把事情复杂化。先把你的基本任务做起来,也许然后,当你真的负担不起时,然后学习加密。
标签: java object encryption arraylist serialization