第8週実践項目4-文字列暗号化

2424 ワード

/* 
Copyright (c)2015,               
All rights reserved. 
    :  4.cbp 
      :   
    :2015 11 30  
     :v1.0 
 
 
    :                      。  ,       : 
           
          abcdefghijklmnopqrstuvwxyz 
          ngzqtcobmuhelkpdawxfyivrsj 
 
       “lao he jiao shu ju jie gou”    “enp bt umnp xby uy umt opy”。  
         ,    、    ,             ,         。 
    :  
    :     
*/  
        【<a target=_blank href="http://blog.csdn.net/lcx16/article/details/49852261">      </a>】

コード:
#include "sqstring.h"  
  
SqString A,B; //           
  
SqString EnCrypt(SqString p)  
{  
    int i=0,j;  
    SqString q;  
    while (i<p.length)  
    {  
        for (j=0; p.data[i]!=A.data[j]; j++);  
        if (j>=p.length)            // A     p.data[i]    
            q.data[i]=p.data[i];  
        else                        // A    p.data[i]    
            q.data[i]=B.data[j];  
        i++;  
    }  
    q.length=p.length;  
    return q;  
}  
  
SqString UnEncrypt(SqString q)  
{  
    int i=0,j;  
    SqString p;  
    while (i<q.length)  
    {  
        for (j=0; q.data[i]!=B.data[j]; j++);  
        if (j>=q.length)            // B     q.data[i]    
            p.data[i]=q.data[i];  
        else                    // B    q.data[i]    
            p.data[i]=A.data[j];  
        i++;  
    }  
    p.length=q.length;  
    return p;  
}  
  
int main()  
{  
    SqString p,q;  
    StrAssign(A,"abcdefghijklmnopqrstuvwxyz");  //  A   
    StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");  //  B   
    char str[MaxSize];  
    printf("
"); printf(" :"); gets(str); // StrAssign(p,str); // p printf(" :
"); printf(" :"); DispStr(p); q=EnCrypt(p); //p q printf(" :"); DispStr(q); p=UnEncrypt(q); //q p printf(" :"); DispStr(p); printf("
"); return 0; }
<img src="http://img.blog.csdn.net/20151130195429639" alt="" />