java - How authentication tag is calculated in AES-GCM-256 -


i have sample code,which encrypt , decrypt string using aes-gcm-256.

i unable understand,how authentication tag being generated on encrypter side , how being used on decrypter side.

actually here not generating authentication tag either on encrypter side nor validating decrypter side,so being done internally library itself.

private static string encrypt(string s, byte[] k) throws exception {         securerandom r = securerandom.getinstance("sha1prng");         // generate 128 bit iv encryption         byte[] iv = new byte[12]; r.nextbytes(iv);          secretkeyspec eks = new secretkeyspec(k, "aes");         cipher c = cipher.getinstance("aes/gcm/nopadding");          // generated authentication tag should 128 bits         c.init(cipher.encrypt_mode, eks, new gcmparameterspec(128, iv));         byte[] es = c.dofinal(s.getbytes(standardcharsets.utf_8));          // construct output "iv + ciphertext"         byte[] os = new byte[12 + es.length];         system.arraycopy(iv, 0, os, 0, 12);         system.arraycopy(es, 0, os, 12, es.length);          // return base64 encoded string         return base64.getencoder().encodetostring(os);      }      private static string decrypt(string eos, byte[] k) throws exception {         // recover our byte array base64 decoding         byte[] os = base64.getdecoder().decode(eos);          // check minimum length (iv (12) + tag (16))         if (os.length > 28) {             byte[] iv = arrays.copyofrange(os, 0, 12);             byte[] es = arrays.copyofrange(os, 12, os.length);              // perform decryption             secretkeyspec dks = new secretkeyspec(k, "aes");             cipher c = cipher.getinstance("aes/gcm/nopadding");             c.init(cipher.decrypt_mode, dks, new gcmparameterspec(128, iv));              // return our decrypted string             return new string(c.dofinal(es), standardcharsets.utf_8);         }         throw new exception();     } 


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -