C编程 - 任何使程序更短的方法

时间:2013-04-07 23:54:51

标签: c

我的程序必须解密密文,尝试所有可能的转换 这是在函数中完成的。如果您有任何想法可以缩短它,将不胜感激。

char decryptCiphertext(char data[], double data2[])
{
   int i, convert, shiftingLetter;
   char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42], 
   decrypted5[42], decrypted6[42], decrypted7[42], decrypted8[42], decrypted9[42], decrypted10[42], 
   decrypted11[42], decrypted12[42], decrypted13[42], decrypted14[42], decrypted15[42], decrypted16[42], 
   decrypted17[42], decrypted18[42], decrypted19[42], decrypted20[42], decrypted21[42], decrypted22[42], 
   decrypted23[42], decrypted24[42], decrypted25[42];

   //Try all possible shifts from 1-25 and store each in an array
   for (i = 0; data[i] != '\0'; i++) 
   {
      convert = data[i] - 'A';  //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
      convert = (convert + 1);    //add shift
      shiftingLetter = convert % 26;    //cycle around
      decrypted1[i] = 'A' + shiftingLetter;  //store new letter in array
   }
   decrypted1[i] = '\0';
   likelyPlaintext(decrypted1, data2);
   //printf(" 1. %s\n", decrypted1);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 2);
      shiftingLetter = convert % 26;
      decrypted2[i] = 'A' + shiftingLetter;
   }
   decrypted2[i] = '\0';
   //printf(" 2. %s\n", decrypted2);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 3);
      shiftingLetter = convert % 26;
      decrypted3[i] = 'A' + shiftingLetter;
   }
   decrypted3[i] = '\0';
   //printf(" 3. %s\n", decrypted3);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 4);
      shiftingLetter = convert % 26;
      decrypted4[i] = 'A' + shiftingLetter;
   }
   decrypted4[i] = '\0';
   //printf(" 4. %s\n", decrypted4);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 5);
      shiftingLetter = convert % 26;
      decrypted5[i] = 'A' + shiftingLetter;
   }
   decrypted5[i] = '\0';
   //printf(" 5. %s\n", decrypted5);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 6);
      shiftingLetter = convert % 26;
      decrypted6[i] = 'A' + shiftingLetter;
   }
   decrypted6[i] = '\0';
   //printf(" 6. %s\n", decrypted6);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 7);
      shiftingLetter = convert % 26;
      decrypted7[i] = 'A' + shiftingLetter;
   }
   decrypted7[i] = '\0';
   //printf(" 7. %s\n", decrypted7);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 8);
      shiftingLetter = convert % 26;
      decrypted8[i] = 'A' + shiftingLetter;
   }
   decrypted8[i] = '\0';
   //printf(" 8. %s\n", decrypted8);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 9);
      shiftingLetter = convert % 26;
      decrypted9[i] = 'A' + shiftingLetter;
   }
   decrypted9[i] = '\0';
   //printf(" 9. %s\n", decrypted9);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 10);
      shiftingLetter = convert % 26;
      decrypted10[i] = 'A' + shiftingLetter;
   }
   decrypted10[i] = '\0';
   //printf(" 10. %s\n", decrypted10);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 11);
      shiftingLetter = convert % 26;
      decrypted11[i] = 'A' + shiftingLetter;
   }
   decrypted11[i] = '\0';
   //printf(" 11. %s\n", decrypted11);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 12);
      shiftingLetter = convert % 26;
      decrypted12[i] = 'A' + shiftingLetter;
   }
   decrypted12[i] = '\0';
   //printf(" 12. %s\n", decrypted12);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 13);
      shiftingLetter = convert % 26;
      decrypted13[i] = 'A' + shiftingLetter;
   }
   decrypted13[i] = '\0';
   //printf(" 13. %s\n", decrypted13);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 14);
      shiftingLetter = convert % 26;
      decrypted14[i] = 'A' + shiftingLetter;
   }
   decrypted14[i] = '\0';
   //printf(" 14. %s\n", decrypted14);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 15);
      shiftingLetter = convert % 26;
      decrypted15[i] = 'A' + shiftingLetter;
   }
   decrypted15[i] = '\0';
   //printf(" 15. %s\n", decrypted15);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 16);
      shiftingLetter = convert % 26;
      decrypted16[i] = 'A' + shiftingLetter;
   }
   decrypted16[i] = '\0';
   //printf(" 16. %s\n", decrypted16);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 17);
      shiftingLetter = convert % 26;
      decrypted17[i] = 'A' + shiftingLetter;
   }
   decrypted17[i] = '\0';
   //printf(" 17. %s\n", decrypted17);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 18);
      shiftingLetter = convert % 26;
      decrypted18[i] = 'A' + shiftingLetter;
   }
   decrypted18[i] = '\0';
   //printf(" 18. %s\n", decrypted18);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 19);
      shiftingLetter = convert % 26;
      decrypted19[i] = 'A' + shiftingLetter;
   }
   decrypted19[i] = '\0';
   //printf(" 19. %s\n", decrypted19);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 20);
      shiftingLetter = convert % 26;
      decrypted20[i] = 'A' + shiftingLetter;
   }
   decrypted20[i] = '\0';
   //printf(" 20. %s\n", decrypted20);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 21);
      shiftingLetter = convert % 26;
      decrypted21[i] = 'A' + shiftingLetter;
   }
   decrypted21[i] = '\0';
   //printf(" 21. %s\n", decrypted21);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 22);
      shiftingLetter = convert % 26;
      decrypted22[i] = 'A' + shiftingLetter;
   }
   decrypted22[i] = '\0';
   //printf(" 22. %s\n", decrypted22);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 23);
      shiftingLetter = convert % 26;
      decrypted23[i] = 'A' + shiftingLetter;
   }
   decrypted23[i] = '\0';
   //printf(" 23. %s\n", decrypted23);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 24);
      shiftingLetter = convert % 26;
      decrypted24[i] = 'A' + shiftingLetter;
   }
   decrypted24[i] = '\0';
   //printf(" 24. %s\n", decrypted24);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 25);
      shiftingLetter = convert % 26;
      decrypted25[i] = 'A' + shiftingLetter;
   }
   decrypted25[i] = '\0';
   //printf(" 25. %s\n", decrypted25);

} 

我的程序必须解密密文,尝试所有可能的转换 这是在函数中完成的。如果您有任何想法可以缩短它,将不胜感激。

2 个答案:

答案 0 :(得分:1)

首先,每个for循环都可以是一个将变量传递给的函数。

记住干。不要重复自己。

我不知道C但是下面应该是一个函数,你传入decrypted4和26并返回结果。

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 4);
      shiftingLetter = convert % 26;
      decrypted4[i] = 'A' + shiftingLetter;
   }

答案 1 :(得分:1)

这是:

   int i, j, convert, shiftingLetter;
   char decryptedArr[25][42];

   //Try all possible shifts from 1-25 and store each in an array
   for (j = 0; j < 25; j++)
   for (i = 0; data[i] != '\0'; i++) 
   {
      convert = data[i] - 'A';  //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
      convert = (convert + i + 1);    //add shift
      shiftingLetter = convert % 26;    //cycle around
      decryptedArr[j][i] = 'A' + shiftingLetter;  //store new letter in array
   }
   decryptedArr[j][i] = '\0';