Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see Model versions and lifecycle.
Stay organized with collections
Save and categorize content based on your preferences.
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.
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>.
packageorg.com.passwordmanager.service;importorg.springframework.stereotype.Service;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;@ServicepublicclassEncryptionService{privatefinalSecretKeysecretKey;publicEncryptionService()throwsException{KeyGeneratorkeyGen=KeyGenerator.getInstance("AES");keyGen.init(256);secretKey=keyGen.generateKey();}publicStringencrypt(Stringdata){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(Exceptione){thrownewRuntimeException("Encryption error",e);}}publicStringdecrypt(StringencryptedData){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(Exceptione){thrownewRuntimeException("Decryption error",e);}}}
Response
packageorg.com.passwordmanager.service;importorg.springframework.stereotype.Service;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;importjava.util.Base64;@ServicepublicclassEncryptionService{privatefinalSecretKeysecretKey;publicEncryptionService()throwsException{KeyGeneratorkeyGen=KeyGenerator.getInstance("AES");keyGen.init(256);secretKey=keyGen.generateKey();}publicStringencrypt(Stringdata){try{// Create and initialize a Cipher instance for AES encryptionCiphercipher=Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE,secretKey);// Encrypt the data bytes using the Cipher instancebyte[]encryptedBytes=cipher.doFinal(data.getBytes("UTF-8"));// Encode the encrypted bytes to a Base64 string and return the resultreturnBase64.getEncoder().encodeToString(encryptedBytes);}catch(Exceptione){thrownewRuntimeException("Encryption error",e);}}publicStringdecrypt(StringencryptedData){try{// Create and initialize a Cipher instance for AES decryptionCiphercipher=Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE,secretKey);// Decode the Base64 string to get the encrypted bytesbyte[]encryptedBytes=Base64.getDecoder().decode(encryptedData);// Decrypt the encrypted bytesbyte[]decryptedBytes=cipher.doFinal(encryptedBytes);// Convert decrypted bytes to a string and return the resultreturnnewString(decryptedBytes,"UTF-8");}catch(Exceptione){thrownewRuntimeException("Decryption error",e);}}}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-18 UTC."],[],[],null,[]]