public byte[] Extract(DigitalAudio stegoAudio) { int[] primeSignal; using (WavReader reader = new WavReader(File.OpenRead(PrimeSignalFilePath))) primeSignal = reader.ReadAudio().GetSignal(); var stegoSignal = stegoAudio.GetSignal(); int samplesPerSegment = (int)Math.Floor((double)stegoSignal.Length / (MessageLength * 8)); var rnd = new Random(RandomSeed); int lseg, rseg; int[] pseudoRandomSignal = new int[samplesPerSegment]; var difference = new double[samplesPerSegment]; List <bool> message = new List <bool>(); double primeMean = 0, difMean = 0; for (int i = 0; (i + 1) * samplesPerSegment <= stegoSignal.Length; i++) { lseg = i * samplesPerSegment; rseg = samplesPerSegment * (i + 1); RndSignal(rnd, pseudoRandomSignal); for (int j = lseg, k = 0; j < rseg; j++, k++) { primeSignal[j] = (int)(primeSignal[j] * 0.95); difference[k] = (stegoSignal[j] - primeSignal[j]) * pseudoRandomSignal[k]; primeMean += primeSignal[j]; difMean += stegoSignal[j]; } message.Add(Math.Sign(primeMean) == Math.Sign(difMean)); primeMean = 0; difMean = 0; } return(BitArrayToByteArray(new BitArray(message.ToArray()))); }
public DigitalAudio Embed(DigitalAudio cover, byte[] message) { var coverSignal = cover.GetSignal(); BitArray embeddingMessage = CreateEmbeddedMessage(message); int FrameSize = (coverSignal.Length / embeddingMessage.Length); int parityBit = 0; int iframe = 0; for (int b = 0; b < embeddingMessage.Length; b++) { for (int i = 0; i < FrameSize; i++) { if ((coverSignal[iframe + i] & 1) == 1) { parityBit++; } } parityBit %= 2; if ((embeddingMessage[b] ? 1:0) != parityBit) { coverSignal[iframe] ^= 1; } iframe += FrameSize; parityBit = 0; } return(cover); }
public DigitalAudio Embed(DigitalAudio audio, byte[] message) { var signal = audio.GetSignal(); if (GetAvailableSpace(audio) < message.Length) { throw new ApplicationException("Attempt to insert a message that is longer than the container covers."); } //data var embeddedMessage = CreateEmbeddedMessage(message); int samplei = 0; int mask = ~((1 << bitsPerSampleCoding) - 1); int length = embeddedMessage.Length; for (int i = 0; i < length;) { signal[samplei] &= mask; for (int j = 0; j < bitsPerSampleCoding; j++) { signal[samplei] |= ((embeddedMessage[i++] ? 1 : 0) << j); } samplei++; } return(audio); }
public byte[] Extract(DigitalAudio audio) { var signal = audio.GetSignal(); byte[] stegoMark = ReadBits(signal, 0, StegoMark.LongLength); if (!stegoMark.SequenceEqual(StegoMark)) { return(null); } byte[] messageSize = ReadBits(signal, (StegoMark.Length * bitsInByte) / bitsPerSampleCoding, msgSizeLength); if (!BitConverter.IsLittleEndian) { messageSize.Reverse(); } long length = BitConverter.ToInt64(messageSize, 0); byte[] message = ReadBits(signal, ((StegoMark.Length + msgSizeLength) * bitsInByte) / bitsPerSampleCoding, length); return(message); }
public byte[] Extract(DigitalAudio stegoAudio) { int[] signal = stegoAudio.GetSignal(); int FrameSize = signal.Length / ((MessageLength + StegoMark.Length + msgSizeLength) * 8); byte[] stegoMark = ExtractBytes(signal, 0, StegoMark.Length, FrameSize); if (!stegoMark.SequenceEqual(StegoMark)) { return(null); } byte[] messageSize = ExtractBytes(signal, StegoMark.Length * 8 * FrameSize, msgSizeLength, FrameSize); if (!BitConverter.IsLittleEndian) { messageSize.Reverse(); } long length = BitConverter.ToInt64(messageSize, 0); byte[] message = ExtractBytes(signal, (msgSizeLength + StegoMark.Length) * 8 * FrameSize, (int)length, FrameSize); return(message); }
public byte[] Extract(DigitalAudio stegoAudio) { var stegoSignal = stegoAudio.GetSignal(); Complex32[] segment = new Complex32[segmentLength]; for (int i = 0; i < segmentLength; i++) { segment[i] = new Complex32(stegoSignal[i], 0); } Fourier.Forward(segment, FourierOptions.Matlab); //Check if has label var headerLen = bitsInByte * StegoMark.Length; List <bool> tmp = new List <bool>(headerLen); //Read Header var startIndex = segmentLength / 2 - 1; long endIndex = startIndex - headerLen; byte[] bytesData = ExtractRange(segment, tmp, startIndex, endIndex); if (!bytesData.SequenceEqual(StegoMark)) { return(null); } // Read msg size var msgSizeLen = msgSizeLength * bitsInByte; startIndex = (int)endIndex; endIndex = startIndex - msgSizeLen; bytesData = ExtractRange(segment, tmp, startIndex, endIndex); if (!BitConverter.IsLittleEndian) { bytesData.Reverse(); } long msgLength = BitConverter.ToInt64(bytesData, 0) * bitsInByte; //Read Data startIndex = (int)endIndex; endIndex = startIndex - msgLength; return(ExtractRange(segment, tmp, startIndex, endIndex)); }
public DigitalAudio Embed(DigitalAudio cover, byte[] message) { var primeSignal = (int[])cover.GetSignal().Clone(); //var embedingData = CreateEmbeddedMessage(message); var embedingData = new BitArray(message); int n_bits = embedingData.Length; var signal = cover.GetSignal(); //Segmentation int samplesPerSegment = (int)Math.Floor(signal.Length / (double)n_bits); var rnd = new Random(RandomSeed); int lseg, rseg; int[] pseudoRandomSignal = new int[samplesPerSegment]; int max = 0, min = 0; for (int i = 0; i < n_bits; i++) { lseg = i * samplesPerSegment; rseg = samplesPerSegment * (i + 1); RndSignal(rnd, pseudoRandomSignal); int bit = (embedingData[i] ? 1 : -1); //int bit = 1; for (int j = lseg, k = 0; j < rseg; j++, k++) { var mix = pseudoRandomSignal[k] * signal[j] * bit; signal[j] += (int)(NoiseRate * mix); if (signal[j] > max) { max = signal[j]; } if (signal[j] < min) { min = signal[j]; } } } //if (max > short.MaxValue || min < short.MinValue) //{ // float scaleFactor = ((short.MaxValue - short.MinValue) / (float)(max - min)); // for (int i = 0; i < signal.Length; i++) // signal[i] = (int)(scaleFactor * (signal[i] - min) + short.MinValue); //} // Extract -------------------------- rnd = new Random(RandomSeed); var difference = new double[samplesPerSegment]; List <bool> outBits = new List <bool>(); double primeMean = 0, difMean = 0; lseg = 0; rseg = 0; int segCount = n_bits; for (int i = 0; i < segCount; i++) { lseg = i * samplesPerSegment; rseg = samplesPerSegment * (i + 1); RndSignal(rnd, pseudoRandomSignal); for (int j = lseg, k = 0; j < rseg; j++, k++) { difference[k] = (signal[j] - primeSignal[j]) * pseudoRandomSignal[k]; primeMean += primeSignal[j]; difMean += signal[j]; } outBits.Add(Math.Sign(primeMean) == Math.Sign(difMean)); primeMean = 0; difMean = 0; } return(cover); }
public void Code() { OutputLogs logs = OutputLogs.Instance(); switch (action) { case NavigationCodes.Extract: OutputLogs.Instance().AddLog(new Message(MessageType.Info, "Extracting started...", method.ToString())); try { var bytes = method.Extract(cover); if (bytes == null) { throw new ApplicationException("No embedded message found."); } Message = Encoding.ASCII.GetString(bytes); string goodMsg = "Embedded message successfuly extracted."; logs.AddLog(new Message(MessageType.Info, goodMsg, method.ToString())); } catch (ApplicationException ex) { logs.AddLog(new Message(MessageType.Error, ex.Message, method.ToString())); } break; case NavigationCodes.Embed: logs.AddLog(new Message(MessageType.Info, "Embedding started...", method.ToString())); try { int[] signal = null; if (GetStatistics) { signal = (int[])cover.GetSignal().Clone(); } var data = Encoding.ASCII.GetBytes(Message); method.Embed(cover, data); logs.AddLog(new Message(MessageType.Info, "Message successfuly embeded.", method.ToString())); SaveFileDialogService.DefaultExt = "wav"; SaveFileDialogService.DefaultFileName = "StegoAudio"; if (SaveFileDialogService.ShowDialog()) { using (var stream = new WavWriter(SaveFileDialogService.OpenFile())) stream.WriteWavDefault(cover); logs.AddLog(new Message(MessageType.Info, "File was succesfuly saved.", "WavWriter")); } if (GetStatistics) { var stegosignal = cover.GetSignal(); var service = DocumentManagerService; var doc = service.CreateDocument("Statistics", new StatisticsViewModel(signal, stegosignal)); doc.Show(); } } catch (ApplicationException ex) { logs.AddLog(new Message(MessageType.Error, ex.Message, method.ToString())); } break; default: logs.AddLog(new Message(MessageType.Error, "Internal error", method.ToString())); break; } }
public DigitalAudio Embed(DigitalAudio cover, byte[] message) { BitArray embeddingMessage = CreateEmbeddedMessage(message); var msgLength = embeddingMessage.Length; // Create embeddingMessage var value = (float)(Math.PI / 2.0); float[] embeddingData = new float[msgLength]; for (int i = 0; i < msgLength; i++) { if (embeddingMessage[i]) { embeddingData[i] = -value; } else { embeddingData[i] = value; } } // Signal var coverSignal = cover.GetSignal(); var ComplexSignal = coverSignal.Select(x => new Complex32(x, 0)).ToArray(); double ratio = cover.SamplesInChannel / segmentLength; int N = (int)Math.Floor(ratio); //Segmnets count //--------------------------------------------- List <Complex32[]> signalSegments = new List <Complex32[]>(N); List <float[]> phases = new List <float[]>(N); List <float[]> magnitude = new List <float[]>(N); List <float[]> deltaPhases = new List <float[]>(N); for (int seg = 0; seg < N; seg++) { //----------Create----- signalSegments.Add(new Complex32[segmentLength]); phases.Add(new float[segmentLength]); magnitude.Add(new float[segmentLength]); deltaPhases.Add(new float[segmentLength]); //--------------------- //---Segments init--- Array.Copy(ComplexSignal, seg * segmentLength, signalSegments[seg], 0, segmentLength); // Signal copy //--------------------- //-------FFT---------- Fourier.Forward(signalSegments[seg], FourierOptions.Matlab); // Signal transform for each segment //-------------------- for (int j = 0; j < segmentLength; j++) { phases[seg][j] = signalSegments[seg][j].Phase; //Phases for each segment magnitude[seg][j] = signalSegments[seg][j].Magnitude; //Magnitude for each segment } } var spectrumMiddle = segmentLength / 2; // Delta phases for (int seg = 1; seg < N; seg++) { for (int j = 0; j < segmentLength; j++) { deltaPhases[seg][j] = phases[seg][j] - phases[seg - 1][j]; } } int startIndex = spectrumMiddle - 1; int startSymmetryIndex = spectrumMiddle + 1; for (int i = 0; i < msgLength; i++) { phases[0][startIndex - i] = embeddingData[i]; phases[0][startSymmetryIndex + i] = -embeddingData[i]; // symmetry } // New phases for (int seg = 1; seg < N; seg++) { for (int j = 0; j < segmentLength; j++) { phases[seg][j] = phases[seg - 1][j] + deltaPhases[seg][j]; } } //Restore signal for (int seg = 0; seg < N; seg++) { for (int j = 0; j < segmentLength; j++) { var A = magnitude[seg][j]; var phase = phases[seg][j]; signalSegments[seg][j] = Complex32.FromPolarCoordinates(A, phase); } Fourier.Inverse(signalSegments[seg], FourierOptions.Matlab); } for (int i = 0; i < N; i++) { for (int j = 0; j < segmentLength; j++) { coverSignal[segmentLength * i + j] = (int)signalSegments[i][j].Real; } } return(cover); }