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

📄 try.cpp

📁 流密码RC4的加密和解密 1、能够对字串进行加密; 2、可以自动生成加密密钥和解密密钥对; 3、能够显示给定密钥下明文加密后的密文; 4、能够用相应解密密钥正确解密密文
💻 CPP
字号:
#include<iostream>
#include<fstream>
using namespace std;

void main()
{   char s[256],T[256];                         //定义初始化向量表S和置换表T
    char k;
	int t,i,j,klen1,plen,m;
    
	cout<<"please enter the length of key:"<<endl;   //输入密钥的长度
	cin>>klen1;
    char* k1=new char[klen1];
	cout<<"please enter the key:"<<endl;         //输入密钥长度
	for(i=0;i<klen1;i++)
	    cin>>k1[i];

	//cout<<"please enter the length of plaintext:"<<endl;  //输入明文的长度
	//cin>>plen;
	ofstream outfile("plaintext.dat",ios::out);    //打开明文文件
	if(!outfile){
		cerr<<"open plaintext.dat error!"<<endl;
		exit(1);
	}
	char p[256];
	cin.getline(p,256);
	char ch;
	for(plen=0;p[plen]!=0;plen++)
        outfile.put(p[plen]);
    outfile.close();

    //char* p=new char[plen];
    char* c=new char[plen];              //同时初始化密文和解密后的明文
    char* d=new char[plen];

	//cout<<"please enter the plaintext:"<<endl;     //输入明文内容
   // for(i=0;i<plen;i++)
	//	cin>>p[i];
	//cout<<endl;

	for(j=0;j<256;j++){                  //初始化S,同时建立一个临时向量T
		s[j]=j;                                   
	    T[j]=k1[j % klen1];             //将K的值循环赋给T中的元素
	}

	j=0;                                    //用T产生S的初始置换
    for(i=0;i<256;i++){
	   j=(j+s[i]+T[i]) % 256;
	   swap(s[i],s[j]);
	}

	i=0;j=0;                             //密钥流的生成,对每个S[i],根据
	for(m=0;m<plen;m++){         //当前S的值,将S[i]与S中的另一字节置换。
	 i=(i+1) % 256;                               
     j=(j+s[i]) % 256;
	 swap(s[i],s[j]);
	 t=(s[i]+s[j]) % 256;
	 k=s[t];
	 c[m]=k ^ p[m];                        //加密操作,产生密文
	}

	//cout<<"the cipertext is:"<<endl;          //输出密文
	ofstream outfile2("cipertext.dat");
    if(!outfile){
		cerr<<"open cipertext.dat error!"<<endl;
		exit(1);
	}
	for(i=0;i<plen;i++)
	//cout<<c[i];
	outfile2.put(c[i]);
	outfile2.close();
    cout<<endl;
	cout<<endl;
    cout<<"************"<<endl;
	char a;
	cout<<"Do you want to decode?(Y/N)"<<endl;     //询问是否解密
	cin>>a;

	if(a=='N')                         //选择"N"结束程序
    cout<<"***end***"<<endl;
    else{
		int klen2;
	    cout<<"please enter the length of your key:"<<endl;  //输入解密密钥
	    cin>>klen2;
		if(klen2==klen1){                  //检查密钥长度是否匹配
        char* k2=new char[klen2];
	    cout<<"please input your key:"<<endl;
        for(i=0;i<klen2;i++)
        cin>>k2[i];
	    cout<<endl;

		for(j=0;j<256;j++){                 //开始解密(过程同加密)
		s[j]=j;
	    T[j]=k2[j % klen2];
		}

	j=0;
    for(i=0;i<256;i++){
	j=(j+s[i]+T[i]) % 256;
	swap(s[i],s[j]);
	}
    
	i=0;j=0;
	for(m=0;m<plen;m++){
	 i=(i+1) % 256;
     j=(j+s[i]) % 256;
	 swap(s[i],s[j]);
	 t=(s[i]+s[j]) % 256;
	 k=s[t];
	 d[m]=k ^ c[m];                         //解密得到明文
	}
	
    ofstream outfile("decodedtext.dat");
    if(!outfile){
		cerr<<"open decodedtext.dat error!"<<endl;
		exit(1);
	}
	cout<<"the plaintext is:"<<endl;               //输出明文
	for(i=0;i<plen;i++){
	outfile.put(d[i]);
    cout<<d[i];
	}
	outfile.close();
	cout<<endl;
    cout<<"***end***"<<endl;
		}
	else cout<<"warning:the key is wrong!"<<endl;	
	}
}

⌨️ 快捷键说明

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