【Agorithm】一次一密加密解密算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<ctime>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<string>
 8 #include<windows.h>
 9 #define MAX 100
10 
11 using namespace std;
12 
13 char key[MAX];
14 int len;
15 
16 char *bitcode(char *str){
17     char *wen;
18     if((wen = (char*)malloc(len+1))==NULL){
19         cout<<"申请内存失败!"<<endl;
20         exit(1);
21     }
22     for(int i=0; i<len; i++){
23         wen[i] = str[i]^key[i];
24     }
25     wen[len] = '\0';
26     return wen;
27 }
28 
29 
30 int main(){
31 
32     char str[MAX];
33     char *miwen, *mingwen;
34     char again;
35 
36     srand(time(NULL));
37     cout<<"\t\t\t\t一次一密加密算法演示!\n\n";
38     s1:
39         cout<<"请输入需要加密的明文字符串:";
40         fflush(stdin);
41         gets(str);
42         len = strlen(str);
43         for(int i=0; i<len; i++){
44             key[i] = rand()%10+'0';
45         }
46         cout<<"此次加密的密钥序列为:";
47         for(int i=0; i<len; i++){
48             cout<<key[i]; 
49         }
50         cout<<endl;
51         miwen = bitcode(str);
52         cout<<"加密前的明文为:";
53         cout<<str<<endl;
54         cout<<"加密后的密文为:"<<miwen<<endl;
55         mingwen = bitcode(miwen);
56         cout<<"解密后的明文为:"<<mingwen<<endl;
57         cout<<endl;
58 
59     s2:
60         cout<<"继续执行 (Y/N)?";
61         fflush(stdin);
62         cin>>again;
63         if(again =='y'|| again =='Y'){
64             goto s1;
65         }
66         else if(again == 'n' || again == 'N'){
67             cout<<"演示结束!"<<endl;
68             return 0;
69         }
70         else{
71             cout<<"输入错误请重新输入!"<<endl;
72             goto s2;
73         }
74         system("pause");
75 }

以下是运行结果:

 

posted @ 2016-09-11 20:17  Dragonir  阅读(1125)  评论(0编辑  收藏  举报