⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hideinbmpdlg.cpp

📁 参考了电脑爱好者2000第10期的一篇关于将文件隐藏于位图中的文章
💻 CPP
字号:
// HideInBmpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HideInBmp.h"
#include "HideInBmpDlg.h"
#include "About.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//本程序利用bmp的RGB的低2,1,3位
//10l位图相对于文件的偏移
//28l像素的位数
//何建敏2000年6月5日
/////////////////////////////////////////////////////////////////////////////
// CHideInBmpDlg dialog

CHideInBmpDlg::CHideInBmpDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHideInBmpDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHideInBmpDlg)
		// 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);
	for(int i=0;i<8;i++)
	{
		mask[i]=1<<i;
	}
}

void CHideInBmpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHideInBmpDlg)
	DDX_Control(pDX, IDC_PUPINTOBMP, m_PutIntoBmp);
	DDX_Control(pDX, IDC_GETFROMBMP, m_GetFromBmp);
	DDX_Control(pDX, IDC_ABOUT, m_About);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHideInBmpDlg, CDialog)
	//{{AFX_MSG_MAP(CHideInBmpDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PUPINTOBMP, OnPupintobmp)
	ON_BN_CLICKED(IDC_GETFROMBMP, OnGetfrombmp)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHideInBmpDlg message handlers

BOOL CHideInBmpDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
    m_About.SetIcon(IDI_ABOUT);
    m_PutIntoBmp.SetIcon(IDI_PUT);
	m_GetFromBmp.SetIcon(IDI_GET);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CHideInBmpDlg::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 CHideInBmpDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHideInBmpDlg::OnPupintobmp() 
{
	CFileDialog m_Dialog(TRUE,NULL,"*.bmp",NULL,"位图文件(*.bmp)");
	m_Dialog.DoModal();
	CString FileName,FileName1,FileTitle,FileExt;
	unsigned int file_len=0,pic_offset=0,bmp_len=0,len=0;
	state='r';
	number=0;
	FileName=m_Dialog.GetPathName();
	FileName.ReleaseBuffer();
	CFileDialog m_Dialog1(TRUE,NULL,"*.*",NULL,"所有文件(*.*)");
	m_Dialog1.DoModal();
	FileName1=m_Dialog1.GetPathName();
	FileName1.ReleaseBuffer();
	try
	{
        m_File1.Open(FileName,CFile::modeReadWrite|CFile::typeBinary);
	    m_File1.Seek(28l,0);
		m_File1.Read(&byte_buf,1);
		if(byte_buf!=24)
		{
			m_File1.Close();
			MessageBox("你所选择的位图文件不是24位","错误",MB_OK|MB_ICONSTOP);
			return;
		}
		m_File1.Seek(10l,0);
		bmp_len=m_File1.GetLength();
		m_File1.Read(&pic_offset,1);
		bmp_len-=pic_offset-1;
		m_File2.Open(FileName1,CFile::modeRead|CFile::typeBinary);
		file_len=m_File2.GetLength();
		if(file_len>=bmp_len/4-45)
		{
			m_File1.Close();
			m_File2.Close();
			MessageBox("你所选择的位图不足入下隐藏文件!","错误",MB_OK|MB_ICONSTOP);
			return;
		}
		m_File1.Seek(pic_offset,0);
		int len1,len2,len3,len4;
		len1=file_len&0xff;
		len2=(file_len>>8)&0xff;
		len3=(file_len>>16)&0xff;
		len4=(file_len>>24)&0xff;
		Put(len1);
		Put(len2);
		Put(len3);
		Put(len4);
		char *filetitle,*fileext;
		FileTitle=m_Dialog1.GetFileTitle();
		FileExt=m_Dialog1.GetFileExt();
		FileTitle.ReleaseBuffer();
		FileTitle.ReleaseBuffer();
		filetitle=FileTitle.GetBuffer(FileTitle.GetLength());
		fileext=FileExt.GetBuffer(FileExt.GetLength());
		for(int i=0;i<8;i++)
		{
			if(i<FileTitle.GetLength())
			{
				Put(*filetitle);
				filetitle++;
			}
			else
			{
				char temp='*';
				Put(temp);
			}
		}
		for(i=0;i<3;i++)
		{
			if(i<FileExt.GetLength())
			{
				Put(*fileext);
				fileext++;
			}
			else
			{
				char temp='*';
				Put(temp);
			}
		}
		while(len<file_len)
		{
			m_File2.Read(&byte_buf,1);
			Put(byte_buf);
			len++;
		}
		m_File2.Close();
		m_File1.Close();
		CString ok;
		ok.Format("已将%s加入%s",FileName1,FileName);
		MessageBox(ok,"提示",MB_OK);
	}
	catch( CFileException* fe )
	{
		switch(fe->m_cause)
		{
		case CFileException::fileNotFound:
			MessageBox("没有找到文件,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		default:
			MessageBox("文件错误,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		}
		fe->Delete();
	}	
}

void CHideInBmpDlg::Put(char byte_buf)
{
	try{
		m_File1.Read(byte_bufs,5);
		if(state=='r'&&number==0)
		{
			if(byte_buf&mask[0]) byte_bufs[0]|=1;
			else byte_bufs[0]&=0xfe;
			if((byte_buf&mask[1])>>1) byte_bufs[0]|=2;
			else byte_bufs[0]&=0xfd;
			if((byte_buf&mask[2])>>2) byte_bufs[1]|=1;
			else byte_bufs[1]&=0xfe;
			if((byte_buf&mask[3])>>3) byte_bufs[2]|=1;
			else byte_bufs[2]&=0xfe;
			if((byte_buf&mask[4])>>4) byte_bufs[2]|=2;
			else byte_bufs[2]&=0xfd;
			if((byte_buf&mask[5])>>5) byte_bufs[2]|=4;
			else byte_bufs[2]&=0xfb;
			if((byte_buf&mask[6])>>6) byte_bufs[3]|=1;
			else byte_bufs[3]&=0xfe;
			if((byte_buf&mask[7])>>7) byte_bufs[3]|=2;
			else byte_bufs[3]&=0xfd;
			m_File1.Seek(-5l,1);
			m_File1.Write(byte_bufs,5);
			state='g';
			number=0;
			m_File1.Seek(-1l,1);
			return;
		}
		if(state=='g'&&number==0)
		{
			if(byte_buf&mask[0]) byte_bufs[0]|=1;
			else byte_bufs[0]&=0xfe;
			if((byte_buf&mask[1])>>1) byte_bufs[1]|=1;
			else byte_bufs[1]&=0xfe;
			if((byte_buf&mask[2])>>2) byte_bufs[1]|=2;
			else byte_bufs[1]&=0xfd;
			if((byte_buf&mask[3])>>3) byte_bufs[1]|=4;
			else byte_bufs[1]&=0xfb;
			if((byte_buf&mask[4])>>4) byte_bufs[2]|=1;
			else byte_bufs[2]&=0xfe;
			if((byte_buf&mask[5])>>5) byte_bufs[2]|=2;
			else byte_bufs[2]&=0xfd;
			if((byte_buf&mask[6])>>6) byte_bufs[3]|=1;
			else byte_bufs[3]&=0xfe;
			if((byte_buf&mask[7])>>7) byte_bufs[4]|=1;
			else byte_bufs[4]&=0xfe;
			m_File1.Seek(-5l,1);
			m_File1.Write(byte_bufs,5);
			state='b';
			number=1;
			m_File1.Seek(-1l,1);
			return;
		}
		if(state=='b'&&number==1)
		{
			if(byte_buf&mask[0]) byte_bufs[0]|=2;
			else byte_bufs[0]&=0xfd;
			if((byte_buf&mask[1])>>1) byte_bufs[0]|=4;
			else byte_bufs[0]&=0xfb;
			if((byte_buf&mask[2])>>2) byte_bufs[1]|=1;
			else byte_bufs[1]&=0xfe;
			if((byte_buf&mask[3])>>3) byte_bufs[1]|=2;
			else byte_bufs[1]&=0xfd;
			if((byte_buf&mask[4])>>4) byte_bufs[2]|=1;
			else byte_bufs[2]&=0xfe;
			if((byte_buf&mask[5])>>5) byte_bufs[3]|=1;
			else byte_bufs[3]&=0xfe;
			if((byte_buf&mask[6])>>6) byte_bufs[3]|=2;
			else byte_bufs[3]&=0xfd;
			if((byte_buf&mask[7])>>7) byte_bufs[3]|=4;
			else byte_bufs[3]&=0xfb;
			m_File1.Seek(-5l,1);
			m_File1.Write(byte_bufs,5);
			m_File1.Seek(-1l,1);
			state='r';
			number=0;
			return;
		}
	}
	catch( CFileException* fe )
	{
		switch(fe->m_cause)
		{
		case CFileException::fileNotFound:
			MessageBox("没有找到文件,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		default:
			MessageBox("文件错误,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		}
		fe->Delete();
	}	
	return;

}

void CHideInBmpDlg::OnGetfrombmp() 
{
	try
	{
		CFileDialog m_Dialog(TRUE,NULL,"*.bmp",NULL,"位图文件(*.bmp)");
		m_Dialog.DoModal();
		CString FilePath,FileTitle,FileExt;
		state='r';
    	number=0;
		FilePath=m_Dialog.GetPathName();
		FilePath.ReleaseBuffer();
		unsigned int bmp_len=0,pic_offset=0,len=0,file_len=0;
		char tag;
		m_File1.Open(FilePath,CFile::modeRead|CFile::typeBinary);
		m_File1.Seek(28l,0);
		m_File1.Read(&tag,1);
		if(tag!=24)
		{
			m_File1.Close();
			MessageBox("你所选择的位图文件不是24位","错误",MB_OK|MB_ICONSTOP);
			return;			
		}
        m_File1.Seek(10l,0);
		m_File1.Read(&pic_offset,1);
		m_File1.Seek(pic_offset,0);
		int len1,len2,len3,len4;
		len1=Get();
		len2=Get();
		len3=Get();
		len4=Get();
		file_len=len1+(len2<<8)+(len3<<16)+(len4<<24);
		for(int i=0;i<8;i++)
		{
			char temp;
			temp=Get();
			if(temp!='*')
				FileTitle+=temp;
		}
		for(i=0;i<3;i++)
		{
			char temp;
			temp=Get();
			if(temp!='*')
				FileExt+=temp;
		}
		FilePath=m_Dialog.GetPathName();
		int le=FilePath.GetLength();
		for(i=0;i<=le-1;i++)
		{
			if(FilePath.Right(1)!="\\") FilePath.Delete(le-1-i,1);
			else break;
		}
		FilePath+=FileTitle;
		FilePath+=".";
		FilePath+=FileExt;
		m_File2.Open(FilePath,CFile::modeCreate|CFile::typeBinary|CFile::modeWrite);
		while(len<file_len)
		{
			char temp;
			temp=Get();
			m_File2.Write(&temp,1);
			len++;
		}
		m_File1.Close();
		m_File2.Close();
		CString String;
		String.Format("已将%s从位图文件中取出",FilePath);
		MessageBox(String,"提示",MB_OK);
	}
	catch( CFileException* fe )
	{

		switch(fe->m_cause)
		{
		case CFileException::fileNotFound:
			MessageBox("没有找到文件,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		default:
			MessageBox("文件错误,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		}
		fe->Delete();
	}	
	return;
	
}

int CHideInBmpDlg::Get()
{
	try
	{
		m_File1.Read(&byte_bufs,5);
		byte_buf=0;
		if(state=='r'&&number==0)
		{
			if(byte_bufs[0]&1==1)      byte_buf|=mask[0];
			if((byte_bufs[0]>>1)&1==1) byte_buf|=mask[1];
			if(byte_bufs[1]&1==1)      byte_buf|=mask[2];
			if(byte_bufs[2]&1==1)      byte_buf|=mask[3];
			if((byte_bufs[2]>>1)&1==1) byte_buf|=mask[4];
			if((byte_bufs[2]>>2)&1==1) byte_buf|=mask[5];
			if(byte_bufs[3]&1==1)      byte_buf|=mask[6];
			if((byte_bufs[3]>>1)&1==1) byte_buf|=mask[7];
			m_File1.Seek(-1l,1);
			state='g';
			number=0;
			return byte_buf;
		}
		if(state=='g'&&number==0)
		{
			if(byte_bufs[0]&1==1)      byte_buf|=mask[0];
			if(byte_bufs[1]&1==1)      byte_buf|=mask[1];
			if((byte_bufs[1]>>1)&1==1) byte_buf|=mask[2];
			if((byte_bufs[1]>>2)&1==1) byte_buf|=mask[3];
			if(byte_bufs[2]&1==1)      byte_buf|=mask[4];
			if((byte_bufs[2]>>1)&1==1) byte_buf|=mask[5];
			if(byte_bufs[3]&1==1)      byte_buf|=mask[6];
			if(byte_bufs[4]&1==1)      byte_buf|=mask[7];
			m_File1.Seek(-1l,1);
			state='b';
			number=1;
			return byte_buf;
		}
		if(state=='b'&&number==1)
		{
			if((byte_bufs[0]>>1)&1==1) byte_buf|=mask[0];
			if((byte_bufs[0]>>2)&1==1) byte_buf|=mask[1];
			if(byte_bufs[1]&1==1)      byte_buf|=mask[2];
			if((byte_bufs[1]>>1)&1==1) byte_buf|=mask[3];
			if(byte_bufs[2]&1==1)      byte_buf|=mask[4];
			if(byte_bufs[3]&1==1)      byte_buf|=mask[5];
			if((byte_bufs[3]>>1)&1==1) byte_buf|=mask[6];
			if((byte_bufs[3]>>2)&1==1) byte_buf|=mask[7];
			m_File1.Seek(-1l,1);
			state='r';
			number=0;
			return byte_buf;
		}
	}
	catch( CFileException* fe )
	{
		switch(fe->m_cause)
		{
		case CFileException::fileNotFound:
			MessageBox("没有找到文件,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		default:
			MessageBox("文件错误,程序不能继续.","错误",MB_OK|MB_ICONERROR);
		}
		fe->Delete();
	}	    
	return 0;
}

void CHideInBmpDlg::OnAbout() 
{
	CAbout m_about;
	m_about.DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -