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

📄 lsb加密dlg.cpp

📁 LSB
💻 CPP
字号:
// LSB加密Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "LSB加密.h"
#include "LSB加密Dlg.h"

//#include <iostream.h>
//#include <fstream.h>
#include <stdio.h>
#include <io.h>
#include <sys/stat.h>

#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()

/////////////////////////////////////////////////////////////////////////////
// CLSBDlg dialog

CLSBDlg::CLSBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLSBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLSBDlg)
	m_strBmpFile = _T("");
	m_strFile = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLSBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLSBDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strBmpFile);
	DDX_Text(pDX, IDC_EDIT2, m_strFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLSBDlg, CDialog)
	//{{AFX_MSG_MAP(CLSBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSEBMP, OnBrowsebmp)
	ON_BN_CLICKED(IDC_FILE, OnFile)
	ON_BN_CLICKED(IDDECRET, OnDecret)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLSBDlg message handlers

BOOL CLSBDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CLSBDlg::OnBrowsebmp() 
{
	// TODO: Add your control notification handler code here
	CString szFilter = "BMP位图文件 (*.bmp)|*.bmp||";
	CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
    if(fileDlg.DoModal()==IDOK)
	{
		m_strBmpFile = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CLSBDlg::OnFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fileDlg(TRUE);
    if(fileDlg.DoModal()==IDOK)
	{
		m_strFile = fileDlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CLSBDlg::OnOK() 
{
	// TODO: Add extra validation here
	if(m_strFile == ""|| m_strBmpFile == "")
	{
		AfxMessageBox("文件名不能为空");
		return;
	}

	Encrypt((LPSTR)(LPCSTR)m_strBmpFile, (LPSTR)(LPCSTR)m_strFile);
	AfxMessageBox("加密成功");
//	CDialog::OnOK();
}

void CLSBDlg::OnDecret() 
{
	// TODO: Add your control notification handler code here
	if( m_strBmpFile == "")
	{
		AfxMessageBox("位图文件名不能为空");
		return;
	}

	Decrypt((LPSTR)(LPCSTR)m_strBmpFile);
	AfxMessageBox(" 解密成功,内容存在当前目录的temp文件中");
}

void CLSBDlg::Encrypt(char *file1, char *file2)
{
//	using namespace std;
    unsigned char ch,ch1,chf1,chf2;
	int i;//循环控制变量
//    ifstream f1(file1,ios::in|ios::out|ios::binary);//打开图片文件
//   ifstream f2(file2,ios::binary);//打开被隐藏文件
//    f2.seekg(0,ios::end);//文件指针指向文件尾
	FILE* f1 = fopen(file1, "r+b");
	FILE* f2 = fopen(file2, "rb");
//    int filelen=f2.tellg();//求被隐藏文件长度,tellg()返回指针位置
	int fd = fileno(f2);
	int filelen = filelength(fd);
	//分离文件长度四个字节
    char strlen[4];//strlen[i]为文件长度第i个字节
    int len=filelen;
    for(i=0;i<4;i++)
    {
    	strlen[i]=(char)len&0x0ff;    //求出第i个字节
    	len=len>>8;
    }

//获取BMP文件的偏移量
	int iOffset = sizeof(BITMAPFILEHEADER);
//	DWORD Pixel_begin = 0;
	char cSize[sizeof(BITMAPFILEHEADER)] = {0};
//	FILE* f1 = fopen(file1, "r+b");
	fread(cSize, sizeof(char), iOffset, f1);
	BITMAPFILEHEADER* pBitMap = (BITMAPFILEHEADER*)cSize;
	DWORD Pixel_begin = pBitMap->bfOffBits;


     //将文件长度按位写进BMP文件中
//    f1.seekg(Pixel_begin,ios::beg);//Pixe1_begin是像素位图陈列的偏移量,由BMP文件读出,其值也可以是某个像素常了节的偏移量
	fseek(f1, Pixel_begin, SEEK_SET);
    for(i = 3; i >= 0; i--)//将长度的四个字节按高低写入图片中
    { 
		ch1=strlen[i];
        ch=ch1&'\x80';//从高互低分离字节的每位,先保留最高位
		ch=ch>>7;//将所求的位移到最低位,以做一日和尚撞一天钟插入到像素字节的最低位
		ch=ch&0x01;//保留所求的位
		for(int j=0;j<8;j++)
		{
//			f1.get(chf1);//得到图片像素的一个分量的字节
			chf1 = fgetc(f1);
//			f1.seekg(-1,ios::cur);//指针回调
			fseek(f1, -1, SEEK_CUR);
			chf1=chf1&'\xfe';//屏蔽最低位
			chf1=chf1|ch;//将所求的位插入到列素字节的最低位
//			f1.put(chf1);//将修改后的像素字节写入原位置
			fputc(chf1, f1);//(FILE*)    将修改后的像素字节写入原位置
			ch1=ch1<<1;//将下一位移到字节的最高位
			ch=ch1&'\x80';
			ch=ch>>7;
			ch=ch&0x01;
		}
	}
	
	//将隐藏文件信息按位写进BMP文件中
//   f2.seekg(0,ios::beg);//文件指针指向文件的第一个字节
	fseek(f2, 0, SEEK_SET);
   for( i=0;i<filelen;i++)
   {
//	  f2.get(chf2);//得到要隐藏的一个字节
	  chf2 = fgetc(f2);
	  ch1=chf2;
	  ch=ch1&'\x80';
	  //ch1=ch>>7;    Error!!!!!!!!
	  ch=ch>>7;
	  ch=ch&0x01;

	  for(int j=0;j<8;j++)
	  { 
//		   f1.get(chf1);
//		   f1.seekg(-1,ios::cur);
		   chf1 = fgetc(f1);
		   fseek(f1, -1, SEEK_CUR);
//		   chf1 = fgetc
		   chf1=chf1&'\xfe';
		   chf1=chf1|ch;
//		   f1.put(chf1);
		   fputc(chf1, f1);
		   ch1=ch1<<1;
		   ch=ch1&'\x80';
		   ch=ch>>7;
		   ch=ch&0x01;
	  }
	}

	fclose(f1);
	fclose(f2);
}

void CLSBDlg::Decrypt(char *file1)
{
	int filelen=0,i,j;
//	char* file2 = NULL;
	unsigned char strlen[4],ch,chf1,chf2;
	int iOffset = sizeof(BITMAPFILEHEADER);
//	DWORD Pixel_begin = 0;
	char cSize[sizeof(BITMAPFILEHEADER)];
//	ifstream f1(file1,ios::binary);//打开待解密的图片文件
//	ofstream f2(file2,ios::binary);//创建被隐藏的文件,在图片文件中求出被隐藏的文件长度
//	f1.seekg(Pixel_begin,ios::beg);
	FILE* f1 = fopen(file1, "r+b");
	fread(cSize, sizeof(char), iOffset, f1);
	BITMAPFILEHEADER* pBitMap = (BITMAPFILEHEADER*)cSize;
	DWORD Pixel_begin = pBitMap->bfOffBits;

	fseek(f1, Pixel_begin, SEEK_SET);
	int fd = creat("temp.txt", _S_IWRITE);
	FILE* f2 = fdopen(fd, "w");

	for(i=3;i>=0;i--)
	{
		strlen[i] = '\0';
		for(j = 0; j <8; j++)
		{
//			f1.get(chf1);
			chf1 = fgetc(f1);
			ch=chf1&0x01;//求被隐藏的一位
			strlen[i]=strlen[i]<<1;
			strlen[i]=strlen[i]|ch;
		}

		 filelen=filelen<<8;
		 filelen=filelen|strlen[i];//从高到低求出长度的每一个字节
	}
	//在图片文件中,求出被隐藏的文件信息
	
	for(i=0; i<filelen; i++)
	{ 
		chf2='\0';
		for(int j=0;j<8;j++)
		{
//			f1.get(chf1);
			chf1 = fgetc(f1);
			ch=chf1&0x01;
			chf2=chf2<<1;
			chf2=chf2|ch;
		}
//		f2.put(chf2);//将所求字节写入文件中
		fputc(chf2, f2);
	}
	  
//	f1.close();f2.close();
	fclose(f1);
	fclose(f2);	
}


⌨️ 快捷键说明

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