Sobes.tech
Senior

Is there experience with encryption/cryptography (AES-256)?

sobes.tech AI

Answer from AI

Yes, I have experience working with AES-256 encryption in Java. AES-256 is a symmetric encryption algorithm with a 256-bit key length, providing a high level of security.

Example of using AES-256 in Java with the javax.crypto library:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.util.Base64;

public class AES256Example {
    private static final int KEY_SIZE = 256;
    private static final int T_LEN = 128; // tag length for GCM

    public static void main(String[] args) throws Exception {
        // Generate key
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(KEY_SIZE);
        SecretKey key = keyGen.generateKey();

        // Initialization vector (IV) for GCM
        byte[] iv = new byte[12]; // usually 12 bytes
        java.security.SecureRandom random = new java.security.SecureRandom();
        random.nextBytes(iv);

        String plaintext = "Example text for encryption";

        // Encryption
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(T_LEN, iv);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes());

        String encryptedBase64 = Base64.getEncoder().encodeToString(encrypted);
        System.out.println("Encrypted text: " + encryptedBase64);

        // Decryption
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] decrypted = cipher.doFinal(encrypted);
        System.out.println("Decrypted text: " + new String(decrypted));
    }
}

This example uses AES-GCM mode, which provides both confidentiality and data integrity. It is important to properly manage keys and IVs for security.