1. /** 
  2.  * 加密解密 
  3.  * @file encrty.c 
  4.  * @brief    
  5.  * @author struggleLinux@gmail.com 
  6.  * @version 0.1 
  7.  * @date 2011-05-10 
  8.  */ 
  9. #include <stdio.h> 
  10. #include <string.h> 
  11.  
  12. #define  MAX 30 
  13.  
  14. char *str_key = "TRAILBAZERS"
  15.  
  16. char alpha[MAX] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  17.  
  18. char key[MAX] = ""
  19.  
  20. /** 
  21.  * 对key进行处理 
  22.  * @brief   prepare_key  
  23.  *  
  24.  * @param   key 
  25.  * 
  26.  * @return   
  27.  */ 
  28. int prepare_key( char * key ); 
  29.  
  30.  
  31. /** 
  32.  * 加密 
  33.  * @brief   encrypt  
  34.  * 
  35.  * @param   data 
  36.  * @param   key 
  37.  */ 
  38. void encrypt( char *data ,char const *key ); 
  39.  
  40.  
  41. /** 
  42.  * 解密 
  43.  * @brief   decrypt  
  44.  * 
  45.  * @param   data 
  46.  * @param   key 
  47.  */ 
  48. void decrypt( char *data ,char const *key ); 
  49.  
  50.  
  51. int main() 
  52.     char date[] = "ATTACK AT DAWN"
  53.     if(prepare_key(key) != 0 ){ 
  54.         printf("key = %s \n",key); 
  55.         encrypt(date,key); 
  56.         printf("加密后 date = %s \n",date); 
  57.         decrypt(date,key); 
  58.         printf("解密后 date = %s \n",date);     
  59.     }else 
  60.         printf("编码错误 : %s\n",key); 
  61.     return 0; 
  62.  
  63. int prepare_key( char * key ) 
  64.     //定义返回值 1 为字符串转换成功, 0为字符串转换失败 
  65.     int result = 1; 
  66.     char  *temp ,*alpha_t; 
  67.      
  68.     temp = key; 
  69.     alpha_t = alpha; 
  70.      
  71.     //将密钥中的小写字符转换为大写字符,并将重复的字符去除 
  72.     while ( *str_key != '\0'){ 
  73.         if(islower(*str_key)) 
  74.             toupper(*str_key); 
  75.         if(!isalpha (*str_key)){ 
  76.             result = 0; 
  77.             break
  78.         } 
  79.         if( strrchr(temp,*str_key) == 0) 
  80.               *key++ = *str_key; 
  81.         str_key++; 
  82.     } 
  83.  
  84.     //将alpha中不存在于密钥 str_key 中的字母添加到key中  
  85.     while( result != 0 && *alpha_t != '\0'){ 
  86.         if( strrchr( temp, *alpha_t) == 0 ) 
  87.             *key++ = *alpha_t; 
  88.         alpha_t++; 
  89.     } 
  90.  
  91.     //判断key是否为空 
  92.     if(key == NULL) 
  93.         result = 0; 
  94.  
  95.     return result; 
  96.  
  97. void encrypt( char *date ,char const *key ) 
  98.     char * find_char; 
  99.  
  100.     //将date中的字符替换,替换后的字符大小写格式与 date初始值 相同 
  101.     while( *date != '\0' ){ 
  102.         if( isalpha(*date) ){ 
  103.             find_char = strchr( alpha , toupper(*date)); 
  104.             if(islower(*date)){ 
  105.                 *date =  *( key +strlen(alpha)-strlen(find_char)); 
  106.                 *date = tolower(*date); 
  107.             }else
  108.                 *date =  *( key +strlen(alpha)-strlen(find_char)); 
  109.             } 
  110.         }    
  111.         date++; 
  112.     } 
  113.  
  114. void decrypt( char * date , char const * key ) 
  115.     char * find_char; 
  116.  
  117.     while( *date != '\0' ){ 
  118.         if( isalpha(*date ) ){ 
  119.             find_char = strchr( key , toupper(*date)); 
  120.             if( islower(*date) ){ 
  121.                 *date = *(alpha + strlen(key)-strlen(find_char)); 
  122.                 *date = tolower(*date); 
  123.             }else
  124.                 *date = *(alpha + strlen(key)-strlen(find_char)); 
  125.              
  126.             } 
  127.         } 
  128.         date++; 
  129.     } 

 
  

VIM 7.3

gcc 版本 : 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)  
system : Fedora 14