Generate code from comments

Generate Java code from natural-language comments

You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly.

System instructions

You are a senior Java developer who is an expert in design patterns and closely follows best object-oriented programming practices.

Prompt Freeform

I'm building a password manager for personal use to help protect my details. However, I am struggling to finish the final touches; specifically, the encryption and decryption methods which should use the AES encryption algorithm. Only response with the code, no explanation.

Please complete each method by making changes in the areas marked with <WRITE CODE HERE>.

package org.com.passwordmanager.service;

import org.springframework.stereotype.Service;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

@Service
public class EncryptionService {

private final SecretKey secretKey;

public EncryptionService() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
secretKey = keyGen.generateKey();
}

public String encrypt(String data) {
try {
// Create and initialize a Cipher instance for AES encryption
// Encrypt the data bytes using the Cipher instance
// Encode the encrypted bytes to a Base64 string and return the result
// <WRITE CODE HERE>
} catch (Exception e) {
throw new RuntimeException("Encryption error", e);
}
}

public String decrypt(String encryptedData) {
try {
// Create and initialize a Cipher instance for AES decryption
// Decode the Base64 string to get the encrypted bytes
// Decrypt the encrypted bytes
// Convert decrypted bytes to a string and return the result
// <WRITE CODE HERE>
} catch (Exception e) {
throw new RuntimeException("Decryption error", e);
}
}
}

Response

package org.com.passwordmanager.service;

import org.springframework.stereotype.Service;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

@Service
public class EncryptionService {

    private final SecretKey secretKey;

    public EncryptionService() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256);
        secretKey = keyGen.generateKey();
    }

    public String encrypt(String data) {
        try {
            // Create and initialize a Cipher instance for AES encryption
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            // Encrypt the data bytes using the Cipher instance
            byte[] encryptedBytes = cipher.doFinal(data.getBytes("UTF-8"));
            // Encode the encrypted bytes to a Base64 string and return the result
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            throw new RuntimeException("Encryption error", e);
        }
    }

    public String decrypt(String encryptedData) {
        try {
            // Create and initialize a Cipher instance for AES decryption
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            // Decode the Base64 string to get the encrypted bytes
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
            // Decrypt the encrypted bytes
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            // Convert decrypted bytes to a string and return the result
            return new String(decryptedBytes, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("Decryption error", e);
        }
    }
}
Model: gemini-2.5-pro
Temperature: 0.2
Max output tokens: 8192
TopK: 40.0
TopP: 0.95