这是列换位法加密解密过程,怎么出个错误啊。怎么改,求加密解密代码
#include<stdio.h>#include<string.h>
#include<time.h>
#define CODELEN 256 /*密文长度.*/
char *encode(char *dest,char *str,int key)
{ int i,j,len,len2,n=0; len=strlen(str);
if((len % key)!=0) len2=len+ key-(len % key);
else len2=len; srand(time(NULL));
for(i=len;i<len2;i++)
str[i]=(char)((rand() % 26) + (rand() % 1)*('A'-'a')+'a');
str[len2]=0;
for(j=0;j<key;j++)
for(i=0;i<len2/key;i++) dest[n++]=str[i*key+j];
dest[n]=0; str[len]=0; return dest; }
char *decode(char *dest,char *str,int key)
{ int i,j,len,n=0;
len=strlen(str);
if((len % key)!=0)
return NULL;
len=len / key;
for(i=0;i<len;i++)
for(j=0;j<key;j++)
{ dest[n++]=str[j*len+i]; //printf("%c",dest[n-1]); }
dest[n]=0; return dest; }
int main()
{ int key,i,j,len; char str[CODELEN]; char xstr[CODELEN];
scanf("%s",str);
scanf("%d",&key);//输入密钥
printf("原始明文:%s\n",str);
printf("加密密文:");
encode(xstr,str,key); printf("%s\n",xstr);
if(decode(str,xstr,key)!=NULL)
{printf("%s\n",str);}
else {printf("NULL");}
}