112、文本串的加密

112、文本串的加密:

目标与要求.一个文本串可用事先给定字母映射表进行加密,字母映射表如下:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

NgzQTCobmUHelkPdAwxfYIvrsJGnZqtcOBMuhELKpaDWXFyiVRjS

未被映射的字符不加以改变,例如,字符串e*ncrypt”被加密成“T*kzwsd{”,试写一程序要求采用菜单方式实现相应功能,其选项及功能说明如下:

(1)加密——将输入的文本串进行加密后输出;

(2)解密——将输入的已加密的文本进行解密后输出;

(3)退出一退出运行。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <string>
  4 #include <stdlib.h>
  5 using namespace std;
  6 int n;//选项
  7 
  8 void init()
  9 {
 10     //界面
 11     cout<<""<<endl;
 12     cout<<""<<endl;
 13     cout<<"                                     文本串的加密 "<<endl;
 14     cout<<""<<endl;
 15     cout<<"                      (1)加密——将输入的文本串进行加密后输出;"<<endl;
 16     cout<<""<<endl;
 17     cout<<"                      (2)解密——将输入的已加密的文本进行解密后输出;"<<endl;
 18     cout<<""<<endl;
 19     cout<<"                      (3)退出一退出运行。"<<endl;
 20     cout<<""<<endl;
 21 }
 22 
 23 void encrypt()//加密
 24 {
 25     string a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 26     string b="NgzQTCobmUHelkPdAwxfYIvrsJGnZqtcOBMuhELKpaDWXFyiVRjS";
 27     string c;
 28     cout<<"                      请输入需要加密的文本串: "<<endl;
 29     cout<<endl;
 30     cout<<"                      ";
 31     cin>>c;
 32     cout<<endl;
 33     cout<<"                      加密后的文本串为: "<<endl;
 34     cout<<endl;
 35     cout<<"                      ";
 36     int lc=c.length();
 37     for(int i=0;i<lc;i++){
 38         int flag=1;
 39         for(int j=0;j<52;j++){
 40             if(c[i]==a[j]){
 41                 cout<<b[j];
 42                 flag=0;
 43                 break;
 44             }
 45         }
 46         if(flag){
 47             cout<<c[i];
 48         }
 49     }
 50     cout<<endl;
 51 }
 52 
 53 void decrypt()//解密
 54 {
 55     string a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 56     string b="NgzQTCobmUHelkPdAwxfYIvrsJGnZqtcOBMuhELKpaDWXFyiVRjS";
 57     string c;
 58     cout<<"                      请输入需要解密的文本串: "<<endl;
 59     cout<<endl;
 60     cout<<"                      ";
 61     cin>>c;
 62     cout<<endl;
 63     cout<<"                      解密后的文本串为: "<<endl;
 64     cout<<endl;
 65     cout<<"                      ";
 66     int lc=c.length();
 67     for(int i=0;i<lc;i++){
 68         int flag=1;
 69         for(int j=0;j<52;j++){
 70             if(c[i]==b[j]){
 71                 cout<<a[j];
 72                 flag=0;
 73                 break;
 74             }
 75         }
 76         if(flag){
 77             cout<<c[i];
 78         }
 79     }
 80     cout<<endl;
 81 }
 82 
 83 void exit()
 84 {
 85     cout<<""<<endl;
 86     cout<<""<<endl;
 87     cout<<""<<endl;
 88     cout<<"                                     退出运行 !!!  "<<endl;
 89     cout<<""<<endl;
 90     cout<<""<<endl;
 91     cout<<""<<endl;
 92     cout<<""<<endl;
 93     system("Pause");
 94 }
 95 
 96 int main()
 97 {
 98     init();
 99     while(1){
100         cout<<"                      请按下功能键 (1 或 2 或 3)"<<endl;
101         cout<<endl;
102         cout<<"                      ";
103         cin>>n;
104         cout<<endl;
105         if(n==1) encrypt();
106         if(n==2) decrypt();
107         if(n==3) exit();
108         init();
109     }
110     return 0;
111 }

原文地址:https://www.cnblogs.com/shixinzei/p/8253078.html