RC4密码算法实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_32880691/article/details/51835716
<pre name="code" class="cpp">#include<iostream>
#include<string>
using namespace std;

void swap(int&, int&);

int main(){
	int bit;//这个bit实际是二的这个次方才对,为了避免引入算2的n次方就直接用了吧 
	cout << "请输入元素个数: ";cin >> bit;
	int* S = new int[bit];
	int* T = new int[bit];
	int* key = new int[bit];
	//连续临时向量 
	int start,end;
	cout << "请输入连续临时向量范围: ";	cin >> start >> end;
	for(int i=0,j=start; i<end-start+1 ; i++,j++)
		key[i] = j; //这里不要想着求key[i]的实际长度要用什么函数,根据你输入的直接算即可
	
 
	for(int i=0 ; i<bit ; i++){
		S[i] = i;
		T[i] = key[i%(end-start+1)];
	}
	//检查S[i],T[i]
	cout << "S[i]: ";
	for(int i=0 ; i<bit ; i++)
		cout << S[i];
	cout << endl;
	cout << "T[i]: ";
	for(int i=0 ; i<bit ; i++)
		cout << T[i];
	cout << endl;
	
	
	for(int i=0,j=0; i<bit ; i++){
		j = (j+S[i]+T[i])%bit;
		swap(S[i] , S[j]);
	} 
	
	int times;//输入你需要的k的个数
	cout << "请输入k的个数: ";cin >>times;
	for(int i=0,j=0,time=1,t,k ; time<=times ; time++){
		i=(i+1)%bit;
		j= (j+S[i])%bit;
		swap(S[i] , S[j]);
		t = (S[i]+S[j])%bit;
		k = S[t];
		cout << k << endl;
	} 
	
	delete key;
	delete T;
	delete S;
	return 0;
} 
void swap (int &a, int &b){
	int c;
	c=a;
	a=b;
	b=c;
}



          
展开阅读全文

没有更多推荐了,返回首页