zoukankan      html  css  js  c++  java
  • 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) = '';
     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 = '';
     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) = '';
     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 = '';
     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 = '';//这句可有可无
     92 
     93     //去*操作
     94     while(*(--pBuf) =='*');
     95     *(++pBuf) = '';
     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
    ",pe);
    110     char* pd = decode(pe,6);
    111     printf("%s
    ",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  10 11 12 13 14 15 16 17




     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  10 11 12 13 14 15 16 17 18



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



      0     

  • 相关阅读:
    CF505E-Mr. Kitayuta vs. Bamboos【贪心,二分】
    51nod2626-未来常数【树上启发式合并,线段树】
    P5371-[SNOI2019]纸牌【矩阵乘法】
    [HAOI2018]奇怪的背包
    [POI2015]MYJ
    CF1132F Clear the String
    [机房测试]矿石
    react报错:Can‘t perform a React state update on an unmounted component
    react 路由 404页面配置
    9.14JavaWeb之@WebServlet注解
  • 原文地址:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9494860.html
Copyright © 2011-2022 走看看