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

📄 lfsr.cpp

📁 1、对于凯撒密文
💻 CPP
字号:
// Lfsr.cpp: implementation of the CLfsr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "030300816.h"
#include "Lfsr.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLfsr::CLfsr()
{

}

CLfsr::~CLfsr()
{

}

void CLfsr::Encipher()
{
	bitset<128> m_bitInitKey(m_decInitKey);
	bitset<128> m_bitFbkKey(m_decFbkKey);
	bitset<128> m_bitTemp(0);
	CString temp=_T("");
	for ( int i = 0 ; i < m_plainString.GetLength() ; i ++ )
	{
		char ch = (char)m_plainString.GetAt( i );
		char tt = 0x01;
		char bi[8];//convert char to bi[8] in binary
		for ( int p = 0 ; p < 8 ; p ++ )
		{
			if ( ch & tt )
				bi[7-p] = '1';
			else
				bi[7-p] = '0';
			tt <<= 1;
		}
		for ( int k = 0 ; k < 8 ; k ++ )
		{
			if ( ((m_bitInitKey[0] == 1 )&& (bi[k] == '1'))
				||
				((m_bitInitKey[0] == 0 )&& (bi[k] == '0')) 
				) 
				temp+="0";
			else 
				temp+="1";
			m_bitTemp = ( m_bitInitKey & m_bitFbkKey );
			if ( m_bitTemp.count() % 2 == 1 )
			{
				m_bitInitKey >>= 1;
				m_bitInitKey.set(m_keySize-1);
			}
			else
			{
				m_bitInitKey >>= 1;
				m_bitInitKey.reset(m_keySize-1);
			}
		}
	}
	m_cipherString = temp;
}

void CLfsr::Decipher()
{
	bitset<128> m_bitInitKey(m_decInitKey);
	bitset<128> m_bitFbkKey(m_decFbkKey);
	bitset<128> m_bitTemp(0);
	CString temp = _T("");
	CString plainTemp = _T("");
	long chTemp = 0;
	char ch;
	for ( int i = 0 ; i < m_cipherString.GetLength() ; )
	{
		for ( int j = 0 ; j < 8 ; j ++ )
		{
			ch = m_cipherString.GetAt( i );
			if ( ( ch == '1' && m_bitInitKey[0] == 1 )
				||
				( ch == '0' && m_bitInitKey[0] == 0 ) )
				temp += "0";
			else
				temp += "1";
			m_bitTemp = ( m_bitInitKey & m_bitFbkKey );
			if ( m_bitTemp.count() % 2 == 1 )
			{
				m_bitInitKey >>= 1;
				m_bitInitKey.set(m_keySize-1);
			}
			else
			{
				m_bitInitKey >>= 1;
				m_bitInitKey.reset(m_keySize-1);
			}
			i ++;
		}
		chTemp = StrToData( temp , 2 );
		plainTemp += (char)chTemp;
	}
	m_plainString = plainTemp;
}

⌨️ 快捷键说明

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