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 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); } }