【发布时间】:2018-05-23 13:04:56
【问题描述】:
我需要做一个解码转置密码的程序。一开始我有一个词,它是加密文本。密钥的长度等于接收到的字的 -1 长度。密钥是随机的。
例子:
-
input text:Uniz isvrh tiesiime ffrmfbi jz tpywjhpf gx ucs zgvey eoj e igpg himua xgxfx qbxo xgw b xihapbx vftx jt xik jpxq pl eo owpygfrit zvjgrhri input word: mark output:They could scarcely believe it possible at two yards and a half below water mark was a regular rent in the form of an isosceles triangle -
input text:Qr oc cvtmxen ev Rga Asto vlg uwiuxksp acw cx kxu lgmilv input word:was output:On my arrival at New York the question was at its height -
input text: Rt kolniz qufkbnx R gjvoczkm zqk kgobzkwin ul cnn suwyckx input word: effect output:In effect however I admitted the existence of the monster
对于示例 1 和 2,程序编译得非常快,但对于示例 3 程序编译了很长时间,因为我有太多的 for 循环。
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
unsigned int i = 0;
int key[10];
unsigned int k = 0;
int check;
unsigned int j, jj, jjj, jjjj, jjjjj, jjjjjj;
int n = 50;
char slowo[10];
char plain[500], cipher[500];
fflush(stdin);
printf("Enter text:");
fgets(plain, 500, stdin);
printf("Enter word:");
fgets(slowo, 500, stdin);
if (strlen(slowo) == 4)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && isspace(cipher[i + 4]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
if (strlen(slowo) == 5)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && isspace(cipher[i + 5]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
if (strlen(slowo) == 6)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && isspace(cipher[i + 6]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
if (strlen(slowo) == 7)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && isspace(cipher[i + 7]))
{
jj--;
}
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (jjjjj = n; jjjjj != 0; jjjjj--)
{
key[4] = (jjjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && cipher[i + 6] == slowo[5] && isspace(cipher[i + 7]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
}
if (strlen(slowo) == 8)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (jjjjj = n; jjjjj != 0; jjjjj--)
{
key[4] = (jjjjj - 25);
for (jjjjjj = 0; jjjjjj != 0; jjjjjj--)
{
key[5] = (jjjjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && cipher[i + 6] == slowo[5] && cipher[i + 7] == slowo[6] && isspace(cipher[i + 7]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
}
}
else {
printf("Error");
return 1;
}
}
我想优化代码以使其更快,但我不知道。
【问题讨论】:
-
您使用了过于残酷的蛮力算法。您可能想查看 Babbage-Kasiski 解密方法。您最终会得到几个候选键,但很有可能只有一个会导致您的已知单词(所谓的 crib)出现在解密的文本中。您可能还想查询 Security Stack Exchange 站点。
-
j、jj、jjj、jjjj、jjjjj、jjjjjj是糟糕的变量名。fflush(stdin)具有未定义的行为(fflush是写操作,stdin是输入流)。 -
这属于codereview。
标签: c algorithm optimization