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

📄 idea_cipher.cpp

📁 这是我编的包括古典密码
💻 CPP
字号:
// IDEA_Cipher.cpp : implementation file
//

#include "stdafx.h"
#include "MY_CAP.h"
#include "math.h"
#include "IDEA_Cipher.h"


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



#define KeyKind 1
#define PlainKind 2
#define CipherKind 3

/////////////////////////////////////////////////////////////////////////////
// CIDEA_Cipher dialog

CMY_CAPDlg *Main_CapI;
CIDEA_Cipher::CIDEA_Cipher(CWnd* pParent /*=NULL*/)
	: CDialog(CIDEA_Cipher::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIDEA_Cipher)
	m_strKeyWord = _T("");
	m_ValidBit = 0;
	m_strHint = _T("");
	//}}AFX_DATA_INIT
}


void CIDEA_Cipher::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIDEA_Cipher)
	DDX_Text(pDX, IDC_STRKEYWORD, m_strKeyWord);
	DDV_MaxChars(pDX, m_strKeyWord, 16);
	DDX_Text(pDX, IDC_HINT, m_strHint);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CIDEA_Cipher, CDialog)
	//{{AFX_MSG_MAP(CIDEA_Cipher)
	ON_COMMAND(IDD_ENCIPHER, OnEncipher)
	ON_COMMAND(IDD_DECIPHER, OnDecipher)
	ON_COMMAND(IDD_HELP, OnHelp)
	ON_COMMAND(IDD_QUIT, OnQuit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIDEA_Cipher message handlers

void CIDEA_Cipher::Trans_Dialog(CWnd *temp)
{
	Main_CapI = (CMY_CAPDlg *)temp;
}

void CIDEA_Cipher::OnEncipher () 
{
	// TODO: Add your command handler code here
	UpdateData (TRUE);
	if (Init_KeyBox (PlainKind) == FALSE)
	{
		m_strHint = "The key is wrong size!";
		UpdateData (FALSE);
		MessageBeep(MB_ICONEXCLAMATION);
		return;
	}
 	m_strHint = "";
	UpdateData (FALSE);
	Main_CapI -> UpdateData (TRUE);
	Main_CapI -> m_Ciphertext = "";
	int digit = 0;
	while (true)
	{
		m_Plaintext = ManagePlaintext (Main_CapI ->m_Plaintext,digit);
		if (m_Plaintext == "") break;
		ProduceIntArray (4, 16, PlainKind, m_Plaintext);
		Main_CapI -> m_Ciphertext
			+= ProduceResultText (m_Ciphertext, PlainKind);
		digit ++;
	}
	Main_CapI -> UpdateData (FALSE);
	if (Main_CapI -> m_Ciphertext != "") Main_CapI -> OnSave_Ciphertext ();
}

void CIDEA_Cipher::OnDecipher () 
{
	// TODO: Add your command handler code here
	UpdateData (TRUE);
	if (Init_KeyBox (CipherKind) == FALSE) 
	{
		m_strHint = "The key is wrong size!";
		UpdateData (FALSE);
		MessageBeep(MB_ICONEXCLAMATION);
		return;
	}
	m_strHint = "";
	UpdateData (FALSE);
	Main_CapI -> UpdateData (TRUE);
	Main_CapI -> m_Plaintext = "";
	sign = 0;
	while (true)
	{
		if (ManageCiphertext (Main_CapI -> m_Ciphertext) == FALSE)
			break;
		Main_CapI -> m_Plaintext 
			+= ProduceResultText (m_Ciphertext, CipherKind);
	}
	Main_CapI -> UpdateData (FALSE);
	if (Main_CapI -> m_Plaintext != "") Main_CapI -> OnSave_Plaintext ();
}

void CIDEA_Cipher::OnHelp() 
{
	// TODO: Add your command handler code here
	WinExec("notepad.exe Help.txt",SW_SHOW);
}

void CIDEA_Cipher::OnQuit() 
{
	// TODO: Add your command handler code here
	OnCancel ();
}

void CIDEA_Cipher::ProduceIntArray (int UpI, int DownJ, int kind, CString strText)
{   //字符数组转化为整形数组
	unsigned int temp;
	int k = 0;
	for (int i = 0; i < UpI; i++)
	{
		for (int j = 0; j < DownJ; j++)
		{
			if (j % 8 == 0)
			{
				temp = strText[k++];
			}
			if (kind == KeyKind)
				m_CurIntKey [i][j] = temp / (unsigned int) pow(2, 7 - (j % 8));
			else if (kind == PlainKind) m_intPlainArray [i][j] = 
				temp / (unsigned int) pow(2, 7 - (j % 8));
			temp = temp % (unsigned int) pow (2, 7 - (j % 8));
		}
	}
}

void CIDEA_Cipher::DistillChildKey (int CurrentPosition, int& ValidBit) //产生一组子密钥
{
	int i, Number = CurrentPosition;
	for (i = ValidBit; i < 8; i++)
	{
		EvaluateArray (m_ChildKey[Number++], m_CurIntKey[i], 16);
		if (Number == 6) break;
	}
	if (Number != 6)
	{
		CycLeft_Key (25);
		ValidBit = 0;
		DistillChildKey (Number, ValidBit);
	}
	else
	{
		ValidBit = i + 1;
	}
}

void CIDEA_Cipher::CycLeft_Key (int digit) //旋环左移digit 位
{
	int temp [8][16];
	int i, j;
	for ( i= 0; i < 8; i++)
	{
		for (j = 0; j < 16; j++)
		{
			temp [i][j] = m_CurIntKey [i][j];
		}
	}
	int m, n;
	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 16; j++)
		{
			n = (j + digit) % 16;
			m = (i + (j + digit) / 16) % 8; 
			m_CurIntKey [i][j] = temp [m][n];
		}
	}
}

void CIDEA_Cipher::EvaluateArray (int *ArrayL, int *ArrayR, int length) //数组等值
{   
	for (int i = 0; i < length; i++)
		ArrayL [i] = ArrayR [i];
}

void CIDEA_Cipher::ModOperation (int *intArrayL,int *intArrayR, int length, int round)
{  //模2运算
	for (int i = 0; i < length; i++)
	{
		Result [round][i] = intArrayL [i] ^ intArrayR [i];
	}
}

void CIDEA_Cipher::AddOperation (int *intArrayL, int *intArrayR, int length, int round)
{  //加运算(mod pow (2, 16))
	int carry = 0, i;
	for (i = length - 1; i >= 0; i--)
	{
		Result [round][i] = (intArrayL [i] + intArrayR [i] + carry) % 2;
		if (intArrayL [i] + intArrayR [i] + carry > 1) carry = 1;
		else carry =0;
	}
}

void CIDEA_Cipher::MulOperation (int *intArrayL, int *intArrayR, int length, int round)
{  //乘运算(mod (pow (2, 16) + 1))
	unsigned long tempL = 0, tempR = 0;
	int  i;
	for (i = 0; i < length; i++)
	{
		tempL += intArrayL [i] * (unsigned long) pow (2, 15 - i);
		tempR += intArrayR [i] * (unsigned long) pow (2, 15 - i);
	}
	if (tempL == 0) tempL = (unsigned long) pow (2, 16);
	if (tempR == 0) tempR = (unsigned long) pow (2, 16);
	tempL = (tempL * tempR) % (unsigned long) (pow (2, 16) + 1);
	for (i = 0; i < length; i++)
	{
		Result [round][i] = tempL / (unsigned long) pow (2, 15 - i);
		tempL = tempL % (unsigned long) pow (2, 15 - i);
	}
}

void CIDEA_Cipher::IterationRound (int Kind, int turn) //8轮迭代运算
{
	GetChildKeyBox (turn, Kind);
	MulOperation (m_intPlainArray[0], m_ChildKey[0], 16, 0);  //(1)
	AddOperation (m_intPlainArray[1], m_ChildKey[1], 16, 1);  //(2)
	AddOperation (m_intPlainArray[2], m_ChildKey[2], 16, 2);  //(3)
	MulOperation (m_intPlainArray[3], m_ChildKey[3], 16, 3);  //(4)
	ModOperation (Result[0],          Result[2],     16, 4);  //(5)
	ModOperation (Result[1],          Result[3],     16, 5);  //(6)
	MulOperation (Result[4],          m_ChildKey[4], 16, 6);  //(7)
	AddOperation (Result[5],          Result[6],     16, 7);  //(8)
	MulOperation (Result[7],          m_ChildKey[5], 16, 8);  //(9)
	AddOperation (Result[6],          Result[8],     16, 9);  //(10)
	ModOperation (Result[0],          Result[8],     16, 10); //(11)
	ModOperation (Result[2],          Result[8],     16, 11); //(12)
	ModOperation (Result[1],          Result[9],     16, 12); //(13)
	ModOperation (Result[3],          Result[9],     16, 13); //(14)
	EvaluateArray (m_intPlainArray[0], Result[10], 16);   //
	EvaluateArray (m_intPlainArray[1], Result[11], 16);   //
	EvaluateArray (m_intPlainArray[2], Result[12], 16);   //
	EvaluateArray (m_intPlainArray[3], Result[13], 16);   //
}

CString CIDEA_Cipher::ProduceResultText (CString& Resulttext, int Kind) 
{
	int i, j, temp = 0;
	Resulttext = "";
	for (i = 0; i < 8; i++)
		IterationRound (Kind, i);
	GetChildKeyBox (8, Kind);
	MulOperation (m_intPlainArray[0], m_ChildKey[0], 16, 0);
	AddOperation (m_intPlainArray[2], m_ChildKey[1], 16, 1);
	AddOperation (m_intPlainArray[1], m_ChildKey[2], 16, 2);
	MulOperation (m_intPlainArray[3], m_ChildKey[3], 16, 3);
	if (Kind == PlainKind)
		for (i = 0; i < 4; i++)
			for (j = 0; j < 16; j++)
				Resulttext += Result[i][j] + '0';
	else
	{
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 16; j++)
			{
				if (j == 8)
				{
					Resulttext += temp;
					temp = 0;
				}
				temp += Result[i][j] * (long) pow (2, 7 - (j % 8));
			}
			Resulttext += temp;
			temp = 0;
		}
	}
	return Resulttext;
}

CString CIDEA_Cipher::ManagePlaintext (CString Plaintext, int digit) //
{
	int length = Plaintext.GetLength ();
	int i;
	CString temp = "";
	for (i = digit * 8; i < length; i++)
	{
		temp += Plaintext [i];
		if (temp.GetLength () == 8) break;
	}
	if (temp.GetLength () == 0) return temp;
	else if (temp.GetLength () != 8)
	{
		while (true)
		{
			temp += ' ';
			if (temp.GetLength () >= 8) break;
		}
	}
	return temp;
}

void CIDEA_Cipher::AddAthwartOperation (int *intText, int length) //对加求逆
{
	unsigned long temp = 0;
	for (int i = 0; i < length; i++)
		temp += intText [i] * (long) pow (2, 15 - i);
	temp = (unsigned long) pow (2, 16) - temp;
	for (i = 0; i < length; i++)
	{
		intText[i] = temp / (unsigned long) pow (2, 15 - i);
		temp = temp % (unsigned long) pow (2, 15 - i);
	}
}

void CIDEA_Cipher::MulAthwartOperation (int *intText, int length) //对乘求逆
{
	unsigned long result = 0, temp;
	int n = 1,i;
	for (i = 0; i < length; i++)
	{
		result += intText [i] * (unsigned long) pow (2, 15 - i);
	}
	if (result == 0) result = (unsigned long) pow(2, 16);
	temp = (unsigned long) pow (2, 16) + 1 ;
	while (true)
	{
		if ((temp * n + 1) % result == 0)
		{
			result = (temp * n + 1) / result;
			break;
		}
		n++;
	}
	for (i = 0; i < length; i++)
	{
		intText[i] = result / (unsigned long) pow (2, 15 - i);
		result = result % (unsigned long) pow (2, 15 - i);
	}
}

BOOL CIDEA_Cipher::ManageCiphertext (CString Ciphertext)
{
	int i, j = 0, k = 0, count = 0;
	int length = Ciphertext.GetLength ();
	if (sign >= length) return FALSE;
	for (i = sign; i < length; i++)
	{
		if (Ciphertext [i] == '0' || Ciphertext [i] == '1')
		{
			if (k == 16) 
			{
				k = 0;
				j++;
			}
			m_intPlainArray [j][k++] = Ciphertext [i] - '0';
			count++;
		}
        if (count == 64) break;
	}
	if (count != 64) return FALSE;
	sign = i + 1;
	return TRUE;
}

BOOL CIDEA_Cipher::Init_KeyBox (int Kind) //产生密钥
{
	if (m_strKeyWord.GetLength () != 16) return FALSE;
	int i, j;
	ProduceIntArray(8, 16, KeyKind, m_strKeyWord);
	m_ValidBit = 0;
	for (i = 0; i < 9; i++)
	{
		DistillChildKey(0, m_ValidBit);
		for (j = 0; j < 6; j++)
		{
			if (Kind == CipherKind)
			{
				if (j == 0 || j == 3)
				{
					MulAthwartOperation (m_ChildKey[j], 16);
				}
				else if (j == 1 || j == 2)
				{
					AddAthwartOperation (m_ChildKey[j], 16);
				}
			}
			EvaluateArray (m_KeyBox[i][j], m_ChildKey[j], 16);
		}
	}
	return TRUE;
}

void CIDEA_Cipher::GetChildKeyBox(int turn, int Kind) //取子密钥
{
	int i;
	if (Kind == PlainKind)
		for (i = 0; i < 6; i++)
			EvaluateArray (m_ChildKey[i], m_KeyBox [turn][i], 16);
	else
	{
		if (turn == 8)
		{
			for (i = 0; i < 4; i++)
				EvaluateArray (m_ChildKey[i], m_KeyBox[0][i], 16);
		}
		else if (turn == 0)
		{
			for (i = 0; i < 6; i++)
			{
				if (i < 4)
					EvaluateArray (m_ChildKey[i], m_KeyBox[8 - turn][i], 16);
				else
					EvaluateArray (m_ChildKey[i], m_KeyBox[7 - turn][i], 16);
			}
		}
		else
		{
			for (i = 0; i < 6; i++)
			{
				if (i == 0 || i == 3)
					EvaluateArray (m_ChildKey[i], m_KeyBox[8 - turn][i], 16);
				else if (i == 1)
					EvaluateArray (m_ChildKey[i], m_KeyBox[8 - turn][2], 16);
				else if (i == 2)
					EvaluateArray (m_ChildKey[i], m_KeyBox[8 - turn][1], 16);
				else
					EvaluateArray (m_ChildKey[i], m_KeyBox[7 - turn][i], 16);
			}
		}
	}
}

⌨️ 快捷键说明

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