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

📄 infohidedlg.cpp

📁 信息隐藏
💻 CPP
字号:
// InfoHideDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InfoHide.h"
#include "InfoHideDlg.h"

#define BUFFER_SIZE_FIVE_KB 1024*5

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInfoHideDlg dialog

CInfoHideDlg::CInfoHideDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CInfoHideDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInfoHideDlg)
		// 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 CInfoHideDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInfoHideDlg)
	DDX_Text(pDX, IDC_STATIC_FILE_INFO, m_strFilesInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInfoHideDlg, CDialog)
	//{{AFX_MSG_MAP(CInfoHideDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_HIDE_INFO, OnButtonHideInfo)
	ON_BN_CLICKED(IDC_BUTTON_RESUME_INFO, OnButtonResumeInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInfoHideDlg message handlers

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

void CInfoHideDlg::OnButtonHideInfo() 
{
	// TODO: Add your control notification handler code here
	//获取文件路径,以便打开
	CString	strBmpRead = "", strBmpWrite = "", strTxtRead = "";
	CFileDialog	fdFileOpen(true);
	fdFileOpen.m_ofn.lpstrTitle		=	"打开宿主BMP文件";
	fdFileOpen.m_ofn.lpstrFilter	=	"位图文件(*.bmp)\0*.bmp\0\0";
	fdFileOpen.m_ofn.Flags			|=	OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
	if( fdFileOpen.DoModal()==IDOK )
		strBmpRead	=	fdFileOpen.GetPathName();

	fdFileOpen.m_ofn.lpstrTitle		=	"打开寄生文件";
	fdFileOpen.m_ofn.lpstrFilter	=	"文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
	fdFileOpen.m_ofn.Flags			|=	OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
	if( fdFileOpen.DoModal()==IDOK )
		strTxtRead	=	fdFileOpen.GetPathName();

	CFileDialog	fdFileSave(false);
	fdFileSave.m_ofn.lpstrTitle	=	"保存水印后的BMP文件";
	fdFileSave.m_ofn.lpstrFilter	=	"位图文件(*.bmp)\0*.bmp\0\0";
	fdFileSave.m_ofn.lpstrDefExt	=	"bmp";
	fdFileSave.m_ofn.Flags			|=	OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
	if( fdFileSave.DoModal()==IDOK )
		strBmpWrite	=	fdFileSave.GetPathName();

	//控件显示文件信息
	m_strFilesInfo.Format(	"宿主BMP文件:%s\n\n寄生文件:%s\n\n水印后的BMP文件:%s",
								strBmpRead, strTxtRead, strBmpWrite	);
	if( strBmpRead.IsEmpty() || strTxtRead.IsEmpty() || strBmpWrite.IsEmpty() )
	{
		m_strFilesInfo	+=	"\n\n文件路径没有指定,无法隐藏信息!";
		UpdateData(false);
		return;
	}
	UpdateData(false);

	//打开文件
	CFile	fBmpRead, fBmpWrite, fTxtRead;
	fBmpRead.Open(strBmpRead, CFile::modeRead);
	fTxtRead.Open(strTxtRead, CFile::modeRead);
	fBmpWrite.Open(strBmpWrite, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);

	//寄生文件过大,不能隐藏信息,控件显示错误信息
	int		nBmpFileLen	=	fBmpRead.GetLength();
	int		nTxtFileLen	=	fTxtRead.GetLength();
	if( ( nTxtFileLen*8 + 4*8 + 54 ) > nBmpFileLen )
	{
		CString	strMaxTxtLen;
		strMaxTxtLen.Format("%d", (nBmpFileLen-54-4*8)/8);
		m_strFilesInfo	+=	( "\n\n该宿主BMP只能隐藏长度小于" + strMaxTxtLen +
								"字节的文件,无法隐藏当前寄生文件!" );
		UpdateData(false);
		fBmpRead.Close();
		fTxtRead.Close();
		fBmpWrite.Close();
		CFile::Remove(strBmpWrite);
		return;
	}

	char	cBufBmp[BUFFER_SIZE_FIVE_KB];			//BMP读取缓冲区
	char*	pBufBmp	=	cBufBmp;
	char	cBufTxt[BUFFER_SIZE_FIVE_KB >> 3];		//TXT读取缓冲区,1bit插入1Byte
	char*	pBufTxt =	cBufTxt;
	int		nBytesBmpCurRead = 0, nBytesTxtCurRead = 0, nBmpSeekPos = 0, nTxtSeekPos = 0;

	//先复制BMP头部
	nBytesBmpCurRead	=	fBmpRead.Read(pBufBmp, 54);
	fBmpWrite.Write(pBufBmp, nBytesBmpCurRead);
	nBmpSeekPos			+=	nBytesBmpCurRead;

	//写入BMP前32字节,作为TXT文件的大小
	nBytesBmpCurRead	=	fBmpRead.Read(pBufBmp, 32);
	//将TXT文件大小存在char数组中,nTxtFileLen的高字节存在数组cTxtLen低字节
	char	cTxtLen[4]	=	{0, 0, 0, 0};
	for(int nByteIndex=0; nByteIndex<4; nByteIndex++)
		cTxtLen[nByteIndex]	=	(char)( nTxtFileLen >> (3-nByteIndex)*8 );
	for(nByteIndex=0; nByteIndex<4; nByteIndex++)
		for(int nBitOffInByte=0; nBitOffInByte<8; nBitOffInByte++)
		{
			*(pBufBmp + 8*nByteIndex + nBitOffInByte)	=
					TxtBitMarkInBmpByte(	cTxtLen[nByteIndex],
											nBitOffInByte,
											*(pBufBmp + 8*nByteIndex + nBitOffInByte)	);
		}
	fBmpWrite.Seek(nBmpSeekPos, CFile::begin);
	fBmpWrite.Write(pBufBmp, nBytesBmpCurRead);
	nBmpSeekPos	+=	32;

	//复制、修改、保存BMP数据部分
	do
	{
		int	nBmpCurByteOff	=	0;		//当前BMP字节偏移量
		//读取BMP、TXT数据到缓冲区
		nBytesBmpCurRead	=	fBmpRead.Read(pBufBmp, BUFFER_SIZE_FIVE_KB);
		nBytesTxtCurRead	=	fTxtRead.Read(pBufTxt, BUFFER_SIZE_FIVE_KB >> 3);

		for(int nTxtByteOff=0; nTxtByteOff<nBytesTxtCurRead; nTxtByteOff++)
		{		//TXT字节偏移量
			for(int nTxtBitOffInByte=0; nTxtBitOffInByte<8; nTxtBitOffInByte++)
			{		//TXT每个字节的比特偏移量
				nBmpCurByteOff	=	nTxtByteOff * 8 + nTxtBitOffInByte;
				*(pBufBmp+nBmpCurByteOff)	=
						TxtBitMarkInBmpByte(	*(pBufTxt+nTxtByteOff),
												nTxtBitOffInByte,
												*(pBufBmp+nBmpCurByteOff) );
			}
			nTxtSeekPos	+=	nBytesTxtCurRead;
		}

		//缓冲区数据写入BMP文件
		fBmpWrite.Seek(nBmpSeekPos, CFile::begin);
		fBmpWrite.Write(pBufBmp, nBytesBmpCurRead);
		nBmpSeekPos	+=	nBytesBmpCurRead;
	}while( nBmpSeekPos + nBytesBmpCurRead != nBmpFileLen );

	//信息隐藏完毕,控件显示
	m_strFilesInfo	+=	"\n\n寄生文件已经被隐藏!";
	UpdateData(false);

	fBmpRead.Close();
	fTxtRead.Close();
	fBmpWrite.Close();
}

char CInfoHideDlg::TxtBitMarkInBmpByte(char cTxtByte, int nTxtBitOffInByte, char cBmpByte)
{
	char	cRetNewBmpByte;
	if( ( cTxtByte >> (7-nTxtBitOffInByte) ) & 1 )
	{
		//当前TXT比特为1,当前BMP字节的1比特数量改为“奇数个”
		int	n1BitCount	=	0;
		for(int nBmpBitOffInByte=0; nBmpBitOffInByte<8; nBmpBitOffInByte++)
		{		//BMP每个字节的比特偏移量
			if( cBmpByte & ( 1 << nBmpBitOffInByte ) )
				n1BitCount++;		//统计当前BMP字节的1比特个数
		}
		if( !(n1BitCount % 2) )		//n1BitCount不是奇数
		{
			if( !((cBmpByte) & 1) )		//BMP当前字节是以0比特结尾
				cRetNewBmpByte	=	cBmpByte | 0x01;
			else
				cRetNewBmpByte	=	cBmpByte & 0xFE;
		}
		else
			cRetNewBmpByte	=	cBmpByte;
	}
	else
	{
		//当前TXT比特为0,当前BMP字节的1比特数量改为偶数个
		int	n1BitCount	=	0;
		for(int nBmpBitOffInByte=0; nBmpBitOffInByte<8; nBmpBitOffInByte++)
		{		//BMP每个字节的比特偏移量
			if( cBmpByte & ( 1 << nBmpBitOffInByte ) )
				n1BitCount++;
		}
		if( n1BitCount % 2 )		//n1BitCount不是偶数
		{
			if( !((cBmpByte) & 1) )		//BMP当前字节是以0比特结尾
				cRetNewBmpByte	=	(cBmpByte) | 0x01;
			else
				cRetNewBmpByte	=	(cBmpByte) & 0xFE;
		}
		else
			cRetNewBmpByte	=	cBmpByte;
	}
	return cRetNewBmpByte;
}

void CInfoHideDlg::OnButtonResumeInfo() 
{
	// TODO: Add your control notification handler code here
	//获取文件路径,以便打开
	CString	strBmpRead = "", strTxtWrite = "";
	CFileDialog	fdFileOpen(true);
	fdFileOpen.m_ofn.lpstrTitle		=	"打开水印后的BMP文件";
	fdFileOpen.m_ofn.lpstrFilter	=	"位图文件(*.bmp)\0*.bmp\0\0";
	fdFileOpen.m_ofn.Flags			|=	OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
	if( fdFileOpen.DoModal()==IDOK )
		strBmpRead	=	fdFileOpen.GetPathName();

	CFileDialog	fdFileSave(false);
	fdFileSave.m_ofn.lpstrTitle		=	"保存寄生文件";
	fdFileSave.m_ofn.lpstrFilter	=	"位图文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
	fdFileSave.m_ofn.lpstrDefExt	=	"bmp";
	fdFileSave.m_ofn.Flags			|=	OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
	if( fdFileSave.DoModal()==IDOK )
		strTxtWrite	=	fdFileSave.GetPathName();

	//控件显示文件信息
	m_strFilesInfo.Format(	"水印后的BMP文件:%s\n\n寄生文件:%s",
								strBmpRead, strTxtWrite	);
	if( strBmpRead.IsEmpty() || strTxtWrite.IsEmpty() )
	{
		m_strFilesInfo	+=	"\n\n文件路径没有指定,无法隐藏信息!";
		UpdateData(false);
		return;
	}
	UpdateData(false);

	CFile	fBmpRead, fTxtWrite;
	fBmpRead.Open(strBmpRead, CFile::modeRead);
	fTxtWrite.Open(strTxtWrite, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
	char	cBufBmp[BUFFER_SIZE_FIVE_KB];			//BMP读取缓冲区
	char*	pBufBmp =	cBufBmp;
	char	cBufTxt[BUFFER_SIZE_FIVE_KB >> 3];		//TXT写入缓冲区
	char*	pBufTxt =	cBufTxt;
	int		nBytesBmpCurRead = 0, nBytesTxtCurRead = 0, nBmpSeekPos = 0, nTxtSeekPos = 0;
	int		nBmpFileLen	=	fBmpRead.GetLength();
	int		nTxtFileLen	=	0;		//TXT文件长度

	//BMP的54字节头部不做运算
	fBmpRead.Seek(54, CFile::begin);

	//先从BMP的数据部分前32字节获得TXT的文件长度
	nBytesBmpCurRead	=	fBmpRead.Read(pBufBmp, 32);
	char	cTxtLen[4]	=	{0, 0, 0, 0};
	for(int nByteIndex=0; nByteIndex<32; nByteIndex+=8)
	{
		cTxtLen[nByteIndex/8]	=	Bmp8ByteMakeTxt1Byte( pBufBmp+nByteIndex );
		nTxtFileLen	=	nTxtFileLen*256 + (UCHAR)cTxtLen[nByteIndex/8];
	}
	nBmpSeekPos	+=	(54 + 32);
	fBmpRead.Seek(nBmpSeekPos, CFile::begin);

	//BMP数据部分转化为TXT的比特流
	do
	{
		//从BMP读到缓冲区
		nBytesBmpCurRead	=	fBmpRead.Read(pBufBmp, BUFFER_SIZE_FIVE_KB);

		//向TXT缓冲区写入1Byte
		for(int nBmpByteOff=0; nBmpByteOff<nBytesBmpCurRead; nBmpByteOff+=8)
			*( pBufTxt + (nBmpByteOff >> 3) )	=
							Bmp8ByteMakeTxt1Byte( pBufBmp+nBmpByteOff );

		//写入TXT文件
		fTxtWrite.Seek(nTxtSeekPos, CFile::begin);
		fTxtWrite.Write(pBufTxt, nBytesBmpCurRead / 8);
		nBmpSeekPos	+=	nBytesBmpCurRead;
		nTxtSeekPos	+=	nBytesBmpCurRead / 8;
	}
	while( nTxtSeekPos<nTxtFileLen );

	//信息恢复完毕,控件显示
	m_strFilesInfo	+=	"\n\n寄生文件已经被恢复!";
	UpdateData(false);

	fTxtWrite.SetLength(nTxtFileLen);		//TXT文件长度,从BMP数据部分前32字节读取
	fBmpRead.Close();
	fTxtWrite.Close();
}

char CInfoHideDlg::Bmp8ByteMakeTxt1Byte(char *cBmp8Byte)
{
	char	cRetNewTxtByte	=	0;
	for(int nTxtBitOff=0; nTxtBitOff<8; nTxtBitOff++)
	{		//TXT当前字节的比特偏移量,即BMP在当前8字节组中的偏移量
		int	n1BitCount	=	0;
		for(int nBmpBitOffInByte=0; nBmpBitOffInByte<8; nBmpBitOffInByte++)
		{		//BMP每个字节的比特偏移量
			if( (*(cBmp8Byte+nTxtBitOff) >> nBmpBitOffInByte) & 1 )
				n1BitCount++;		//统计当前BMP字节的1比特个数
		}
		if( n1BitCount % 2 )
			//当前BMP字节的1比特数量为“奇数个”,当前TXT比特为1
			cRetNewTxtByte	=	cRetNewTxtByte | ( 1 << (7-nTxtBitOff) );
	}
	return cRetNewTxtByte;
}

⌨️ 快捷键说明

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