|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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[j]-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[ti++] =str[index];
}
}
t[len]='\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[ti++] = str[index];
}
}
temp[len]='\x00';
return temp;
} |
|