铁栅栏密码算法c

时间:2014-03-22 05:28:30

标签: c algorithm memory-management cryptography

我正在c中编写一个铁栅栏密码算法,以获得乐趣并提高我的C编程技巧。我觉得它适用于小的输入短语,但当输入短语很大时,它会因某些原因而出现乱码。

这里是代码:(抱歉,我无法将其缩减为SSCCE,我不知道算法的哪个部分导致了问题)

#include<stdio.h>
#include<string.h>
#include <stdlib.h>

/* function to append a char to a char array */
void append(char* s, char c)
{
   int len = strlen(s);
   s[len] = c;
   s[len+1] = '\0';
}

int main(void)
{
   int num_rails;

   for (num_rails = 2; num_rails < 6; num_rails++)
   {

   char* message = "therailfencecipheristrickyespeciallywhentheinputisverylongabcdefghijklmnopqrstuvwxyzblerpblorp";

   int word_len = strlen(message);

   char* lines[num_rails];
   char* rails[num_rails];

   int len_rails[num_rails];
   memset(len_rails, 0, num_rails*sizeof(int));

   int i,j,k,mod;
   int repeats;
   int period = (2*num_rails) - 2;

   printf("%d characters, %d rails:\n", word_len, num_rails);

   printf("\nplaintext: %s\n", message);

   /* encryption */

   for (i = 0; i < num_rails; i++)
   {
      if ((lines[i] = malloc(sizeof(char))) == NULL)
      {
         printf("\nUnable to allocate memory.\n");
         return EXIT_FAILURE;
      }
   }

   for (repeats = 0; repeats < ((word_len/period)+1); repeats++)
   {
      if (repeats*period < word_len)
         append(lines[0], message[repeats*period]);

      for (j = 1; j < period/2; j++)
      {
         if ((j + (repeats*period)) < word_len)
            append(lines[j], message[j + (repeats*period)]);

         if ((((repeats+1)*period) - j) < word_len)
            append(lines[j], message[((repeats+1)*period) - j]);
      }

      if (((period/2) + (repeats*period)) < word_len)
         append(lines[j], message[(period/2)+(repeats*period)]);
   }

   char encrypted[word_len];
   strcpy(encrypted,lines[0]);

   for (i = 1; i < num_rails; i++)
      strcat(encrypted, lines[i]);

   printf("\nciphertext: %s\n", encrypted);

   /* decryption */

   for (i = 0; i < num_rails; i++)
   {
      if ((rails[i] = malloc(sizeof(int) * 40)) == NULL)
      {
         printf("\nUnable to allocate memory.\n");
         return EXIT_FAILURE;
      }
   }

   mod = word_len % period;

   len_rails[0] = word_len / period;
   len_rails[num_rails-1] = len_rails[0];

   for (i = 1; i < num_rails - 1; i++)
      len_rails[i] = len_rails[0] * 2;

   for (i = 0; i < mod && i < num_rails; i++)
   {
      len_rails[i]++;
   }

   for (j = i-2; i < mod && j > -1; j--)
   {
      len_rails[j]++;
      i++;
   }

   printf("\nrail lengths:");
   for (i = 0; i < num_rails; i++)
      printf(" %d", len_rails[i]);

   putchar('\n');
   k = 0;

   for (i = 0; i < num_rails; i++)
   {
      for (j = 0; j < len_rails[i]; j++)
      {
         append(rails[i], encrypted[k++]);
      }
   }

   char deciphered[word_len];
   strcpy(deciphered, "");

   for (i = 0; i < ((word_len/period)+1); i++)
   {
      if (rails[0][i])
         append(deciphered, rails[0][i]);

      for (j = 1; j < num_rails-1; j++)
      {
         if (rails[j][i*2])
            append(deciphered, rails[j][i*2]);
      }

      if (rails[num_rails-1][i])
         append(deciphered, rails[num_rails-1][i]);

      for (j = num_rails-2; j > 0; j--)
      {
         if (rails[j][(i*2)+1])
            append(deciphered, rails[j][(i*2)+1]);
      }
   }

   printf("\ndeciphered: %s\n", deciphered);

   printf("==========================================\n");
   }
}

它应该编译并运行正常,以便您可以测试它。

它应该打印纯文本,然后加密并打印,然后将加密的文本解密回纯文本并打印2,3,4,5轨,但它应该适用于任意数量的轨道。

问题是如果输入变量“message”对于不同数量的轨道超过一定大小,则输出会出现乱码。

例如。

2个导轨变为63个字符的乱码

3个导轨变为64个字符的乱码

4个导轨变为95个字符的乱码

5个轨道变为乱码,为126个字符

最接近我能够解决的问题是,只要len_rails []的任何值超过31,输出就会因轨道数量而出现乱码..

有没有人知道为什么会这样?它与我如何分配内存有关吗?它已经有一段时间了,因为我做了任何C编程,我的内存处理有点生锈。

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:1)

在这一行:

if ((lines[i] = malloc(sizeof(char))) == NULL)

您只为单个字符分配内存,但随后尝试使用该缓冲区存储多个char数据。将sizeof(char)(顺便说一下,总是1)乘以您计划存储在数组中的字符数。

请记住在结束前释放()内存。