- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<ctime>
- #include<cmath>
- #include<cstring>
- #include<string>
- #include<windows.h>
- #define MAX 100
-
- using namespace std;
-
- char key[MAX];
- int len;
-
- char *bitcode(char *str){
- char *wen;
- if((wen = (char*)malloc(len+1))==NULL){
- cout<<"申请内存失败!"<<endl;
- exit(1);
- }
- for(int i=0; i<len; i++){
- wen[i] = str[i]^key[i];
- }
- wen[len] = '\0';
- return wen;
- }
-
-
- int main(){
-
- char str[MAX];
- char *miwen, *mingwen;
- char again;
-
- srand(time(NULL));
- cout<<"\t\t\t\t一次一密加密算法演示!\n\n";
- s1:
- cout<<"请输入需要加密的明文字符串:";
- fflush(stdin);
- gets(str);
- len = strlen(str);
- for(int i=0; i<len; i++){
- key[i] = rand()%10+'0';
- }
- cout<<"此次加密的密钥序列为:";
- for(int i=0; i<len; i++){
- cout<<key[i];
- }
- cout<<endl;
- miwen = bitcode(str);
- cout<<"加密前的明文为:";
- cout<<str<<endl;
- cout<<"加密后的密文为:"<<miwen<<endl;
- mingwen = bitcode(miwen);
- cout<<"解密后的明文为:"<<mingwen<<endl;
- cout<<endl;
-
- s2:
- cout<<"继续执行 (Y/N)?";
- fflush(stdin);
- cin>>again;
- if(again =='y'|| again =='Y'){
- goto s1;
- }
- else if(again == 'n' || again == 'N'){
- cout<<"演示结束!"<<endl;
- return 0;
- }
- else{
- cout<<"输入错误请重新输入!"<<endl;
- goto s2;
- }
- system("pause");
- }
以下是运行结果: