Click here to Skip to main content
11,408,708 members (60,779 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi friend I first I am taking an text data in string veriable & then i am transfering it to Encrypt function which encrypts string using DES algo it is giving me correct string by incrypting but in next step i am writing that string to wave file means i am hiding string in wave file but at the time of hiding string in wave file it si not hiding whole string it is just hiding first 11 charactes those there are 16 charcter in the encrypted string.so why it is not hiding whole string.
but one thing is there if i hide the same encrypted string by hardcoded way means instead of calling to encrypt function i am directly assigning some value to string veriable in that case it is working fine & hiding all the string without any problem.i have attached my both function
ENCRYPT function & Hide data function here please review it & help me thank you

 private void btnHide_Click(object sender, EventArgs e)
        {
            BinaryWriter messageWriter = new BinaryWriter(new MemoryStream());
      
            Stream sourceStream = null;
            FileStream destinationStream = null;
            WaveStream audioStream = null;
 
            //create a stream that contains the message, preceeded by its length

            messageWriter.Write(txtMessage.Text.Length);
            string encryptDatas =txtMessage.Text;  //Encrypt(txtMessage.Text);
            messageWriter.Write(Encoding.ASCII.GetBytes(encryptDatas));
            messageWriter.Seek(0, SeekOrigin.Begin);
 
            Stream messageStream = messageWriter.BaseStream;
            //open the key file
            Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open);
 
            try
            {
 
                //how man samples do we need?
                long 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.");
                }
 
                //use a .wav file as the carrier
                sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open);
 

                this.Cursor = Cursors.WaitCursor;
 
                //create an empty file for the carrier wave
                destinationStream = new FileStream(txtDstFile.Text, FileMode.Create);
 
                //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;
                    throw new Exception(errorReport);
                }
 
                //hide the message
                WaveUtility utility = new WaveUtility(audioStream, destinationStream);
                utility.Hide(messageStream, keyStream);
                MessageBox.Show("data is hided");
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message);
            }
            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;
            }
        }
 

public string Encrypt(string originalString)
        {
            byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");
            if (String.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException
                       ("The string which needs to be encrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvid = new DESCryptoServiceProvider();
            MemoryStream memoryStre = new MemoryStream();
            CryptoStream cryptoStre = new CryptoStream(memoryStre,
                cryptoProvid.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStre);
            writer.Write(originalString);
            writer.Flush();
            cryptoStre.FlushFinalBlock();
            writer.Flush();
            byte[] bb = memoryStre.GetBuffer();
            int l=(int)memoryStre.Length;
            writer.Close();
            writer.Dispose();
            cryptoProvid.Clear();
            cryptoProvid.Dispose();
            
            memoryStre.Close();
            memoryStre.Dispose();
            cryptoStre.Clear();
            cryptoStre.Close();
            cryptoStre.Dispose();
            writer.Close();
            writer.Dispose();
            return Convert.ToBase64String(bb, 0,l );
        }
 
     
without decrypting i am just showing what string is get encrypted , I have function to decrypt also but there is problem in writeing encrypted string only when i give same encrypted string by hard coding it gives me correct out put but when i call to encrypt method those it send correct string encrypting but also when goes to write it does not write whole sting in wave file.so please help me on it .Thanks.
Posted 10-Oct-12 22:35pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 364
2 Maciej Los 195
3 Peter Leow 140
4 PIEBALDconsult 130
0 Sergey Alexandrovich Kryukov 8,626
1 OriginalGriff 6,855
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100