| 12345678910111213141516171819202122232425262728 |
- package storage;
- import model.Chat;
- import java.io.*;
- import java.nio.file.*;
- import java.security.*;
- import javax.crypto.*;
- import javax.crypto.spec.SecretKeySpec;
- import java.util.*;
- public class SecureStorage {
- public static void saveChats(List<Chat> chats, String password) throws Exception {
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
- ObjectOutputStream objOut = new ObjectOutputStream(byteStream);
- objOut.writeObject(chats);
- objOut.close();
- byte[] data = byteStream.toByteArray();
- byte[] key = Arrays.copyOf(password.getBytes(), 16); // AES 128-bit
- SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- byte[] encrypted = cipher.doFinal(data);
- Files.write(Paths.get("chats.dat"), encrypted);
- }
- }
|