title: Getting error(Error: Unsupported state or unable to authenticate data) in nodejs decryption
body: <p>I have encrypted a message using AES/GCM/NoPadding algorithm(AES-256) in java &amp; trying to decrypt it in NodeJs. Getting exception "<strong>Error: Unsupported state or unable to authenticate data</strong>" while decryption. Below is the complete code of java and nodejs &amp; error message:
  Pl help me where is the incorrect code in java or nodejs.</p>
  
  <p><strong>Below is the code started with Java encryption code :</strong></p>
  
  <pre><code> public static String encryptAES(String privateString, String skey) throws Exception{   
      byte[] iv = new byte[GCM_IV_BYTES_LENGTH]; //12 iv length
      byte[] tag = new byte[GCM_TAG_BYTES_LENGTH]; //16 tag length
      (new SecureRandom()).nextBytes(iv);
      (new SecureRandom()).nextBytes(tag);
  
      Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); //algorithm type
      GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_BYTES_LENGTH * Byte.SIZE, iv);
      cipher.init(Cipher.ENCRYPT_MODE, getKey(skey), ivSpec);
  
      byte[] ciphertext = cipher.doFinal(privateString.getBytes("UTF8"));
  
      byte[] ivTag = new byte[GCM_IV_BYTES_LENGTH + GCM_TAG_BYTES_LENGTH]; // merging iv and tag
      System.arraycopy(iv, 0, ivTag, 0, iv.length);
      System.arraycopy(tag, 0, ivTag, iv.length, tag.length);
  
      byte[] encrypted = new byte[ivTag.length + ciphertext.length]; //merging ivtag and cipher
      System.arraycopy(ivTag, 0, encrypted, 0, ivTag.length);
      System.arraycopy(ciphertext, 0, encrypted, ivTag.length, ciphertext.length);
  
      String encoded = Base64.getEncoder().encodeToString(encrypted); //b64 encoded value
      System.out.println("encrypted str:&gt;" + encoded.length() + " | " + encoded);
      return encoded;
  }
  </code></pre>
  
  <p><strong>//NodeJS decryption code :</strong></p>
  
  <pre><code>function decryptTokenResponse(encryptedStr){
      let data = encryptedStr
      const bData = Buffer.from(data, 'base64');
  
      const iv = bData.slice(0, 12);
      const tag = bData.slice(12, 28);
      const text = bData.slice(28);
  
      var decipher = crypto.createDecipheriv(algorithm,masterkey, iv)
      decipher.setAuthTag(tag)
      var plainText = decipher.update(text,'base64','utf-8');
      plainText += decipher.final('utf-8'); **//getting exception here**
      console.log('Decrypted data = ' + plainText)
  }           
  
  
  **//Error :**
  
                  internal/crypto/cipher.js:145
                    const ret = this._handle.final();
                                             ^
  
                  Error: Unsupported state or unable to authenticate data
                      at Decipheriv.final (internal/crypto/cipher.js:145:28)
                      at decryptTokenResponse (/home/jdoodle.js:40:27)
                      at Object.&lt;anonymous&gt; (/home/jdoodle.js:18:1)
                      at Module._compile (internal/modules/cjs/loader.js:678:30)
                      at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
                      at Module.load (internal/modules/cjs/loader.js:589:32)
                      at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
                      at Function.Module._load (internal/modules/cjs/loader.js:520:3)
                      at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
                      at startup (internal/bootstrap/node.js:228:19)
                  Command exited with non-zero status 1
  </code></pre>