多栏栅栏密码 算法
#include <stdio.h>#include <stdlib.h>
#include <string.h>
char * encrypt(int n, const char * str);
void dencrypt();
char * dencrypt2(int n, const char * str);
int main(void){
char * str3 = "Lj`lmda`p|iscvfkgdq$";
//char * str3 = "Qoeqrifeunxh0kpliv";
for(int i =1;i<=20;i++){
char * str4 = dencrypt2(i,str3);
for(int j = 0;j<strlen(str4);j++)
{
putchar(str4-5);
}
printf("\n");
}
return 0;
}
char * encrypt(int n, const char * str)
{
// 验证是否能加密
int len = strlen(str);
if(n<=0) return NULL;
int c; // 每个栏 字符的数量,有时候len不一定能被n整除
c = (len % n) == 0 ? len /n : len / n + 1;
int index;
char * t =(char *)calloc(len+1,sizeof(char));
int ti = 0;
for(int i = 0; i < c; i++)
{
for(int j = 0; j < n; j++)
{
index = i + j * c;
if(index > len -1)
break;
t =str;
}
}
t='\x00';
return t;
}
char * dencrypt2(int n, const char * str)
{
// 验证是否能加密
int len = strlen(str);
if(n<=0) return NULL;
int index;
// 将字符创分成c段,每段n个字符
int c;
c = (len % n) == 0 ? len /n : len / n + 1;
char * temp = (char *)calloc(len+1,sizeof(char));
int ti = 0;
for(int i =0; i < n; i++)
{
for(int j = 0; j < c; j++)
{
index = i + j * n;
if(index > len -1) break;
temp = str;
}
}
temp='\x00';
return temp;
} 看看学习学习
页:
[1]