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

📄 rc4.cpp

📁 最常见的流密码RC4的visual c++实现。希望对大家有帮助。
💻 CPP
字号:
// RC4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;
void endecrypt(char input[],char inputkey[],int len,int keylen);
void swap(int a,int b);
int main()
{
	char input[256];
	char inputkey[256];
	int k=0;
	int i=0;
	cout<<"**************This is a RC4 cipher************"<<endl;
	cout<<"***********20061120130-------张小斌***********"<<endl;
	ifstream file1("input.txt");
	ifstream file2("inputkey.txt");
	while(!file1.eof())
	{
		file1.get(input[k]);
		k++;
	}
	cout<<"plaintext is:"<<endl;
	for(int a=0;a<k;a++)
	cout<<input[a];
	cout<<endl;
	while(!file2.eof())
	{
		file2.get(inputkey[i]);
		i++;
	}
	cout<<"key is:"<<endl;
	for(int a=0;a<i;a++)
	cout<<inputkey[a];
	cout<<endl;
	endecrypt(input,inputkey,k,i);
	getchar();
	getchar();
	return 0;
}
void endecrypt(char input[],char inputkey[],int len,int keylen)
{
	int S[256];
	char T[256];
	int output[256];
	for(int i=0;i<256;i++)
	{
		S[i]=i;
		T[i]=inputkey[i%keylen];
	}
	for(int c=0;c<256;c++)
	{
		int j=0;
		j=(j+S[c]+T[c])%256;
		swap(S[c],S[j]);
	}
	int a=0,b=0,t=0;
	for(int i=0;i<len;i++)
	{
		char K;
		a=(a+1)%256;
		b=(b+S[a])%256;
		swap(S[a],S[b]);
		t=(S[a]+S[b])%256;
		K=S[t];
		output[i]=S[t]^input[i];
	}
	cout<<"ciphertext is:"<<endl;
    char ch=' ';
	ofstream file;
	file.open("output.txt");
	for(int i=0;i<len;i++)
	{
	file.put(output[i]);
	file.put(ch);
	}
	file.close();
	for(int i=0;i<len;i++)
	cout<<output[i]<<" ";
}
void swap(int a,int b)
{
	int t;
	t=a;
	a=b;
	b=t;
}


⌨️ 快捷键说明

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