/// <summary> /// Hides a message in WaveAudio object's Left and Right Audio channels. /// </summary> /// <param name="message">String Message to be hidden.</param> public void HideMessage(string message) { /*Cache audio channel streams locally from WaveAudio object*/ List <short> leftStream = file.GetLeftStream(); List <short> rightStream = file.GetRightStream(); /*Hide Message in Streams*/ byte[] bufferMessage = System.Text.Encoding.UTF8.GetBytes(message); //Convert string Message into byte[] short tempBit; int bufferIndex = 0; //Set message stream index counter. int bufferLength = bufferMessage.Length; //Get length of message stream. int channelLength = leftStream.Count; //Get length of audio stream (both left and right streams have same length). int storageBlock = (int)Math.Ceiling((double)bufferLength / (channelLength * 2)); //Get storage block range based of length of audio channel stream and message stream. if (bufferLength > channelLength) //Check if message stream length is greater than a channel's audio stream length. { throw new MessageSizeExceededException("Message size is too large for target Audio stream."); //Throw an Exception. } /*Store message length info in first elements of left and right streams*/ leftStream[0] = (short)(bufferLength / 32767); //Store Quotient of actual size in first element of audio stream. rightStream[0] = (short)(bufferLength % 32767); //Store Remainder of actual size in first element of audio stream. for (int i = 1; i < leftStream.Count; i++) //Iterate over length of audio channel stream, skip first element since it contains message length, store message bits into left and right audio streams. { if (i < leftStream.Count) //Check if storing has not exceeded audio stream length. { if (bufferIndex < bufferLength && i % 8 > 7 - storageBlock && i % 8 <= 7) //Condition to target elements from the last position of every 8 bit block of audio stream (calculated based on storageBlock). { tempBit = (short)bufferMessage[bufferIndex++]; //Get message bit leftStream.Insert(i, tempBit); //Replace audio data bit with message bit. } } if (i < rightStream.Count) { if (bufferIndex < bufferLength && i % 8 > 7 - storageBlock && i % 8 <= 7) { tempBit = (short)bufferMessage[bufferIndex++]; rightStream.Insert(i, tempBit); } } } file.UpdateStreams(leftStream, rightStream); //Streams now have message hidden in it, update streams to actual WaveAudio object. }