(C#加密)幻术-大踲无形

首先:我看下面的代码只是知道大概的原理核心算法还是不太清楚~~有清楚的麻烦回复下谢谢咯咯
--这也是看Msdn就是把在一个图片上隐藏数据

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Text; 

namespace Steganography
{
    
/// <summary>
    
/// Summary description for SteganographyForm.
    
/// </summary>

    public class SteganographyForm : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button buttonHideMessage;
        
private System.Windows.Forms.Panel panelOriginalImage;
        
private System.Windows.Forms.TextBox textBoxOriginalMessage;
        
private System.Windows.Forms.Panel panelModifiedImage;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.GroupBox groupBox3;
        
private System.Windows.Forms.GroupBox groupBox4;
        
private System.Windows.Forms.Button buttonExtractMessage;
        
private System.Windows.Forms.TextBox textBoxExtractedlMessage;
        
private System.Windows.Forms.GroupBox groupBox2;

        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public SteganographyForm()
        
{
            
//
            
// Required for Windows Form Designer support
            
//
            InitializeComponent();

            
//
            
// TODO: Add any constructor code after InitializeComponent call
            
//
            try
            
{
                
//load original bitmap from a file
                bitmapOriginal = (Bitmap)Bitmap.FromFile(
                    
@"..\..\katie_plaintext.jpg");

                
//center to screen
                this.CenterToScreen( );
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
                    
"Error loading image. " + 
                    ex.Message );
            }

        }


        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new SteganographyForm());
        }


        
private void SteganographyForm_Paint(
            
object sender, 
            System.Windows.Forms.PaintEventArgs e)
        
{
            
try
            
{
                
//get Graphics object for painting original
                Graphics gPanelOriginal = 
                    Graphics.FromHwnd(
                        panelOriginalImage.Handle);

                
//draw original bitmap into panel
                gPanelOriginal.DrawImage(
                    bitmapOriginal, 
new Point(0 ,0));

                
//return if there is no modified image yet
                if (bitmapModified==null)
                    
return;

                
//get Graphics object for painting modified
                Graphics gPanelModified = 
                    Graphics.FromHwnd(
                        panelModifiedImage.Handle);

                
//draw modified bitmap into panel
                gPanelModified.DrawImage(
                    bitmapModified, 
new Point(0 ,0));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
                    
"Error drawing image." +
                    ex.Message);
                
this.Close( );
            }

        }


        
private void buttonHideMessage_Click(
            
object sender, System.EventArgs e)
        
{
            
try
            
{
                
//show wait cursor
                this.Cursor = Cursors.WaitCursor;

                
//start off with copy of original image
                bitmapModified = new Bitmap(
                    bitmapOriginal, 
                    bitmapOriginal.Width, 
                    bitmapOriginal.Height);

                
//get original message to be hidden
                int numberbytes = 
                    (
byte)textBoxOriginalMessage.Text.Length;
                
byte[] bytesOriginal = new byte[numberbytes+1];
                bytesOriginal[
0= (byte)numberbytes;
                Encoding.UTF8.GetBytes(
                    textBoxOriginalMessage.Text,
                    
0,
                    textBoxOriginalMessage.Text.Length,
                    bytesOriginal,
                    
1);

                
//set bits 1, 2, 3 of byte into LSB red
                
//set bits 4, 5, 6 of byte into LSB green
                
//set bits 7 and 8 of byte into LSB blue
                int byteCount = 0;//----一列一列搞定
                for (int i=0; i<bitmapOriginal.Width; i++)
                
{
                    
for (int j=0; j<bitmapOriginal.Height; j++)
                    
{
                        
if (bytesOriginal.Length==byteCount)
                            
return;
                        
//---返回当前循环的坐标点的颜色数据
                        Color clrPixelOriginal = 
                            bitmapOriginal.GetPixel(i, j);
                        
//为基元整型类型、枚举类型和 boolean 类型预定义了二元 | 运算符。对于基元整型类型和枚举类型,| 计算操作数的按位“或”。
                        
//--注意这里是对~~血迹之术-的实际应用中的变种
                        
//--这里的算法还是不太清楚估计是-防止三元素重合成1点后产生的错位--如果大大们知道麻烦告诉我下
                        byte r = 
                            (
byte)((clrPixelOriginal.R & ~0x7|
                            (bytesOriginal[byteCount]
>>0)&0x7);
                        
byte g = 
                            (
byte)((clrPixelOriginal.G & ~0x7|
                            (bytesOriginal[byteCount]
>>3)&0x7);
                        
byte b = 
                            (
byte)((clrPixelOriginal.B & ~0x3|
                            (bytesOriginal[byteCount]
>>6)&0x3);
                        byteCount
++;

                        
//set pixel to modified color
                        bitmapModified.SetPixel(
                            i, j, Color.FromArgb(r, g, b));
                    }


                   
                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
                    
"Error hiding message." +
                    ex.Message);
            }

            
finally
            
{
                
//show normal cursor
                this.Cursor = Cursors.Arrow;

                
//repaint
                Invalidate();
            }

        }


        
private void buttonExtractMessage_Click(
            
object sender, System.EventArgs e)
        
{
            
//get bytes of message from modified image
            byte[] bytesExtracted = new byte [256+1];
            
try
            
{
                
//show wait cursor, can be time-consuming
                this.Cursor = Cursors.WaitCursor;
                
                
//get bits 1, 2, 3 of byte from LSB red
                
//get bits 4, 5, 6 of byte from LSB green
                
//get bits 7 and 8 of byte from LSB blue
                int byteCount = 0;
                
for (int i=0; i<bitmapModified.Width; i++)
                
{
                    
for (int j=0; j<bitmapModified.Height; j++)
                    
{
                        
if (bytesExtracted.Length==byteCount)
                            
return;

                        Color clrPixelModified 
= 
                            bitmapModified.GetPixel(i, j);
                        
byte bits123 = 
                            (
byte)((clrPixelModified.R&0x7)<<0);
                        
byte bits456 = (
                            
byte)((clrPixelModified.G&0x7)<<3);
                        
byte bits78  = (
                            
byte)((clrPixelModified.B&0x3)<<6);
                    
                        bytesExtracted[byteCount] 
= 
                            (
byte)(bits78 |bits456 | bits123);
                        byteCount
++;
                    }


                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
                    
"Error extracting message." +
                    ex.Message);
            }

            
finally
            
{
                
//show normal cursor
                this.Cursor = Cursors.Arrow;

                
//get number of bytes from start of array
                int numberbytes = bytesExtracted[0];

                
//get remaining bytes in array into string
                textBoxExtractedlMessage.Text =  
                    Encoding.UTF8.GetString(
                    bytesExtracted,
                    
1,
                    numberbytes);
            }
        
        }


        
//shared private fields
        private Bitmap bitmapOriginal;
        
private Bitmap bitmapModified;
    }

}

posted @ 2007-04-23 23:36  苹果王子  阅读(693)  评论(2)    收藏  举报