35. 通过实现一个序列加密的功能,熟悉对二维空间与一维空间的操作。



按行读的话,肯定可以读出数据,如果按列来读的话,则会出再乱码的现像。正
是这种现像可作为一种加密手段,称为序列加密。
hello everyone  原始序列
可以看成
hello_
everyo
ne****

按列提取
hen
eve
le*
lr*
oy*
_o*

henevele*lr*oy*_o*   此时就为加密后的序列


现在实现加密与解密的功能:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 char* encode(char* str,int col)
  5 {
  6     int i,j;
  7     int strLen = strlen(str);
  8     int bufLen = strLen + col - strLen%col;
  9 
 10     char* strBuf = malloc(bufLen+1);//线性一维空间,存储完原始数据后之后当成二维空间使用
 11     strcpy(strBuf,str);
 12     //   char* pbuf = buf;
 13     for(i = strLen;i < bufLen;i++)
 14     {
 15         *(strBuf+i) = '*';//strBuf[i] = '*';
 16     }
 17     *(strBuf+i) = '\0';
 18 
 19 
 20     //将原始字符串行列置反存储到新空间
 21     char* newStrBuf = malloc(bufLen+1);
 22     char* pNewStrBuf = newStrBuf;
 23     char (*pStrBuf)[col] = strBuf;//二维方式访问strBuf,
 24     for(j = 0;j < col;j++)//按顺序访问原始数据中每列上的所有行
 25     {
 26         for(i = 0;i<bufLen/col;i++)
 27         {
 28             *pNewStrBuf++ = pStrBuf[i][j];
 29         }
 30     }
 31     *pNewStrBuf = '\0';
 32 
 33 
 34     free(strBuf);
 35     return newStrBuf;
 36 }
 37 
 38 char* encode2(char* str,int col)
 39 {
 40     int i,j;
 41     int strLen = strlen(str);
 42     int bufLen = strLen + col - strLen%col;
 43 
 44     char* strBuf = malloc(bufLen);//线性一维空间,存储完原始数据后之后当成二维空间使用
 45     strcpy(strBuf,str);
 46     //   char* pbuf = buf;
 47     for(i = strLen;i < bufLen;i++)
 48     {
 49         *(strBuf+i) = '*';//strBuf[i] = '*';
 50     }
 51  //   *(strBuf+i) = '\0';
 52 
 53 
 54     //将原始字符串行列置反存储到新空间
 55     char* newStrBuf = malloc(bufLen+1);
 56     char* pNewStrBuf = newStrBuf;
 57     char (*pStrBuf)[col] = strBuf;//二维方式访问strBuf,
 58     for(j = 0;j < col;j++)//按顺序访问原始数据中每列上的所有行
 59     {
 60         for(i = 0;i<bufLen/col;i++)
 61         {
 62             *pNewStrBuf++ = pStrBuf[i][j];
 63         }
 64     }
 65     *pNewStrBuf = '\0';
 66 
 67 
 68     free(strBuf);
 69     return newStrBuf;
 70 }
 71 
 72 
 73 char* decode(char *str, int row)
 74 {
 75     int bufLen = strlen(str);
 76     char* buf = malloc(bufLen+1);
 77 
 78 
 79     int i,j;
 80     int col = (bufLen)/row;//计算出新的原始数据的列
 81 
 82     char (*pStr)[col] = str;
 83     char *pBuf = buf;
 84     for(j = 0;j < col ;j++)
 85     {
 86         for(i = 0;i < row;i++)
 87         {
 88              *pBuf++ = pStr[i][j];
 89         }
 90     }
 91     *pBuf = '\0';//这句可有可无
 92 
 93     //去*操作
 94     while(*(--pBuf) =='*');
 95     *(++pBuf) = '\0';
 96 
 97 
 98 
 99     return buf;
100 
101 }
102 
103 int main(void)
104 {
105    // int code = 6;
106     char str[] = "hello everyone";
107 
108     char* pe = encode(str,6);
109     printf("%s\n",pe);
110     char* pd = decode(pe,6);
111     printf("%s\n",pd);
112 
113     free(pe);
114     free(pd);
115 
116 
117     return 0;
118 }

 





 h e l l o  e v e r y o n e 0   
 0  1  2  3  4  5  6  7  8  9 1011121314151617




 h  e  l  l  o  e v e r y o n e 0 *  * *  0
 0 1 2  3  4 5  6  7  8  9 101112131415161718

 



 h  e  l  l  o    
 e v e  r  y o
 n e * * * *



  0     

转载于:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9494860.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值