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

📄 hillcodordlg.cpp

📁 在VC环境下采用希尔密码体制加密、解密和破译。该软件实现了输入任意长度的密钥对文件进行加密和解密
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	fileE.Close();
	fileKey.Close();
}



void CHillCodorDlg::OnButtonSaveMFile() 
{
	// TODO: Add your control notification handler code here
	// 提示选择保存文件路径
	
	CFileDialog dlg(FALSE, "txt", m_strDisMFilePath, OFN_HIDEREADONLY, 
		"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
	
	// 提示用户选择保存的路径
	if (dlg.DoModal() != IDOK)
	{
		// 返回
		return;
	}
	
	// 获取用户指定的文件路径
	m_strDisMFilePath = dlg.GetPathName();
	
	// 更新
	UpdateData(FALSE);
}

void CHillCodorDlg::OnButtonOpenEFile() 
{
	// TODO: Add your control notification handler code here
	// 提示选择打开文件路径
	
	CFileDialog dlg(TRUE, "txt", m_strDisEFilePath, OFN_HIDEREADONLY, 
		"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
	
	// 提示用户选择保存的路径
	if (dlg.DoModal() != IDOK)
	{
		// 返回
		return;
	}
	
	// 获取用户指定的文件路径
	m_strDisEFilePath = dlg.GetPathName();
	
	// 更新
	UpdateData(FALSE);
}

void CHillCodorDlg::OnButtonOpenKeyFileDis() 
{
	// TODO: Add your control notification handler code here
	// 提示选择打开文件路径
	
	CFileDialog dlg(TRUE, "txt", m_strDisKeyFilePath, OFN_HIDEREADONLY, 
		"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
	
	// 提示用户选择保存的路径
	if (dlg.DoModal() != IDOK)
	{
		// 返回
		return;
	}
	
	// 获取用户指定的文件路径
	m_strDisKeyFilePath = dlg.GetPathName();
	
	// 更新
	UpdateData(FALSE);
}

void CHillCodorDlg::OnButtonDiscryption() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFile fileM; // 明文
	CFileException fe;

	if(!fileM.Open(m_strDisMFilePath.GetBuffer(0), CFile::modeWrite|CFile::modeCreate, &fe))
	{
		CString strError = "";
		strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
			m_strDisMFilePath.GetBuffer(0), fe.m_cause);
		AfxMessageBox(strError);
		return;
	}

	CFile fileE;  // 密文
	if(!fileE.Open(m_strDisEFilePath.GetBuffer(0), CFile::modeRead, &fe))
	{
		CString strError = "";
		strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
			m_strDisEFilePath.GetBuffer(0), fe.m_cause);
		AfxMessageBox(strError);
		return;
	}

	CFile fileKey; // 解密密钥文件
	if(!fileKey.Open(m_strDisKeyFilePath.GetBuffer(0), CFile::modeRead, &fe))
	{
		CString strError = "";
		strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
			m_strDisKeyFilePath.GetBuffer(0), fe.m_cause);
		AfxMessageBox(strError);
		return;
	}
	
	long *pDiscryKey = NULL;  // 存放解密密钥
	
	// 获取密钥长度(一个密钥元素占4个字节)
	int nKeyLen = (fileKey.GetLength() - sizeof(long)) / sizeof(long);

	// 获取密钥
	pDiscryKey = new long[nKeyLen];
	if(NULL == pDiscryKey)
	{
		exit(1);
	}
	fileKey.Read(pDiscryKey, nKeyLen*sizeof(long));

	// 获取明文文件长度
	int nMFileLen = 0;
	fileKey.Read(&nMFileLen, sizeof(long));

	// 获取解密块大小
	int nBlockSize = (long)sqrt((double)nKeyLen);
	if(nBlockSize*nBlockSize != nKeyLen)
	{
		if(NULL != pDiscryKey)
		{
			delete pDiscryKey;
			pDiscryKey = NULL;
		}
		fileM.Close();
		fileE.Close();
		fileKey.Close();

		return;
	}

	// 获取解密块数
	int nBlockNum = fileE.GetLength() / nBlockSize;

	// 分配明文块和密文块空间
	char * pM = NULL; // 存放明文快
	char * pE = NULL; // 存放密文块
	int i = 0;
	pM = new char[nBlockSize];
	if(NULL == pM)
	{
		exit(1);
	}
	pE = new char[nBlockSize];
	if(NULL == pE)
	{
		exit(1);
	}

	//设置解密密钥
	CHillCode codeDiscryptor;
	CMatrix matrixDiscryptKey(pDiscryKey, nBlockSize, nBlockSize);
	codeDiscryptor.SetDiscryKey(matrixDiscryptKey);

	BeginWaitCursor();

	// 循环解密
	for(i=0; i<nBlockNum; i++)
	{
		fileE.Read(pE, nBlockSize);
		codeDiscryptor.SetE((const unsigned char *)pE, nBlockSize);
		memcpy(pM, codeDiscryptor.Discrypting(), nBlockSize);
		fileM.Write(pM, nBlockSize);
	}

	// 处理最后一块
	NULL;

	EndWaitCursor();

	// 结束处理
	if(NULL != pE)
	{
		delete pE;
		pE = NULL;
	}
	if(NULL != pM)
	{
		delete pM;
		pM = NULL;
	}
	if(NULL != pDiscryKey)
	{
		delete pDiscryKey;
		pDiscryKey = NULL;
	}
	fileM.Close();
	fileE.Close();
	fileKey.Close();
}

void CHillCodorDlg::OnButtonGetKey() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFile fileM; // 明文
	CFileException fe;

	if(!fileM.Open(m_strUnMFilePath.GetBuffer(0), CFile::modeRead, &fe))
	{
		CString strError = "";
		strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
			m_strUnMFilePath.GetBuffer(0), fe.m_cause);
		AfxMessageBox(strError);
		return;
	}

	CFile fileE;  // 密文
	if(!fileE.Open(m_strUnEFilePath.GetBuffer(0), CFile::modeRead, &fe))
	{
		CString strError = "";
		strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
			m_strUnEFilePath.GetBuffer(0), fe.m_cause);
		AfxMessageBox(strError);
		return;
	}

	// 获取处理文件的大小
	int nFileLen = (fileM.GetLength() > fileE.GetLength())? fileE.GetLength(): fileM.GetLength();

	// 循环破译
	long lBlockSize = 1; // 处理块的大小
	long lLoop = nFileLen; // 在明文中找可逆的明文矩阵试探次数
	long lKeyLen = 1; // 密钥的长度
	const long BLOCK_SIZE_LIMIT = (long)sqrt(nFileLen);

	BeginWaitCursor();

	for(lBlockSize=1; lBlockSize<=BLOCK_SIZE_LIMIT; lBlockSize++)
	{
		// 重置文件指针
		fileM.SeekToBegin();
		fileE.SeekToBegin();

		// 获取猜测密文长度
		lKeyLen = lBlockSize * lBlockSize;
		
		// 初始化存储明文逆矩阵和密文矩阵
		CMatrix matrixM;  // 存储明文逆矩阵
		CMatrix matrixE;  // 存储密文矩阵
		matrixM.InitMatrix(lBlockSize, lBlockSize);
		matrixE.InitMatrix(lBlockSize, lBlockSize);
		char * szM = new char[lKeyLen];
		if(!szM)
		{
			exit(1);
		}
		char * szE = new char[lKeyLen];
		if(!szE)
		{
			exit(1);
		}
		int i = 0;
		lLoop = nFileLen / (lKeyLen);

		// 声明密钥矩阵
		CMatrix matrixKeyNew; // 存储最新获得的密钥
		CMatrix matrixKeyOld; // 存储上次循环获得的密钥

		for(i=0; i<lLoop; i++)
		{
			fileM.Read(szM, sizeof(char)*lBlockSize*lBlockSize);
			fileE.Read(szE, sizeof(char)*lBlockSize*lBlockSize);// 读取密文,并移动文件指针

			// 初始化明文逆矩阵
			int nRow = 0;
			int nCol = 0;
			for(nRow=0; nRow<lBlockSize; nRow++)
			{
				for(nCol=0; nCol<lBlockSize; nCol++)
				{
					matrixM.SetElement(nRow, nCol, (long)szM[nRow*lBlockSize+nCol]);
				}
			}
			matrixM.Transpose();
			long lPhalanxValue = 0; 
			lPhalanxValue = matrixM.GetPhalanxValue();
			if(!lPhalanxValue || !(lPhalanxValue%2))
			{
				continue;
			}
			matrixM.Adjoint();
			matrixM = matrixM * Reciprocal(lPhalanxValue);
			matrixM.Mod(MAPLEN);
			
			// 初始化密文矩阵
			for(nRow=0; nRow<lBlockSize; nRow++)
			{
				for(nCol=0; nCol<lBlockSize; nCol++)
				{
					matrixE.SetElement(nRow, nCol, (long)szE[nRow*lBlockSize+nCol]);
				}
			}
			matrixE.Transpose();

			// 获取新密钥
			matrixKeyNew = matrixE * matrixM;
			matrixKeyNew.Mod(MAPLEN);

			// 判断是否和上次求得的密钥是否相同
			if(matrixKeyOld.IsEmpty())
			{
				matrixKeyOld = matrixKeyNew;
			}
			else if(!(matrixKeyOld == matrixKeyNew))
			{// 该密钥长度是错误的
				break;
			}
		}
		if(szM)
		{
			delete szM;
			szM = NULL;
		}
		if(szE)
		{
			delete szE;
			szE = NULL;
		}

		// 显示密钥
		if(!matrixKeyOld.IsEmpty() && !matrixKeyNew.IsEmpty() && matrixKeyOld == matrixKeyNew)
		{
			long lValue = 0;
			int j = 0;
			char * szKey = new char[lKeyLen+1];
			if(!szKey)
			{
				exit(1);
			}
			memset(szKey, 0, lKeyLen+1);
			for(i=0; i<lBlockSize; i++)
			{
				for(j=0; j<lBlockSize; j++)
				{
					matrixKeyNew.GetElement(i, j, lValue);
					szKey[i*lBlockSize+j] = (char)lValue;
				}
			}
			m_strUnKey = szKey;
			UpdateData(FALSE);
			if(szKey)
			{
				delete szKey;
				szKey = NULL;
			}
			break;
		}		
	}

	EndWaitCursor();

	// 结束处理
	fileM.Close();
	fileE.Close();
}

void CHillCodorDlg::OnButtonOpenMUn() 
{
	// TODO: Add your control notification handler code here
	// 提示选择打开文件路径
	
	CFileDialog dlg(TRUE, "txt", m_strUnMFilePath, OFN_HIDEREADONLY, 
		"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
	
	// 提示用户选择保存的路径
	if (dlg.DoModal() != IDOK)
	{
		// 返回
		return;
	}
	
	// 获取用户指定的文件路径
	m_strUnMFilePath = dlg.GetPathName();
	
	// 更新
	UpdateData(FALSE);
}

void CHillCodorDlg::OnButtonOpenEUn() 
{
	// TODO: Add your control notification handler code here
	// 提示选择打开文件路径
	
	CFileDialog dlg(TRUE, "txt", m_strUnEFilePath, OFN_HIDEREADONLY, 
		"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
	
	// 提示用户选择保存的路径
	if (dlg.DoModal() != IDOK)
	{
		// 返回
		return;
	}
	
	// 获取用户指定的文件路径
	m_strUnEFilePath = dlg.GetPathName();
	
	// 更新
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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