'Issues while decrypting Initialization Vector IV algorithm

I am tying to implement Intilization vector 4 the issue I am facing is I am able to encrypt my text using intilization vector4 algorithm but when it comes to decryption I always get this error can anyone tell me what is wrong with my code

public static final int GCM_TAG_LENGTH = 16;

public static final int AES_KEY_SIZE = 256;
public static final int GCM_IV_LENGTH = 12;

SecretKey key;

 public void init() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(AES_KEY_SIZE);

        // Generate Key
         key = keyGenerator.generateKey();
    }



@PostMapping("/encrypt/{name}")
    public void getValues(@PathVariable("name") String name) throws Exception {
        init();
        byte[] IV = new byte[GCM_IV_LENGTH];
        SecureRandom random = new SecureRandom();
        random.nextBytes(IV);

        System.out.println("Original Text : " + name);

        byte[] cipherText = encrypt(subId.getBytes(), key, IV);
        System.out.println("Encrypted Text : " + Base64.getEncoder().encodeToString(cipherText));
    }

 @PostMapping("/decrypt/{name}")
    public void getDecryptValues(@PathVariable("name") String name) throws Exception {
        byte [] cipherText=name.getBytes();
        byte[] IV = new byte[GCM_IV_LENGTH];
        String decryptedText = decrypt(cipherText, key, IV);
        System.out.println("DeCrypted Text : " + decryptedText);
    }

public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception
    {
        // Get Cipher Instance for selected algorithm
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

        // Create SecretKeySpec for key
        SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");

        // Create GCMParameterSpec for key
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);

        // Initialize Cipher for ENCRYPT_MODE for encrypt plaintext
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);

        // Perform Encryption
        byte[] cipherText = cipher.doFinal(plaintext);

        return cipherText;
    }

public static String decrypt(byte[] cipherText, SecretKey key, byte[] IV) throws Exception
    {
        // Get Cipher Instance based on selective AES algorithm
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

        // Create SecretKeySpec for key
        SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");

        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);

        // Initialize Cipher for DECRYPT_MODE to in plain text
        cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);

        // Perform Decryption on encrypted text
        byte[] decryptedText = cipher.doFinal(cipherText);

        return new String(decryptedText);
    }

My encryption code works fine but there is issue with decryption I always get this error "javax.crypto.AEADBadTagException: Tag mismatch!" How can I resolve this error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source