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

📄 stream_cipher.cpp

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

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

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

/////////////////////////////////////////////////////////////////////////////

CMY_CAPDlg *Main_CapS;

CStream_Cipher::CStream_Cipher(CWnd* pParent /*=NULL*/)
	: CDialog(CStream_Cipher::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStream_Cipher)
	m_LFST_Size = 0;
	m_Feedback_Key = _T("");
	m_Init_Key = _T("");
	m_IStream_Size = 0;
	m_Feedback_Number = 0;
	m_CKey_Stream = _T("");
	//}}AFX_DATA_INIT
}

void CStream_Cipher::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStream_Cipher)
	DDX_Text(pDX, IDC_LFSTSIZE, m_LFST_Size);
	DDX_Text(pDX, IDC_FEEDBACKKEY, m_Feedback_Key);
	DDX_Text(pDX, IDC_Init_Key, m_Init_Key);
	DDX_Text(pDX, IDC_STREAM_SIZE, m_IStream_Size);
	DDX_Text(pDX, IDC_KEY_STREAM, m_CKey_Stream);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStream_Cipher, CDialog)
	//{{AFX_MSG_MAP(CStream_Cipher)
	ON_COMMAND(IDD_QUIT, OnQuit)
	ON_COMMAND(IDD_SETKEY, OnSetkey)
	ON_COMMAND(IDD_ENCIPHER, OnEncipher)
	ON_COMMAND(IDD_DECIPHER, OnDecipher)
	ON_COMMAND(IDD_HELP, OnHelp)
	ON_COMMAND(IDD_CREATE_KEYSTREAM, OnCreateKeystream)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStream_Cipher message handlers

//////////////////////////////////////////////////////////////////
///////////////////Stream Cipher ////////////////////////////////
/////////////////////////////////////////////////////////////////

void CStream_Cipher::OnQuit() //退出
{
	// TODO: Add your command handler code here
	OnOK();
	
}
void CStream_Cipher::OnSetkey() //初始化密钥及其反馈参数
{
	// TODO: Add your command handler code here
	UpdateData(TRUE);
	Main_CapS -> UpdateData(TRUE);
	m_Feedback_Number = 0;
	if (Initial_Key(m_Init_Key,m_LFST_Size) == TRUE)
		Initial_FeedbackKey(m_Feedback_Key,m_LFST_Size);
}

void CStream_Cipher::OnEncipher() //加密
{
	// TODO: Add your command handler code here
	Main_CapS -> UpdateData (TRUE);
	m_Ciphertext = "";
	m_CKey_Stream = "";
	m_IStream_Size = 0;
	m_Plaintext = Main_CapS -> m_Plaintext;
	Evaluate_Key();
	Produce_Ciphertext();
	UpdateData(FALSE);
	Main_CapS -> m_Ciphertext = m_Ciphertext;
	Main_CapS -> UpdateData(FALSE);
	if (Main_CapS ->m_Ciphertext.GetLength () != 0)
		Main_CapS -> OnSave_Ciphertext ();
}

void CStream_Cipher::OnDecipher() //解密
{
	// TODO: Add your command handler code here
	Main_CapS -> UpdateData (TRUE);
	m_Plaintext = "";
	m_CKey_Stream = "";
	m_IStream_Size = 0;
	m_Ciphertext = Main_CapS -> m_Ciphertext;
	Evaluate_Key();
	Produce_Plaintext();
	UpdateData(FALSE);
	Main_CapS -> m_Plaintext = m_Plaintext;
	Main_CapS -> UpdateData (FALSE);
	if (Main_CapS ->m_Plaintext.GetLength () != 0)
		Main_CapS -> OnSave_Plaintext ();
}

void CStream_Cipher::Trans_Dialog(CWnd *temp) //传递主界面指针
{
	Main_CapS = (CMY_CAPDlg *)temp;
}

BOOL CStream_Cipher::Initial_Key(CString Init_Key, int LFST_Size) //初始化密钥
{
	if (LFST_Size >0 && LFST_Size <= 48)
	{
		int count = 0, temp;
		for (int i = 0; i < LFST_Size; i++)
		{
			if (count >= Init_Key.GetLength())
				temp = 0;
			else
			{
				if (Init_Key[count] >= '0' && Init_Key[count] <= '9')
					temp = Init_Key[count]-'0';
				else if (Init_Key[count] >= 'A' && Init_Key[count] <= 'F')
					temp = Init_Key[count] - 'A' + 10;
				else 
				{
					MessageBox("Your input is illegal");
					return FALSE;
				}
			}
			for (int j = 3; j >= 0; j--)
			{
				m_Key[i] = Key[i] = temp / (int)pow(2, j);
				temp %= (int)pow(2,j);
				i++;
				if (i >= LFST_Size) break;
			}
			count++;
			i--;
		}
	}
	else 
	{
		MessageBox("Your input is illegal");
		return FALSE;
	}
	return TRUE;
}


void CStream_Cipher::Initial_FeedbackKey(CString Feedback_Key,int Max) //初始化反馈参数
{
	int count = Feedback_Key.GetLength (), temp = 0, j = 0, Feed =0;
	for (int i = 0; i < count; i++)
	{
		if (Feedback_Key[i] >= '0' && Feedback_Key[i] <= '9')
			temp = Feedback_Key[i]-'0';
		else if (Feedback_Key[i] >= 'A' && Feedback_Key[i] <= 'F')
			temp = Feedback_Key[i] - 'A' + 10;
		else 
		{
			m_Feedback_Number = 0;
			MessageBox("Your input is illegal");
			break;
		}
		for (j =3; j >=0; j--)
		{
			if ( temp/(int)pow(2,j) == 1 ) 
			{
				Feedback[m_Feedback_Number++] = Feed;
			}
			temp = temp % (int)pow(2,j);
			Feed++;
			if (Feed > Max) return;
		}
	}
}

int CStream_Cipher::Produce_Key() //生成一个字节的密钥
{
	int value = 0, i =0 ,j = 0, temp;
	for ( i = 7; i >= 0; i--)
	{
		value += Key[m_LFST_Size - 1] * (int) pow(2,i);
		m_CKey_Stream += (Key[m_LFST_Size - 1] + '0');
		m_IStream_Size++;
		if (m_IStream_Size % 16 == 0 )
		{
			m_CKey_Stream += 13;
			m_CKey_Stream += '\n';
		}
		temp = 0;
		for ( j = 0; j < m_Feedback_Number; j++)
			temp = temp ^ Key[Feedback[j]];
		for ( j = m_LFST_Size -1; j > 0; j--)
			Key[j] = Key[j-1];
		Key[0] = temp;
	}
	return value;
}

void CStream_Cipher::Produce_Ciphertext() //生成密文
{
	 int count = m_Plaintext.GetLength (), i = 0,temp = 0, Ikey;
	 for (i = 0; i < count; i++)
	 {
		 Ikey = Produce_Key();
		 temp = (int)m_Plaintext[i];
		 temp = temp ^ Ikey;
		 for (int j = 7; j >= 0; j--)
		 {
			 m_Ciphertext += (temp/(int)pow(2, j) + '0');
			 temp = temp % (int)pow(2, j);
		 }
	 }
}

void CStream_Cipher::Produce_Plaintext() //生成明文
{
	int count = m_Ciphertext.GetLength (), i, j, temp = 0, Ikey;
	for (i = 0; i < count; )
	{
		Ikey = Produce_Key();
		temp = 0;
		for ( j = 7; j >= 0; j--)
		{
			if ( i >= count) return;
			if (m_Ciphertext[i] == '0' || m_Ciphertext[i] == '1')
			{
				temp += ((int)m_Ciphertext[i] - '0') * (int) pow(2, j);
			}
			else if (m_Ciphertext[i] == ' ')
			{
				i++;
				j++;
				continue;
			}
			else if (m_Ciphertext[i] == 13 && m_Ciphertext[i+1] == '\n')
			{
				i += 2;
				j++;
				continue;
			}
			else
			{
				MessageBox("Your Ciphertext is illegal");
				return;
			}
			i++;
		}
		temp = temp ^ Ikey;
		m_Plaintext += temp;
	}
}

void CStream_Cipher::OnHelp() //打开帮助文档
{
	// TODO: Add your command handler code here
	WinExec("notepad.exe Stream.txt",SW_SHOW);
}

void CStream_Cipher::Evaluate_Key() //  还原初始密钥
{
	for (int i = 0; i < m_LFST_Size; i++)
		Key[i] = m_Key[i];
}

void CStream_Cipher::OnCreateKeystream() 
{
	// TODO: Add your command handler code here
	UpdateData(TRUE);
	int i, Max;
	m_CKey_Stream = "";
	Max = (int)ceil(m_IStream_Size/8.0);
	m_IStream_Size = 0;
	for (i = 0; i < Max; i++)
	{
		Produce_Key();
	}
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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