[原]VC++信息安全编程(15)基于一阶Bézier曲线的信息隐藏
信息隐藏指在设计和确定模块时,使得一个模块内包含的特定信息(过程或数据),对于不需要这些信息的其他模块来说,是透明的。传统的信息隐藏起源于古老的隐写术。如在古希腊战争中,为了安全地传送军事情报,奴隶主剃光奴隶 的头发,将情报文在奴隶的头皮上,待头发长起后再派出去传送消息。我国古代也早有以藏头诗、藏尾诗、漏格诗以及绘画等形式,将要表达的意思和“密语”隐藏在诗文或画卷中的特定位置,一般人只注意诗或画的表面意境,而不会去注意或破解隐藏其中的密语。 信息隐藏的发展历史可以一直追溯到"匿形术(Steganography)"的使用。"匿形术"一词来源于古希腊文中"隐藏的"和"图形"两个词语的组合。虽然"匿形术"与"密码术(Cryptography)"都是致力于信息的保密技术,但是,两者的设计思想却完全不同。"密码术"主要通过设计加密技术,使保密信息不可读,但是对于非授权者来讲,虽然他无法获知保密信息的具体内容,却能意识到保密信息的存在。而"匿形术"则致力于通过设计精妙的方法,使得非授权者根本无从得知保密信息的存在与否。相对于现代密码学来讲,信息隐藏的最大优势在于它并不限制对主信号的存取和访问,而是致力于签字信号的安全保密性。
我们来实践一个基于一阶Bézier曲线的信息隐藏编程实例 ,需要一定的数学基础,请见代码分析。
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDataHideInBMPBZDlg dialog
CDataHideInBMPBZDlg::CDataHideInBMPBZDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDataHideInBMPBZDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDataHideInBMPBZDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDataHideInBMPBZDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDataHideInBMPBZDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDataHideInBMPBZDlg, CDialog)
//{{AFX_MSG_MAP(CDataHideInBMPBZDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BASEBMP_HIDE, OnBasebmpHide)
ON_BN_CLICKED(IDC_HIDEFILE, OnHidefile)
ON_BN_CLICKED(IDC_DOHIDE, OnDohide)
ON_BN_CLICKED(IDC_BASEBMP_RESUME, OnBasebmpResume)
ON_BN_CLICKED(IDC_BASEFILE, OnBasefile)
ON_BN_CLICKED(IDC_RESUMEDATA, OnResumedata)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDataHideInBMPBZDlg message handlers
BOOL CDataHideInBMPBZDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog.The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
t=0.035; //融合系数
m_bBaseBMPHide=false;
m_bBaseBMPResume=false;
m_bHidefile=false;
m_bBasefile=false;
GetDlgItem(IDC_DOHIDE)->EnableWindow(m_bBaseBMPHide && m_bHidefile);
GetDlgItem(IDC_RESUMEDATA)->EnableWindow(m_bBaseBMPResume && m_bBasefile);
return TRUE;// return TRUEunless you set the focus to a control
}
void CDataHideInBMPBZDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
//to draw the icon.For MFC applications using the document/view model,
//this is automatically done for you by the framework.
void CDataHideInBMPBZDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
//the minimized window.
HCURSOR CDataHideInBMPBZDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
unsigned char CDataHideInBMPBZDlg::HideCalculate(unsigned char B, unsigned char H)
{
unsigned char R=(int)((1-t)*B+0.5f)+(int)(t*H+0.5f);
return R;
}
unsigned char CDataHideInBMPBZDlg::ShowCalculate(unsigned char C, unsigned char B)
{
unsigned char R=(int)((C- (int)((1-t)*B+0.5f))/t+0.5f);
if(R%32>15)
R+=15;
R=(int)(R/32.0f+0.5f);
return R;
}
void CDataHideInBMPBZDlg::OnBasebmpHide()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"载体位图文件(*.bmp)|*.bmp||",NULL);
if(dlg.DoModal()==IDOK)
{
m_bBaseBMPHide=true;
m_sCarrierFileHide=dlg.GetPathName();
GetDlgItem(IDC_BASEBMP_HIDE)->SetWindowText(m_sCarrierFileHide);
}
else
m_bBaseBMPHide=false;
GetDlgItem(IDC_DOHIDE)->EnableWindow(m_bBaseBMPHide && m_bHidefile);
}
void CDataHideInBMPBZDlg::OnHidefile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,"*","*.*",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"待隐藏文件(*.*)|*.*||",NULL);
if(dlg.DoModal()==IDOK)
{
m_bHidefile=true;
m_sHideFile=dlg.GetPathName();
m_sHideFileName=dlg.GetFileName();
GetDlgItem(IDC_HIDEFILE)->SetWindowText(m_sHideFileName);
}
else
m_bHidefile=false;
GetDlgItem(IDC_DOHIDE)->EnableWindow(m_bBaseBMPHide && m_bHidefile);
}
void CDataHideInBMPBZDlg::OnDohide()
{
// TODO: Add your control notification handler code here
CFile BaseFile,HideFile;
DWORD BaseFileLen,HideFileLen;
unsigned char* BaseData;
unsigned char* HideData;
if(m_bBaseBMPHide==true && m_bHidefile==true)
{
BaseFile.Open(m_sCarrierFileHide,CFile::modeReadWrite);
BaseFileLen=BaseFile.GetLength();
HideFile.Open(m_sHideFile,CFile::modeReadWrite);
HideFileLen=HideFile.GetLength();
//信息头:文件名*文件长*
CString InfoHead;
InfoHead.Format("%s*%d*",m_sHideFileName,HideFileLen);
int HeadLen=InfoHead.GetLength();
if(BaseFileLen<((HeadLen+HideFileLen)*8)/3+100)
{
AfxMessageBox("待隐藏文件过大!请换一较大载体位图文件。");
return;
}
BaseData=new unsigned char;
BaseFile.Read(BaseData,BaseFileLen);
HideData=new unsigned char;
//对明文进行组帧
for(int k=0;k<HeadLen;k++)
HideData=InfoHead.GetAt(k);
HideFile.Read(HideData+HeadLen,HideFileLen);
for(DWORD kk=HeadLen+HideFileLen;kk<BaseFileLen;kk++)
HideData=0;
if(BaseData!=24)
{
AfxMessageBox("载体文件格式错误!");
BaseFile.Close();
if(BaseData!=NULL)
delete BaseData;
return;
}
unsigned char Temp=0;
DWORD j=0;
for(DWORD i=54;i<BaseFileLen;i++)
{
Temp=((HideData&224)>>5)<<5; //1
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=((HideData&28)>>2)<<5; //2
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=(HideData&3)<<1; //3
j++;
Temp|=(HideData&128)>>7;
Temp*=32;
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=((HideData&112)>>4)<<5; //4
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=((HideData&14)>>1)<<5; //5
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=(HideData&1)<<2; //6
j++;
Temp|=(HideData&192)>>6;
Temp*=32;
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=((HideData&56)>>3)<<5; //7
BaseData=HideCalculate(BaseData,Temp);
i++;
Temp=(HideData&7)<<5; //8
BaseData=HideCalculate(BaseData,Temp);
j++;
}
BaseFile.Close();
HideFile.Close();
CFile file;
file.Open("融合图像.bmp",CFile::modeCreate|CFile::modeReadWrite);
file.Write(BaseData,BaseFileLen);
file.Close();
AfxMessageBox("信息隐藏完毕!");
if(BaseData!=NULL)
delete BaseData;
if(HideData!=NULL)
delete HideData;
}
}
void CDataHideInBMPBZDlg::OnBasebmpResume()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"载体位图文件(*.bmp)|*.bmp||",NULL);
if(dlg.DoModal()==IDOK)
{
m_bBaseBMPResume=true;
m_sCarrierFileResume=dlg.GetPathName();
GetDlgItem(IDC_BASEBMP_RESUME)->SetWindowText(m_sCarrierFileResume);
}
else
m_bBaseBMPResume=false;
GetDlgItem(IDC_RESUMEDATA)->EnableWindow(m_bBaseBMPResume && m_bBasefile);
}
void CDataHideInBMPBZDlg::OnBasefile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"原始文件(*.bmp)|*.bmp||",NULL);
if(dlg.DoModal()==IDOK)
{
m_bBasefile=true;
m_sBaseFile=dlg.GetPathName();
GetDlgItem(IDC_BASEFILE)->SetWindowText(m_sBaseFile);
}
else
m_bBasefile=false;
GetDlgItem(IDC_RESUMEDATA)->EnableWindow(m_bBaseBMPResume && m_bBasefile);
}
void CDataHideInBMPBZDlg::OnResumedata()
{
// TODO: Add your control notification handler code here
CFile BaseFile;
CFile CarrierFile;
DWORD BaseFileLen;
DWORD CarrierFileLen;
unsigned char* BaseData;
unsigned char* CarrierData;
if(m_bBaseBMPResume==true && m_bBasefile==true)
{
CarrierFile.Open(m_sCarrierFileResume,CFile::modeReadWrite);
CarrierFileLen=CarrierFile.GetLength();
BaseFile.Open(m_sBaseFile,CFile::modeReadWrite);
BaseFileLen=BaseFile.GetLength();
if(BaseFileLen!=CarrierFileLen)
{
AfxMessageBox("选取的不是充作载体的原始文件!");
CarrierFile.Close();
BaseFile.Close();
return;
}
CarrierData=new unsigned char;
CarrierFile.Read(CarrierData,CarrierFileLen);
BaseData=new unsigned char;
BaseFile.Read(BaseData,CarrierFileLen);
if(CarrierData!=24)
{
AfxMessageBox("载体文件格式错误!");
CarrierFile.Close();
BaseFile.Close();
if(CarrierData!=NULL)
delete CarrierData;
if(BaseData!=NULL)
delete BaseData;
return;
}
if(BaseData!=24)
{
AfxMessageBox("原始文件格式错误!");
CarrierFile.Close();
BaseFile.Close();
if(CarrierData!=NULL)
delete CarrierData;
if(BaseData!=NULL)
delete BaseData;
return;
}
unsigned char Temp=0;
DWORD j=0;
for(DWORD i=54;i<CarrierFileLen;i++)
{
Temp=(ShowCalculate(CarrierData,BaseData)&7)<<5;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&7)<<2;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&6)>>1;
CarrierData=Temp;
j++;
Temp=(ShowCalculate(CarrierData,BaseData)&1)<<7;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&7)<<4;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&7)<<1;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&4)>>2;
CarrierData=Temp;
j++;
Temp=(ShowCalculate(CarrierData,BaseData)&3)<<6;
i++;
Temp|=(ShowCalculate(CarrierData,BaseData)&7)<<3;
i++;
Temp|=ShowCalculate(CarrierData,BaseData)&7;
CarrierData=Temp;
j++;
}
BaseFile.Close();
CarrierFile.Close();
j=0;
CString FileName="";
while(true)
{
if(CarrierData=='*')
{
j++;
break;
}
else
{
FileName+=CString(CarrierData);
j++;
}
}
CString FileLen="";
while(true)
{
if(CarrierData=='*')
{
j++;
break;
}
else
{
FileLen+=CString(CarrierData);
j++;
}
}
BaseFileLen=atoi(FileLen);
CFile file;
file.Open(FileName,CFile::modeCreate|CFile::modeReadWrite);
file.Write(CarrierData+j,BaseFileLen);
file.Close();
AfxMessageBox("信息恢复完毕!");
if(BaseData!=NULL)
delete BaseData;
if(CarrierData!=NULL)
delete CarrierData;
}
}
作者:yincheng01 发表于2011-12-15 7:46:03 原文链接
页:
[1]