我需要做一个解码Transposition密码的程序。一开始我有一个单词,它是加密文本。密钥的长度等于接收字的-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;
}
}
我想优化代码以使其更快,但我不知道。
答案 0 :(得分:0)
在您的代码中有一些可能的优化 - 所谓的 keyhole优化 - 但我宁愿更改您正在使用的算法。锁孔优化是明智的,而且是愚蠢的,premature optimization is the root of all evil。 降低算法复杂度几乎总是您的首要任务。然后,只有这样你才能进行进一步的优化。
在这里你知道密钥长度(在第三个例子中)是五,因为 crib effect
是六个字符;我们将密钥称为12345。
我们将密钥与密文对齐,因为忽略了空格:
12 345123 4512345 1 23451234 512 345123451 23 451 2345123
Rt kolniz qufkbnx R gjvoczkm zqk kgobzkwin ul cnn suwyckx
我们现在可以使用线性方程系统在O(n3)中实现强制Babbage解密:
x1 - 1 = R
x2 - 2 = t
x3 - 3 = k
x4 - 4 = o
x5 - 5 = l
x6 - 1 = n
...
x48 - 3 = x
但我们宁愿观察我们只有一个单词,其长度等于'效果' - 它是'kolniz'。 这意味着我们可以立即恢复密钥:这是差异“nikol - efeec”。第一个符号1等于从效果的第二个e
向 kolniz 的n
前移,即9个位置。因此,当我们找到一个映射到 worm 的1个符号的字母时,例如第一个'R',我们从R返回9个位置,我们找到'I'。
上述算法大致为O(n + m),其中n为密文的长度,m为密钥的长度。
假设我们有两个长度等于“效果”的单词,比如说它们是“abcdef”和“ghijkl”。我们使用哪一个?
假设1:abcdef是效应的密文。如果这是真的那么'e'的第一个符号的移位是'a'而相同的移位必须'f'变成't'。
假设2:ghikl是效应的密文。如果这是真的那么'e'的第一个符号的移位是'g'而相同的移位必须'l'变成't'。
两个假设都是正确的,我们不能正确的是另一个单词在第一个和最后一个字母中具有相同的移位差异的那些是'e'和't '在我们的婴儿床中,例如第一个词是'效果',另一个是“美洲狮”,“挽歌”,“哈洛”或“老佛爷”。那些机会很少。