Exemplo n.º 1
0
        // event. click on 'Cacher' boutton
        private void cacherBtn_Click(object sender, EventArgs e)
        {
            // Désactivation du boutton 'Lire' du fichier audio de sortie + 'Extraction Rapide..'
            playOutputBtn.Enabled = ExtractionRapideBtn.Enabled = false;

            // remise à zéro de la barre de chargement
            progressBar1.Value = 0;

            // 1 - vérification du fichier audio d'entrée + la clé + le texte à cacher
            if (audioFileTxtBox.Text == "")
            {
                MessageBox.Show("Aucun fichier audio choisi !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (cleTxtBox.Text == "")
            {
                MessageBox.Show("Aucune clé choisie !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtAcacherTab1.Text == "")
            {
                MessageBox.Show("Aucun texte à cacher !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 2 - demande d'emplacement d'enregistrement du fichier audio de sortie
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Title      = "Enregistrer le fichier audio de sortie dans";
            saveFileDialog1.DefaultExt = "wav";
            saveFileDialog1.Filter     = "audio files (*.wav)|*.wav";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) // si emplacement choisi
            {
                // Let's hide the message/text !

                // on commence le calcul du temp d'éxecution
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // create streams
                Stream     sourceStream      = null;
                FileStream destinationStream = null;
                WaveStream audioStream       = null;

                // create a stream that contains the message, preceeded by its length
                Stream messageStream = ToolsAndFunctions.stringToStream(txtAcacherTab1.Text);
                // create a stream that contains the key
                Stream keyStream = ToolsAndFunctions.stringToStream(cleTxtBox.Text);

                long countSamplesRequired = 0; // var to count samples required (we use it in catch() so this is the best place)

                try
                {
                    //how man samples do we need?
                    countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length);

                    if (countSamplesRequired > Int32.MaxValue)
                    {
                        throw new Exception("Message too long, or bad key! This message/key combination requires " + countSamplesRequired + " samples, only " + Int32.MaxValue + " samples are allowed.");
                    }

                    // open source stream
                    sourceStream = new FileStream(audioFileTxtBox.Text, FileMode.Open);

                    // create destination stream
                    destinationStream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);

                    //copy the carrier file's header
                    audioStream = new WaveStream(sourceStream, destinationStream);
                    if (audioStream.Length <= 0)
                    {
                        throw new Exception("Invalid WAV file");
                    }

                    //are there enough samples in the carrier wave?
                    if (countSamplesRequired > audioStream.CountSamples)
                    {
                        String errorReport = "The carrier file is too small for this message and key!\r\n"
                                             + "Samples available: " + audioStream.CountSamples + "\r\n"
                                             + "Samples needed: " + countSamplesRequired + "\r\n\r\n"
                                             + "\tOuvrir la fenêtre d'enregistrement audio ?";
                        throw new Exception(errorReport);
                    }

                    //hide the message
                    this.Cursor = Cursors.WaitCursor;
                    WaveUtility utility = new WaveUtility(audioStream, destinationStream);
                    utility.Hide(messageStream, keyStream);

                    // set output audio file path
                    outputAudioTxtBox.Text = saveFileDialog1.FileName;

                    // on arrête le calcul du temp d'éxecution + on l'affiche
                    sw.Stop();
                    execTime.Text = sw.ElapsedMilliseconds.ToString() + " ms";

                    // activate 'Lire' boutton + 'Extraction Rapide..'
                    playOutputBtn.Enabled = ExtractionRapideBtn.Enabled = true;

                    // chargement complet de la barre de chargement
                    progressBar1.Value = 100;
                }
                catch (Exception ex)
                {
                    //this.Cursor = Cursors.Default;
                    // if Message contains "Samples needed" , mean that wav file is not enought
                    if (ex.Message.Contains("Samples needed"))
                    {
                        if (MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == System.Windows.Forms.DialogResult.Yes)
                        {
                            Form fen = new RecordSound(countSamplesRequired);
                            fen.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                finally
                {
                    if (keyStream != null)
                    {
                        keyStream.Close();
                    }
                    if (messageStream != null)
                    {
                        messageStream.Close();
                    }
                    if (audioStream != null)
                    {
                        audioStream.Close();
                    }
                    if (sourceStream != null)
                    {
                        sourceStream.Close();
                    }
                    if (destinationStream != null)
                    {
                        destinationStream.Close();
                    }
                    this.Cursor = Cursors.Default;
                    if (sw.IsRunning)
                    {
                        sw.Stop();
                    }
                } // fin try
            }     // fin if
        }
Exemplo n.º 2
0
        // event. click on 'Extraire' boutton
        private void extraireBtn_Click(object sender, EventArgs e)
        {
            // vidage de la zone qui contiendra le message caché
            txtCacherTab2.Text = "";

            // remise à zéro de la barre de chargement
            progressBar1.Value = 0;

            // 1 - vérification du fichier audio d'entrée + la clé
            if (audioFileTxtBox.Text == "")
            {
                MessageBox.Show("Aucun fichier audio choisi !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (cleTxtBox.Text == "")
            {
                MessageBox.Show("Aucune clé choisie !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Let's Extract the message !

            // on commence le calcul du temp d'éxecution
            Stopwatch sw = new Stopwatch();

            sw.Start();

            // create streams
            FileStream sourceStream = null;
            WaveStream audioStream  = null;

            //create an empty stream to receive the extracted message
            MemoryStream messageStream = new MemoryStream();

            // create a stream that contains the key
            Stream keyStream = ToolsAndFunctions.stringToStream(cleTxtBox.Text);

            try
            {
                //open the carrier file
                sourceStream = new FileStream(audioFileTxtBox.Text, FileMode.Open);
                audioStream  = new WaveStream(sourceStream);
                WaveUtility utility = new WaveUtility(audioStream);

                //exctract the message from the carrier wave
                this.Cursor = Cursors.WaitCursor;
                Thread thread = new Thread(() => { utility.Extract(messageStream, keyStream); });
                thread.Start();

                int timeout = (int)TimeoutNumericUpDown.Value * 1000; // conversion en milliseconde
                if (!thread.Join(timeout))                            // if timeout
                {
                    thread.Abort();
                    throw new Exception("[Timeout !]\r\nIl est probable que le fichier audio ou que la clé soit erronée !");
                }

                // show message
                if (messageStream.Length > 0)
                {
                    messageStream.Seek(0, SeekOrigin.Begin);
                    txtCacherTab2.Text = new StreamReader(messageStream).ReadToEnd();
                }

                // on arrête le calcul du temp d'éxecution + on l'affiche
                sw.Stop();
                execTime.Text = sw.ElapsedMilliseconds.ToString() + " ms";

                // chargement complet de la barre de chargement
                progressBar1.Value = 100;
            }
            catch (Exception ex)
            {
                //this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally
            {
                if (keyStream != null)
                {
                    keyStream.Close();
                }
                if (messageStream != null)
                {
                    messageStream.Close();
                }
                if (audioStream != null)
                {
                    audioStream.Close();
                }
                if (sourceStream != null)
                {
                    sourceStream.Close();
                }
                this.Cursor = Cursors.Default;
                if (sw.IsRunning)
                {
                    sw.Stop();
                }
            }
        }