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

📄 hillcode.cpp

📁 在VC环境下采用希尔密码体制加密、解密和破译。该软件实现了输入任意长度的密钥对文件进行加密和解密
💻 CPP
字号:
#include "stdafx.h"
#include "HillCode.h"

CHillCode::CHillCode() 
: m_szM(NULL), m_szE(NULL), m_matrixEncryKey(), m_matrixDiscryKey(), m_nLenM(0), m_nLenE(0)
{
}

CHillCode::~CHillCode()
{
	if (NULL != m_szM) 
	{
		delete m_szM;
		m_szM = NULL;
	}
	if(NULL != m_szE)
	{
		delete m_szE;
		m_szE = NULL;
	}
}

void CHillCode::SetM(const unsigned char *pM)
{
	if (NULL != m_szM)
	{
		delete m_szM;
		m_szM = NULL;
	}

	m_szM = new unsigned char[m_nLenM];
	if (NULL == m_szM)
	{
		exit(1);
	}
	memcpy((char *)m_szM, (const char *)pM, m_nLenM);
}

int CHillCode::SetEncryKey(const long * plMatrix, const int LEN)
{
	int nLen = LEN;
	m_nLenM = (int)sqrt((double)nLen);
	m_nLenE = m_nLenM;
	if(0 >= m_nLenM)
	{
		return 0;
	}

	m_matrixEncryKey.InitMatrix(m_nLenM, m_nLenM);
	int i = 0;
	int j = 0;
	for(i=0; i<m_nLenM; i++)
	{
		for(j=0; j<m_nLenM; j++)
		{
			m_matrixEncryKey.SetElement(i, j, (long)plMatrix[i*m_nLenM+j]);
		}
	}
	m_matrixDiscryKey = m_matrixEncryKey;
	long lPhalanxValue = 0; 
	lPhalanxValue = m_matrixDiscryKey.GetPhalanxValue();
	if(!lPhalanxValue || !(lPhalanxValue%2))
	{
		return 0;
	}
	m_matrixDiscryKey.Adjoint();
	m_matrixDiscryKey = m_matrixDiscryKey * Reciprocal(lPhalanxValue);
	m_matrixDiscryKey.Mod(MAPLEN);
	
	return 1;
}

const char * CHillCode::Encrypting()
{
	if(0 == m_szM)
	{
		return NULL;
	}

	CMatrix matrixM;
	matrixM.InitMatrix(m_nLenM, 1);
	int i = 0;
	for(i=0; i<m_nLenM; i++)
	{
		matrixM.SetElement(i, 0, (long)m_szM[i]);
	}

	CMatrix matrixE;
	long lValue = 0;

	if(0 != m_szE)
	{
		delete m_szE;
		m_szE = NULL;
	}
	matrixE.InitMatrix(m_nLenE, 1);
	matrixE = m_matrixEncryKey * matrixM;
	matrixE.Mod(MAPLEN);
	m_szE = new unsigned char[m_nLenE];
	if(0 == m_szE)
	{
		exit(1);
	}
	for(i=0; i<m_nLenM; i++)
	{
		matrixE.GetElement(i, 0, lValue);
		m_szE[i] = (unsigned char)lValue;
	}

	return (const char *)m_szE;
}

void CHillCode::SetDiscryKey(const CMatrix &matrixDiscryKey)
{
	if(matrixDiscryKey.GetRows() != matrixDiscryKey.GetCols())
	{
		return;
	}
	m_matrixDiscryKey = matrixDiscryKey;
	m_nLenM = matrixDiscryKey.GetCols();
	m_nLenE = matrixDiscryKey.GetCols();
}

void CHillCode::SetE(const unsigned char *pE)
{
	if (NULL != m_szE)
	{
		delete m_szE;
		m_szE = NULL;
	}

	m_szE = new unsigned char[m_nLenE];
	if (NULL == m_szE)
	{
		exit(1);
	}
	memcpy((char *)m_szE, (const char *)pE, m_nLenE);
}

const char * CHillCode::Discrypting()
{
	if(0 == m_szE)
	{
		return NULL;
	}

	CMatrix matrixE;
	matrixE.InitMatrix(m_nLenE, 1);
	int i = 0;
	for(i=0; i<m_nLenE; i++)
	{
		matrixE.SetElement(i, 0, (long)m_szE[i]);
	}

	CMatrix matrixM;
	long lValue = 0;

	if(0 != m_szM)
	{
		delete m_szM;
		m_szM = NULL;
	}
	matrixM.InitMatrix(m_nLenM, 1);
	matrixM = m_matrixDiscryKey * matrixE;
	matrixM.Mod(MAPLEN);
	m_szM = new unsigned char[m_nLenM];
	if(0 == m_szM)
	{
		exit(1);
	}
	for(i=0; i<m_nLenM; i++)
	{
		matrixM.GetElement(i, 0, lValue);
		m_szM[i] = (unsigned char)lValue;
	}

	return (const char *)m_szM;
}

void CHillCode::SetM(const unsigned char *pM, const int LEN)
{
	if(LEN != m_nLenM)
	{
		return;
	}
	if (NULL != m_szM)
	{
		delete m_szM;
		m_szM = NULL;
	}

	m_szM = new unsigned char[LEN];
	if (NULL == m_szM)
	{
		exit(1);
	}
	memcpy((char *)m_szM, (const char *)pM, LEN);	
}

void CHillCode::SetE(const unsigned char *pE, const int LEN)
{
	if(m_nLenE != LEN)
	{
		return;
	}

	if (NULL != m_szE)
	{
		delete m_szE;
		m_szE = NULL;
	}

	m_szE = new unsigned char[m_nLenE];
	if (NULL == m_szE)
	{
		exit(1);
	}
	memcpy((char *)m_szE, (const char *)pE, m_nLenE);
}

const long * CHillCode::GetDiscryptedKey() const
{
	return m_matrixDiscryKey.GetMatrix();
}

⌨️ 快捷键说明

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