public static string encrypt(string PlainText, byte[] key, byte[] iv, long AAD, out byte[] outTag) { string sR = string.Empty; try { byte[] plainBytes = Encoding.ASCII.GetBytes(PlainText); GcmBlockCipher cipher = new GcmBlockCipher(new AesFastEngine()); AeadParameters parameters = new AeadParameters(new KeyParameter(key), 128, iv, null); var tempAddBytesArr = BitConverter.GetBytes(AAD); cipher.Init(true, parameters); foreach (var item in tempAddBytesArr) { cipher.ProcessAadByte(item); } byte[] encryptedBytes = new byte[cipher.GetOutputSize(plainBytes.Length)]; Int32 retLen = cipher.ProcessBytes (plainBytes, 0, plainBytes.Length, encryptedBytes, 0); outTag = cipher.GetMac(); cipher.DoFinal(encryptedBytes, retLen); sR = Convert.ToBase64String(encryptedBytes, Base64FormattingOptions.None); } catch (Exception ex) { outTag = new byte[1] { 0x0 }; Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } return(sR); }
public void Update(byte input) { cipher.ProcessAadByte(input); }