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

📄 pailiema1dlg.cpp

📁 一种新的加解密方法(排列码加解密)
💻 CPP
字号:
// pailiema1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "pailiema1.h"
#include "pailiema1Dlg.h"
#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CPailiema1Dlg dialog

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

void CPailiema1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPailiema1Dlg)
	DDX_Text(pDX, IDC_PASS, m_pass);
	DDX_Text(pDX, IDC_PATH, m_path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPailiema1Dlg, CDialog)
	//{{AFX_MSG_MAP(CPailiema1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_D, OnD)
	ON_BN_CLICKED(IDC_E, OnE)
	ON_BN_CLICKED(IDC_BROW, OnBrow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPailiema1Dlg message handlers

BOOL CPailiema1Dlg::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
	
	CDialog::OnInitDialog();
	m_path=' ';
	m_pass=' ';
    UpdateData(FALSE);
    int s0[24][4]={{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
	int s1[24][4]={{0,1,3,2},{0,1,2,3},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
	int s2[24][4]={{0,2,1,3},{0,1,2,3},{0,1,3,2},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};

	int s3[24][4]={{0,2,3,1},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
       
    int s4[24][4]={{0,3,1,2},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s5[24][4]={{0,3,2,1},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s6[24][4]={{1,0,2,3},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s7[24][4]={{1,0,3,2},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s8[24][4]={{1,2,0,3},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,3,0},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s9[24][4]={{1,2,3,0},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,3,0,2},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    int s10[24][4]={{1,3,0,2},{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,2,0},
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},
	{3,0,1,2},{3,0,1,2},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
    //排列码表
	int i,k,j;
    for( j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                  ArrayTable[k+(j*24)][i]=s0[(k+j)%24][i];
		}
	}
	for( j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[576+k+(j*24)][i]=s1[(k+j)%24][i];
		}
	}
    for( j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[1152+k+(j*24)][i]=s2[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[1728+k+(j*24)][i]=s3[(k+j)%24][i];
		}
	}
    for( j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[2304+k+(j*24)][i]=s4[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[2880+k+(j*24)][i]=s5[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[3456+k+(j*24)][i]=s6[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[4032+k+(j*24)][i]=s7[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[4608+k+(j*24)][i]=s8[(k+j)%24][i];
		}
	}
    for(j=0;j<24;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[5184+k+(j*24)][i]=s9[(k+j)%24][i];
		}
	}
    for(j=0;j<16;j++){
		for(k=0;k<24;k++){
			for(i=0;i<4;i++)
                ArrayTable[5760+k+(j*24)][i]=s10[(k+j)%24][i];
		}
	}


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

int CPailiema1Dlg::DoModal() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::DoModal();
}
//给文件加密的函数
bool CPailiema1Dlg::ecfile(LPCTSTR fpath)
{
	char *data;
	CFile *file;
	
	DWORD flen;
    UpdateData(TRUE);

    char* password = m_pass.GetBuffer(0);

	file = new CFile;
	if ( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}
   file = new CFile;
	if ( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

	flen = file->GetLength();
	data = new char[(int)flen];

	file->SeekToBegin();
	file->Read(data, flen);
	int m[4]={0},z=0;
	for(int j=0;j<4;j++)
	    m[j]=int(*(password+j));//取钥匙
  //只加密低四位
    for(int i=0; i<(int)flen; i++)
	{
		
	
    char p= data[i]&15;//disiwei
    char q= data[i]&240;//gaosiwei
    int h[4]={0},y[16]={0};
	int r[4]={0};
	for(j=0;j<4;j++)
	    r[j]=int((p>>j)&1);//最高位在r[7]
	//取排列码

    int h1=m[0]&15+((m[0]>>4)&15)*16;
    int h2=(m[1]&15+((m[1]>>4)&15)*16)%24;//ArrayTable[h1*24+h2]即为所求排列码。
	for(j=2;j<4;j++)
	    for(int z=0;z<8;z++)
	        y[(j-2)*8+z]=(m[j]>>z)&1;// 异或控制位
	
	for(j=0;j<4;j++){
	     z=ArrayTable[h1*24+h2][j];
		h[z]=r[j];
		int k=(j*4+ArrayTable[h1*24+h2][j]);
		if (y[k]==1) { h[z]=(h[z]+1)%2;}
	} //加密完成 数组h里  
    int t=0;
	for(j=0;j<4;j++)    
        t+=(int)(h[j]*pow(2,j));
    p=char(t);     
    data[i]=p|q;//写回
   //变密钥
	int x[4]={0};
	m[4]=(m[4]+1)%256;
	for(j=0;j<3;j++)
		m[j]+=int(q);
	for(j=0;j<4;j++)
	    x[j]=m[(j+1)%4];
	for(j=0;j<4;j++)
	    m[j]=x[j];
	}

	file->SeekToBegin();
	file->Write(data, flen);
	

    delete[] data;
  
	file->Close();
	delete file;

	return TRUE;
}


//给文件解密的函数
bool CPailiema1Dlg::dcfile(LPCTSTR fpath)
{
	char * data;
	CFile *file;
	
	DWORD flen;
	/*char man[4];*/

	file = new CFile;
	if( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

	flen = file->GetLength();
	data = new char[(int)flen];

	//检验密码是不是正确
	/*file->Seek(-4, CFile::end);
	file->Read(&man, 4);*/
    UpdateData(TRUE);

    char* password = m_pass.GetBuffer(0);
	int m[4]={0};
	for(int j=0;j<4;j++)
	    m[j]=int(*(password+j));//取钥匙





	//解密
	file->SeekToBegin();
	file->Read(data, flen);

	for(int i=0; i<(int)flen; i++)
	{

        char p= data[i]&15;//disiwei
        char q= data[i]&240;//gaosiwei
        int h[4]={0},y[16]={0};
  	    int r[4]={0};
	    for(j=0;j<4;j++)
	        r[j]=int((p>>j)&1);//最高位在r[7]
	    //取排列码

        int h1=m[0]&15+((m[0]>>4)&15)*16;
        int h2=(m[1]&15+((m[1]>>4)&15)*16)%24;//ArrayTable[h1*24+h2]即为所求排列码。
	    for(j=2;j<4;j++)
	         for(int z=0;z<8;z++)
	              y[(j-2)*8+z]=(m[j]>>z)&1;// 异或控制位
	
	    for(j=0;j<4;j++){
	        int z=ArrayTable[h1*24+h2][j];
	    	h[j]=r[z];
		    int k=(j*4+ArrayTable[h1*24+h2][j]);
		    if (y[k]==1) { h[j]=(h[j]+1)%2;}
		} //加密完成 数组h里  
        int t=0;
	    for(j=0;j<4;j++)    
            t+=(int)(h[j]*pow(2,j));
        p=char(t);     
        data[i]=p|q;//写回
        //变密钥
	    int x[4]={0};
	    m[4]=(m[4]+1)%256;
        for(j=0;j<3;j++)
		    m[j]+=int(q);
	    for(j=0;j<4;j++)
	        x[j]=m[(j+1)%4];
	    for(j=0;j<4;j++)
	        m[j]=x[j];

		
	}
	file->SeekToBegin();
	file->Write(data, flen);
	file->SetLength(flen-4);
	file->Close();

	delete[] data;
	delete file;
	return TRUE;
}


void CPailiema1Dlg::OnD() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_path == "")
	{
		AfxMessageBox("没有选择要解密的文件?");
		return;
	}

	UpdateData(TRUE);
	if(m_pass == "")
	{
		AfxMessageBox("没有写上密码");
		return;
	}

	if(CPailiema1Dlg::dcfile(m_path))
	{
		MessageBox("解密成功了");
	}
	else
	{
		MessageBox("解密失败了");
	}
	
}

void CPailiema1Dlg::OnE() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_path == "")
	{
		AfxMessageBox("没有选择要加密的文件?");
		return;
	}

	UpdateData(TRUE);
	if(m_pass == "")
	{
		AfxMessageBox("没有写上密码");
		return;
	}


	if(CPailiema1Dlg::ecfile(m_path))
	{
		MessageBox("加密成功了已经");
	}
	else
	{
		MessageBox("没加密成功");
	}	
	
}

void CPailiema1Dlg::OnBrow() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);
	if(dlg.DoModal() == IDOK)
	{
		m_path = dlg.GetPathName();
		UpdateData(FALSE);
	}
	else
	{
		return;
	}


	
}

⌨️ 快捷键说明

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