public bool Extract(int mode, FileStream KeyStream, Stream MessageStream) { SteganoWave.WaveStream AudioStream = new SteganoWave.WaveStream(stream); /* 冗余 */ bytesPerSample = AudioStream.Format.wBitsPerSample / 8; AudioStream.Seek(0, SeekOrigin.Begin); /* Extra output informations */ //FileStream _fs = new FileStream(AudioHide.storage + "Message\\record01.txt", FileMode.Create); //StreamWriter _sw = new StreamWriter(_fs); switch (mode) { /* src/out/key/mes OK * src --- this.stream * key --- KeyStream * message --- MessageStream * out --- OutWave */ #region Improved LSB algorithm case 0: byte[] waveBuffer = new byte[bytesPerSample]; int cnt = 0; int keyByte; //distance of the next carrier sample int mes, Mes = 0; int[] length = { 0, 0, 0, 0 }; long Length = 1000000; while (AudioStream.ReadByte() >= 0 && MessageStream.Length < Length) { Mes = 0; //if (AudioStream.Position == 1) // AudioStream.Seek(0, SeekOrigin.Begin); AudioStream.Seek(-1, SeekOrigin.Current); for (int bitIndex = 0; bitIndex < 8; bitIndex++) { cnt++; /* 这里看编码规则解码 值应减1 */ keyByte = GetKeyValue(KeyStream); /* jump out of stream */ for (int i = 0; i < keyByte; i++) { AudioStream.Read(waveBuffer, 0, waveBuffer.Length); } mes = (int)(waveBuffer[waveBuffer.Length - 1] % 2 == 0 ? 0 : 1); /* 4 Bytes for message's length */ if (cnt < 32) { length[(cnt - 1) / 8] += (1 & mes) << ((cnt - 1) % 8); } else if (cnt == 32) { Length = (length[3] << 24) + (length[2] << 16) + (length[1] << 8) + length[0]; } else { Mes += (1 & mes) << bitIndex; } } if (cnt > 32) { MessageStream.WriteByte((byte)Mes); } } break; #endregion #region parity case 1: /* Echo hiding methods*/ break; #endregion #region MCLF case 2: /* MCLF methods*/ break; #endregion default: break; } /*_sw.Flush(); * _sw.Close(); * _fs.Flush(); * _fs.Close();*/ AudioStream.Dispose(); return(true); }
public bool Hide(int mode, FileStream KeyStream, Stream MessageStream, string name) { FileStream OutWave = new FileStream(name, FileMode.Create); SteganoWave.WaveStream AudioStream = new SteganoWave.WaveStream(stream, OutWave); /* 冗余 */ bytesPerSample = AudioStream.Format.wBitsPerSample / 8; /* Extra output informations */ //FileStream _fs = new FileStream(AudioHide.storage + "Message\\record01.txt", FileMode.Create); //StreamWriter _sw = new StreamWriter(_fs); switch (mode) { /* src/out/key/mes OK * src --- this.stream * key --- KeyStream * message --- MessageStream * out --- OutWave */ #region Improved LSB algorithm case 0: byte[] waveBuffer = new byte[bytesPerSample]; byte message, bit, waveByte; int messageBuffer; //receives the next byte of the message or -1 int keyByte; //distance of the next carrier sample while ((messageBuffer = MessageStream.ReadByte()) >= 0) { message = (byte)messageBuffer; for (int bitIndex = 0; bitIndex < 8; bitIndex++) { keyByte = GetKeyValue(KeyStream); /* copy the previous sample point */ for (int i = 0; i < keyByte - 1; i++) { Copy(waveBuffer, 0, waveBuffer.Length, OutWave); } /* modify the last bit of the selected point */ stream.Read(waveBuffer, 0, waveBuffer.Length); waveByte = (byte)(waveBuffer[bytesPerSample - 1] % 2); bit = (byte)(((message & (byte)(1 << bitIndex)) > 0) ? 1 : 0); if ((bit ^ waveByte) == 1) { waveByte = (byte)(waveByte > 0 ? -1 : 1); waveBuffer[bytesPerSample - 1] += waveByte; } OutWave.Write(waveBuffer, 0, bytesPerSample); } } //copy the rest of the wave without changes waveBuffer = new byte[stream.Length - stream.Position]; stream.Read(waveBuffer, 0, waveBuffer.Length); OutWave.Write(waveBuffer, 0, waveBuffer.Length); break; #endregion #region parity case 1: /* Echo hiding methods*/ break; #endregion #region MCLF case 2: /* MCLF methods*/ break; #endregion default: break; } /*_sw.Flush(); * _sw.Close(); * _fs.Flush(); * _fs.Close();*/ OutWave.Close(); return(true); }