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

📄 rc4.cpp

📁 学习RC4时写的一个简单的程序
💻 CPP
字号:
// RC4.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "RC4.h"
#include "RC4Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRC4App

BEGIN_MESSAGE_MAP(CRC4App, CWinApp)
	//{{AFX_MSG_MAP(CRC4App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRC4App construction

CRC4App::CRC4App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CRC4App object

CRC4App theApp;

/////////////////////////////////////////////////////////////////////////////
// CRC4App initialization

BOOL CRC4App::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CRC4Dlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
void swap(int &a,int &b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
}

void RC4::GetKey(CString strdata)
{
	int count,k=0;
	char ch;
	CString *str;
	count=strdata.GetLength();
	for(int i=0;i<count;i++)
	{
		ch=strdata.GetAt(i);
        if(ch==' ') k++;
    }
	str=new CString [k+1];
    for(i=0;i<k+1;i++)
	{
		str[i]=strdata.SpanExcluding(" ");
		int length,x;
		x=count-length;
		length=str[i].GetLength();
		for(int j=0;j<=length;j++)
		strdata.Delete(0,1);
	}
    for(i=0;i<256;i++)
	{
		key[i]=atoi(str[i%(k+1)]);//将字符串转换成整形,并填充整个密钥数组
	}
}
void RC4::InitialSbox()
{
    for(int i=0;i<256;i++)
	     s[i]=i;

}
void RC4::KSA()
{
	int i, j=0;
	for(i=0;i<256;i++)
	{	
		j=( i + s[i] + key[i]) %256;
		swap(s[i], s[j]);
	}

}
int RC4::Psga(int &i,int &j)
{
   int t,k;
   i= (i + 1) % 256;
   j= (j + s[i]) % 256;
   swap(s[i], s[j]);
   t=(s[i]+s[j])% 256;
   k=s[t];
   return k;
}
CString RC4::cipher(CString strdata,int k)
{
	CString result="",*str;
	int length,b[8],c[8];
	for(int i=7;i>=0;i--)
	{
        b[i]=k%2;
		k=k/2;
	}
	unsigned char ch;
	length=strdata.GetLength();
	str=new CString [length];
    for(i=0;i<length;i++)
	{
		ch=strdata.GetAt(i);
		for(int j=7;j>=0;j--)
		{
		   c[j]=(int)ch%2;
		   ch=ch/2;
		}
		for(j=0;j<8;j++)
		{
		   b[j]=(b[j]+c[j])%2;
		}	
	 str[i].Format("%d%d%d%d%d%d%d%d",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
	}
    for(i=0;i<length;i++)
		result+=str[i];
	
	return result;
}

⌨️ 快捷键说明

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