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

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

None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
using System.Data;
None.gif
None.gif
using System.Text; 
None.gif
None.gif
namespace Steganography
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for SteganographyForm.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SteganographyForm : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button buttonHideMessage;
InBlock.gif        
private System.Windows.Forms.Panel panelOriginalImage;
InBlock.gif        
private System.Windows.Forms.TextBox textBoxOriginalMessage;
InBlock.gif        
private System.Windows.Forms.Panel panelModifiedImage;
InBlock.gif        
private System.Windows.Forms.GroupBox groupBox1;
InBlock.gif        
private System.Windows.Forms.GroupBox groupBox3;
InBlock.gif        
private System.Windows.Forms.GroupBox groupBox4;
InBlock.gif        
private System.Windows.Forms.Button buttonExtractMessage;
InBlock.gif        
private System.Windows.Forms.TextBox textBoxExtractedlMessage;
InBlock.gif        
private System.Windows.Forms.GroupBox groupBox2;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required designer variable.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public SteganographyForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Required for Windows Form Designer support
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: Add any constructor code after InitializeComponent call
InBlock.gif            
//
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//load original bitmap from a file
InBlock.gif
                bitmapOriginal = (Bitmap)Bitmap.FromFile(
InBlock.gif                    
@"..\..\katie_plaintext.jpg");
InBlock.gif
InBlock.gif                
//center to screen
InBlock.gif
                this.CenterToScreen( );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
InBlock.gif                    
"Error loading image. " + 
InBlock.gif                    ex.Message );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Clean up any resources being used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.buttonHideMessage = new System.Windows.Forms.Button();
InBlock.gif            
this.panelOriginalImage = new System.Windows.Forms.Panel();
InBlock.gif            
this.textBoxOriginalMessage = new System.Windows.Forms.TextBox();
InBlock.gif            
this.panelModifiedImage = new System.Windows.Forms.Panel();
InBlock.gif            
this.groupBox1 = new System.Windows.Forms.GroupBox();
InBlock.gif            
this.groupBox3 = new System.Windows.Forms.GroupBox();
InBlock.gif            
this.groupBox4 = new System.Windows.Forms.GroupBox();
InBlock.gif            
this.buttonExtractMessage = new System.Windows.Forms.Button();
InBlock.gif            
this.textBoxExtractedlMessage = new System.Windows.Forms.TextBox();
InBlock.gif            
this.groupBox2 = new System.Windows.Forms.GroupBox();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// buttonHideMessage
InBlock.gif            
// 
InBlock.gif
            this.buttonHideMessage.Location = new System.Drawing.Point(10594);
InBlock.gif            
this.buttonHideMessage.Name = "buttonHideMessage";
InBlock.gif            
this.buttonHideMessage.Size = new System.Drawing.Size(14425);
InBlock.gif            
this.buttonHideMessage.TabIndex = 0;
InBlock.gif            
this.buttonHideMessage.Text = "Hide Message";
InBlock.gif            
this.buttonHideMessage.Click += new System.EventHandler(this.buttonHideMessage_Click);
InBlock.gif            
// 
InBlock.gif            
// panelOriginalImage
InBlock.gif            
// 
InBlock.gif
            this.panelOriginalImage.Location = new System.Drawing.Point(1926);
InBlock.gif            
this.panelOriginalImage.Name = "panelOriginalImage";
InBlock.gif            
this.panelOriginalImage.Size = new System.Drawing.Size(412521);
InBlock.gif            
this.panelOriginalImage.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// textBoxOriginalMessage
InBlock.gif            
// 
InBlock.gif
            this.textBoxOriginalMessage.Location = new System.Drawing.Point(182594);
InBlock.gif            
this.textBoxOriginalMessage.Name = "textBoxOriginalMessage";
InBlock.gif            
this.textBoxOriginalMessage.Size = new System.Drawing.Size(24021);
InBlock.gif            
this.textBoxOriginalMessage.TabIndex = 1;
InBlock.gif            
// 
InBlock.gif            
// panelModifiedImage
InBlock.gif            
// 
InBlock.gif
            this.panelModifiedImage.Location = new System.Drawing.Point(46126);
InBlock.gif            
this.panelModifiedImage.Name = "panelModifiedImage";
InBlock.gif            
this.panelModifiedImage.Size = new System.Drawing.Size(411521);
InBlock.gif            
this.panelModifiedImage.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// groupBox1
InBlock.gif            
// 
InBlock.gif
            this.groupBox1.Location = new System.Drawing.Point(163569);
InBlock.gif            
this.groupBox1.Name = "groupBox1";
InBlock.gif            
this.groupBox1.Size = new System.Drawing.Size(27960);
InBlock.gif            
this.groupBox1.TabIndex = 3;
InBlock.gif            
this.groupBox1.TabStop = false;
InBlock.gif            
this.groupBox1.Text = "Original Message";
InBlock.gif            
// 
InBlock.gif            
// groupBox3
InBlock.gif            
// 
InBlock.gif
            this.groupBox3.Location = new System.Drawing.Point(109);
InBlock.gif            
this.groupBox3.Name = "groupBox3";
InBlock.gif            
this.groupBox3.Size = new System.Drawing.Size(432551);
InBlock.gif            
this.groupBox3.TabIndex = 3;
InBlock.gif            
this.groupBox3.TabStop = false;
InBlock.gif            
this.groupBox3.Text = "Original Image";
InBlock.gif            
// 
InBlock.gif            
// groupBox4
InBlock.gif            
// 
InBlock.gif
            this.groupBox4.Location = new System.Drawing.Point(4519);
InBlock.gif            
this.groupBox4.Name = "groupBox4";
InBlock.gif            
this.groupBox4.Size = new System.Drawing.Size(432551);
InBlock.gif            
this.groupBox4.TabIndex = 3;
InBlock.gif            
this.groupBox4.TabStop = false;
InBlock.gif            
this.groupBox4.Text = "Modified Image";
InBlock.gif            
// 
InBlock.gif            
// buttonExtractMessage
InBlock.gif            
// 
InBlock.gif
            this.buttonExtractMessage.Location = new System.Drawing.Point(451594);
InBlock.gif            
this.buttonExtractMessage.Name = "buttonExtractMessage";
InBlock.gif            
this.buttonExtractMessage.Size = new System.Drawing.Size(14425);
InBlock.gif            
this.buttonExtractMessage.TabIndex = 2;
InBlock.gif            
this.buttonExtractMessage.Text = "Extract Message";
InBlock.gif            
this.buttonExtractMessage.Click += new System.EventHandler(this.buttonExtractMessage_Click);
InBlock.gif            
// 
InBlock.gif            
// textBoxExtractedlMessage
InBlock.gif            
// 
InBlock.gif
            this.textBoxExtractedlMessage.Location = new System.Drawing.Point(624594);
InBlock.gif            
this.textBoxExtractedlMessage.Name = "textBoxExtractedlMessage";
InBlock.gif            
this.textBoxExtractedlMessage.ReadOnly = true;
InBlock.gif            
this.textBoxExtractedlMessage.Size = new System.Drawing.Size(24021);
InBlock.gif            
this.textBoxExtractedlMessage.TabIndex = 3;
InBlock.gif            
// 
InBlock.gif            
// groupBox2
InBlock.gif            
// 
InBlock.gif
            this.groupBox2.Location = new System.Drawing.Point(605569);
InBlock.gif            
this.groupBox2.Name = "groupBox2";
InBlock.gif            
this.groupBox2.Size = new System.Drawing.Size(27860);
InBlock.gif            
this.groupBox2.TabIndex = 3;
InBlock.gif            
this.groupBox2.TabStop = false;
InBlock.gif            
this.groupBox2.Text = "Extractedl Message";
InBlock.gif            
// 
InBlock.gif            
// SteganographyForm
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(930672);
InBlock.gif            
this.Controls.Add(this.textBoxOriginalMessage);
InBlock.gif            
this.Controls.Add(this.buttonHideMessage);
InBlock.gif            
this.Controls.Add(this.panelModifiedImage);
InBlock.gif            
this.Controls.Add(this.groupBox1);
InBlock.gif            
this.Controls.Add(this.panelOriginalImage);
InBlock.gif            
this.Controls.Add(this.groupBox3);
InBlock.gif            
this.Controls.Add(this.groupBox4);
InBlock.gif            
this.Controls.Add(this.buttonExtractMessage);
InBlock.gif            
this.Controls.Add(this.textBoxExtractedlMessage);
InBlock.gif            
this.Controls.Add(this.groupBox2);
InBlock.gif            
this.Name = "SteganographyForm";
InBlock.gif            
this.Text = "Steganography";
InBlock.gif            
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SteganographyForm_Paint);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif            
this.PerformLayout();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new SteganographyForm());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void SteganographyForm_Paint(
InBlock.gif            
object sender, 
InBlock.gif            System.Windows.Forms.PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//get Graphics object for painting original
InBlock.gif
                Graphics gPanelOriginal = 
InBlock.gif                    Graphics.FromHwnd(
InBlock.gif                        panelOriginalImage.Handle);
InBlock.gif
InBlock.gif                
//draw original bitmap into panel
InBlock.gif
                gPanelOriginal.DrawImage(
InBlock.gif                    bitmapOriginal, 
new Point(0 ,0));
InBlock.gif
InBlock.gif                
//return if there is no modified image yet
InBlock.gif
                if (bitmapModified==null)
InBlock.gif                    
return;
InBlock.gif
InBlock.gif                
//get Graphics object for painting modified
InBlock.gif
                Graphics gPanelModified = 
InBlock.gif                    Graphics.FromHwnd(
InBlock.gif                        panelModifiedImage.Handle);
InBlock.gif
InBlock.gif                
//draw modified bitmap into panel
InBlock.gif
                gPanelModified.DrawImage(
InBlock.gif                    bitmapModified, 
new Point(0 ,0));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
InBlock.gif                    
"Error drawing image." +
InBlock.gif                    ex.Message);
InBlock.gif                
this.Close( );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void buttonHideMessage_Click(
InBlock.gif            
object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//show wait cursor
InBlock.gif
                this.Cursor = Cursors.WaitCursor;
InBlock.gif
InBlock.gif                
//start off with copy of original image
InBlock.gif
                bitmapModified = new Bitmap(
InBlock.gif                    bitmapOriginal, 
InBlock.gif                    bitmapOriginal.Width, 
InBlock.gif                    bitmapOriginal.Height);
InBlock.gif
InBlock.gif                
//get original message to be hidden
InBlock.gif
                int numberbytes = 
InBlock.gif                    (
byte)textBoxOriginalMessage.Text.Length;
InBlock.gif                
byte[] bytesOriginal = new byte[numberbytes+1];
InBlock.gif                bytesOriginal[
0= (byte)numberbytes;
InBlock.gif                Encoding.UTF8.GetBytes(
InBlock.gif                    textBoxOriginalMessage.Text,
InBlock.gif                    
0,
InBlock.gif                    textBoxOriginalMessage.Text.Length,
InBlock.gif                    bytesOriginal,
InBlock.gif                    
1);
InBlock.gif
InBlock.gif                
//set bits 1, 2, 3 of byte into LSB red
InBlock.gif                
//set bits 4, 5, 6 of byte into LSB green
InBlock.gif                
//set bits 7 and 8 of byte into LSB blue
InBlock.gif
                int byteCount = 0;//----一列一列搞定
InBlock.gif
                for (int i=0; i<bitmapOriginal.Width; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int j=0; j<bitmapOriginal.Height; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (bytesOriginal.Length==byteCount)
InBlock.gif                            
return;
InBlock.gif                        
//---返回当前循环的坐标点的颜色数据
InBlock.gif
                        Color clrPixelOriginal = 
InBlock.gif                            bitmapOriginal.GetPixel(i, j);
InBlock.gif                        
//为基元整型类型、枚举类型和 boolean 类型预定义了二元 | 运算符。对于基元整型类型和枚举类型,| 计算操作数的按位“或”。
InBlock.gif                        
//--注意这里是对~~血迹之术-的实际应用中的变种
InBlock.gif                        
//--这里的算法还是不太清楚估计是-防止三元素重合成1点后产生的错位--如果大大们知道麻烦告诉我下
InBlock.gif
                        byte r = 
InBlock.gif                            (
byte)((clrPixelOriginal.R & ~0x7|
InBlock.gif                            (bytesOriginal[byteCount]
>>0)&0x7);
InBlock.gif                        
byte g = 
InBlock.gif                            (
byte)((clrPixelOriginal.G & ~0x7|
InBlock.gif                            (bytesOriginal[byteCount]
>>3)&0x7);
InBlock.gif                        
byte b = 
InBlock.gif                            (
byte)((clrPixelOriginal.B & ~0x3|
InBlock.gif                            (bytesOriginal[byteCount]
>>6)&0x3);
InBlock.gif                        byteCount
++;
InBlock.gif
InBlock.gif                        
//set pixel to modified color
InBlock.gif
                        bitmapModified.SetPixel(
InBlock.gif                            i, j, Color.FromArgb(r, g, b));
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                   
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
InBlock.gif                    
"Error hiding message." +
InBlock.gif                    ex.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//show normal cursor
InBlock.gif
                this.Cursor = Cursors.Arrow;
InBlock.gif
InBlock.gif                
//repaint
InBlock.gif
                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void buttonExtractMessage_Click(
InBlock.gif            
object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//get bytes of message from modified image
InBlock.gif
            byte[] bytesExtracted = new byte [256+1];
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//show wait cursor, can be time-consuming
InBlock.gif
                this.Cursor = Cursors.WaitCursor;
InBlock.gif                
InBlock.gif                
//get bits 1, 2, 3 of byte from LSB red
InBlock.gif                
//get bits 4, 5, 6 of byte from LSB green
InBlock.gif                
//get bits 7 and 8 of byte from LSB blue
InBlock.gif
                int byteCount = 0;
InBlock.gif                
for (int i=0; i<bitmapModified.Width; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int j=0; j<bitmapModified.Height; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (bytesExtracted.Length==byteCount)
InBlock.gif                            
return;
InBlock.gif
InBlock.gif                        Color clrPixelModified 
= 
InBlock.gif                            bitmapModified.GetPixel(i, j);
InBlock.gif                        
byte bits123 = 
InBlock.gif                            (
byte)((clrPixelModified.R&0x7)<<0);
InBlock.gif                        
byte bits456 = (
InBlock.gif                            
byte)((clrPixelModified.G&0x7)<<3);
InBlock.gif                        
byte bits78  = (
InBlock.gif                            
byte)((clrPixelModified.B&0x3)<<6);
InBlock.gif                    
InBlock.gif                        bytesExtracted[byteCount] 
= 
InBlock.gif                            (
byte)(bits78 |bits456 | bits123);
InBlock.gif                        byteCount
++;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
InBlock.gif                    
"Error extracting message." +
InBlock.gif                    ex.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//show normal cursor
InBlock.gif
                this.Cursor = Cursors.Arrow;
InBlock.gif
InBlock.gif                
//get number of bytes from start of array
InBlock.gif
                int numberbytes = bytesExtracted[0];
InBlock.gif
InBlock.gif                
//get remaining bytes in array into string
InBlock.gif
                textBoxExtractedlMessage.Text =  
InBlock.gif                    Encoding.UTF8.GetString(
InBlock.gif                    bytesExtracted,
InBlock.gif                    
1,
InBlock.gif                    numberbytes);
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//shared private fields
InBlock.gif
        private Bitmap bitmapOriginal;
InBlock.gif        
private Bitmap bitmapModified;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/ajaxren/archive/2007/04/23/724489.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值