public static MemoryStream EncryptHelper(byte[] plainTextArray, byte[] keyArray, byte[] nonceArray, byte[] aadArray, int readCount) { var encryptedDataStream = new MemoryStream(); using (var baseStream = new MemoryStream(plainTextArray)) using (var stream = new AesGcmEncryptStream(baseStream, keyArray, nonceArray, TagBitsLength, aadArray)) { int readBytes; var buffer = new byte[readCount]; while ((readBytes = stream.Read(buffer, 0, readCount)) > 0) { encryptedDataStream.Write(buffer, 0, readBytes); } } return(encryptedDataStream); }
/// <summary> /// Returns an updated stream where the stream contains the encrypted object contents. /// The specified instruction will be used to encrypt data. /// </summary> /// <param name="toBeEncrypted"> /// The stream whose contents are to be encrypted. /// </param> /// <param name="instructions"> /// The instruction that will be used to encrypt the object data. /// </param> /// <returns> /// Encrypted stream, i.e input stream wrapped into encrypted stream /// </returns> internal static Stream EncryptRequestUsingInstructionV2(Stream toBeEncrypted, EncryptionInstructions instructions) { Stream gcmEncryptStream = new AesGcmEncryptStream(toBeEncrypted, instructions.EnvelopeKey, instructions.InitializationVector, DefaultTagBitsLength); return(gcmEncryptStream); }