Пример #1
0
        private void btnExtract_Click(object sender, System.EventArgs e)
        {
            Bitmap bitmap = (Bitmap)picImage.Image;

            //empty stream for the extracted message
            Stream messageStream = new MemoryStream();

            //get a stream for the key
            Stream keyStream = GetKeyStream();

            if (keyStream.Length == 0)
            {
                MessageBox.Show("Please enter a password or select a key file.");
                txtKeyText.Focus();
            }
            else
            {
                try{
                    //extract the hidden message from the bitmap
                    CryptUtility.ExtractMessageFromBitmap(bitmap, keyStream, ref messageStream);

                    //save the message, if a filename is available
                    if (txtExtractedMsgFile.Text.Length > 0)
                    {
                        messageStream.Seek(0, SeekOrigin.Begin);
                        FileStream fs            = new FileStream(txtExtractedMsgFile.Text, FileMode.Create);
                        byte[]     streamContent = new Byte[messageStream.Length];
                        messageStream.Read(streamContent, 0, streamContent.Length);
                        fs.Write(streamContent, 0, streamContent.Length);
                    }

                    //display the message - displays chaos, if it's no unicode text
                    messageStream.Seek(0, SeekOrigin.Begin);
                    StreamReader reader        = new StreamReader(messageStream, UnicodeEncoding.Unicode);
                    String       readerContent = reader.ReadToEnd();
                    if (readerContent.Length > txtExtractedMsgText.MaxLength)
                    {
                        readerContent = readerContent.Substring(0, txtExtractedMsgText.MaxLength);
                    }
                    txtExtractedMsgText.Text = readerContent;
                }catch (Exception ex) {
                    MessageBox.Show("Exception:\r\n" + ex.Message);
                }
            }
            //close the streams
            messageStream.Close();
            keyStream.Close();
            bitmap = null;
        }
Пример #2
0
        private void btnHide_Click(object sender, System.EventArgs e)
        {
            Bitmap bitmap = (Bitmap)picImage.Image;

            //get a stream for the message to hide
            Stream messageStream = GetMessageStream();

            if (messageStream.Length == 0)
            {
                MessageBox.Show("Please enter a message or select a file.");
                txtMessageText.Focus();
            }
            else
            {
                //get a stream fot the key
                Stream keyStream = GetKeyStream();
                if (keyStream.Length == 0)
                {
                    MessageBox.Show("Please enter a password or select a key file.");
                    txtKeyText.Focus();
                }
                else
                {
                    try{
                        //hide the message
                        CryptUtility.HideMessageInBitmap(messageStream, bitmap, keyStream, chkGrayscale.Checked);
                        //display result
                        picImage.Image        = bitmap;
                        btnSaveBitmap.Enabled = true;
                        MessageBox.Show("Hide the message successfull !");
                    }catch (Exception ex) {
                        MessageBox.Show("Exception:\r\n" + ex.Message);
                    }
                }
                keyStream.Close();
            }
            messageStream.Close();
            bitmap = null;
        }