第八周 项目四--字符串加密

来源:互联网 发布:什么是安全控件windows 编辑:程序博客网 时间:2024/05/23 12:20

问题及代码:

main.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*                 
  2. Copyright (c)2016,烟台大学计算机与控制工程学院                 
  3. All rights reserved.                 
  4. 文件名称:字符串加密.cpp                 
  5. 作    者:   陈朋           
  6. 完成日期:2016年10月21日                 
  7. 版 本 号:v1.0                    
  8. 问题描述:            
  9. 输入描述:串             
  10. 程序输出:加密后的串。              
  11. */        
  12. #include <stdio.h>  
  13. #include "sqString.h"  
  14.   
  15. SqString A,B; //用于存储字符映射表  
  16.   
  17. SqString EnCrypt(SqString p)  
  18. {  
  19.     int i=0,j;  
  20.     SqString q;  
  21.     while (i<p.length)  
  22.     {  
  23.         for (j=0; p.data[i]!=A.data[j]&&j<A.length; j++);  
  24.         if (j>=A.length)            //在A串中未找到p.data[i]字母  
  25.             q.data[i]=p.data[i];  
  26.         else                        //在A串中找到p.data[i]字母  
  27.             q.data[i]=B.data[j];  
  28.         i++;  
  29.     }  
  30.     q.length=p.length;  
  31.     return q;  
  32. }  
  33.   
  34. SqString UnEncrypt(SqString q)  
  35. {  
  36.     int i=0,j;  
  37.     SqString p;  
  38.     while (i<q.length)  
  39.     {  
  40.         for (j=0; q.data[i]!=B.data[j]&&j<B.length; j++);  
  41.         if (j>=B.length)            //在B串中未找到q.data[i]字母  
  42.             p.data[i]=q.data[i];  
  43.         else                    //在B串中找到q.data[i]字母  
  44.             p.data[i]=A.data[j];  
  45.         i++;  
  46.     }  
  47.     p.length=q.length;  
  48.     return p;  
  49. }  
  50.   
  51. int main()  
  52. {  
  53.     SqString p,q;  
  54.     StrAssign(A,"abcdefghijklmnopqrstuvwxyz");  //建立A串  
  55.     StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");  //建立B串  
  56.     char str[MaxSize];  
  57.     printf("输入原文串:");  
  58.     gets(str);                                  //获取用户输入的原文串  
  59.     StrAssign(p,str);                           //建立p串  
  60.     printf("加密解密如下:\n");  
  61.     printf("  原文串:");  
  62.     DispStr(p);  
  63.     q=EnCrypt(p);                               //p串加密产生q串  
  64.     printf("  加密串:");  
  65.     DispStr(q);  
  66.     p=UnEncrypt(q);                         //q串解密产生p串  
  67.     printf("  解密串:");  
  68.     DispStr(p);  
  69.     printf("\n");  
  70.     return 0;  
  71. }  
  72. 运行结果:

0 0
原创粉丝点击