public class Otpyrc { private static final String ALGO = "AES"; private static final String CIPHER = "AES/GCM/NoPadding"; private static final int KEY_SIZE = 128; private static final int IV_SIZE = 12; private static final int TAG_SIZE = 128; private static final String BASE64_KEY = "Vn5rHf/xGvW9Vyt3w9TYbg=="; private static SecretKey getKey() { byte[] decodedKey = Base64.getDecoder().decode(BASE64_KEY); return new SecretKeySpec(decodedKey, ALGO); } public static void encryptToFile(String content, Path outputPath) throws Exception { byte[] iv = new byte[IV_SIZE]; SecureRandom random = new SecureRandom(); random.nextBytes(iv); Cipher cipher = Cipher.getInstance(CIPHER); GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_SIZE, iv); cipher.init(Cipher.ENCRYPT_MODE, getKey(), gcmSpec); byte[] ciphertext = cipher.doFinal(content.getBytes()); byte[] combined = new byte[iv.length + ciphertext.length]; System.arraycopy(iv, 0, combined, 0, iv.length); System.arraycopy(ciphertext, 0, combined, iv.length, ciphertext.length); Files.writeString(outputPath, Base64.getEncoder().encodeToString(combined)); } public static String decryptFromFile(Path inputPath) throws Exception { try { byte[] combined = Base64.getDecoder().decode(Files.readString(inputPath)); byte[] iv = new byte[IV_SIZE]; byte[] ciphertext = new byte[combined.length - IV_SIZE]; System.arraycopy(combined, 0, iv, 0, IV_SIZE); System.arraycopy(combined, IV_SIZE, ciphertext, 0, ciphertext.length); Cipher cipher = Cipher.getInstance(CIPHER); GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_SIZE, iv); cipher.init(Cipher.DECRYPT_MODE, getKey(), gcmSpec); byte[] decrypted = cipher.doFinal(ciphertext); return new String(decrypted); } catch (Exception e) { return Files.readString(inputPath); } } }