其他分享
首页 > 其他分享> > 序列加密

序列加密

作者:互联网

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
char* encode(char* buf,int line)
{
    int len=strlen(buf);
    int nlen=len;
    if(len%line)
        nlen=len+(line-len%line);
    char* tmp=(char*)malloc(nlen+1);
    strcpy(tmp,buf);
    for(int i=strlen(tmp);i<nlen;i++)
    {
        tmp[i]='*';
    }
    tmp[nlen]='\0';
    char* de=(char*)malloc(nlen+1);
    char* se=de;
    char(*rp)[line]=tmp;
    for(int i=0;i<line;i++)
    {
        for(int j=0;j<(nlen/line);j++)
        {
            *se++=rp[j][i];
        }
    }
    return de;
}

char* decode(char* buf,int line)
{
    int len=strlen(buf);
    char* tmp=(char*)malloc(len+1);
    char* str=tmp;
    int row=len/line;
    char(*rp)[line]=buf;
    for(int i=0;i<line;i++)
    {
        for(int j=0;j<row;j++)
        {
            *str++=rp[j][i];
        }
    }
    while((*--str)=='*');
    *(++str)='\0';
   return tmp;
}

int main(int argc,char* argv[])
{
    char* str="china is great";
    char* s=encode(str,3);
    printf("%s\n",s);

    char* ss=decode(s,strlen(s)/3);
    printf("%s\n",ss);

    return 0;
}

 

标签:tmp,加密,int,len,nlen,char,序列,line
来源: https://www.cnblogs.com/SunShine-gzw/p/15783562.html