这个太简单了,没什么好说的
#include <stdio.h>
#include <string.h>
#define STRING_LEN_MAX 71
char g_ciphertext[STRING_LEN_MAX];
char g_plaintext[STRING_LEN_MAX];
char g_Code2CharTable[] = {'_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.'};
// c[i] = (p[ki % n] - i) % 28;
// =>
// ki % n = j => i = (j + xn)/k,
// (j + xn)%k == 0
// 0<(j + xn)/k<n
// =>
// p[j] = c[(j + xn)/k] + 28*y + (j + xn)/k
// 0<c[(j + xn)/k] + 28*y + (j + xn)/k<28
void TwistChar2Code(char* str, int iStringLen)
{
for (int i = 0; i < iStringLen; i++)
{
switch (str[i])
{
case '_':
str[i] = 0;
break;
case 'a':
str[i] = 1;
break;
case 'b':
str[i] = 2;
break;
case 'c':
str[i] = 3;
break;
case 'd':
str[i] = 4;
break;
case 'e':
str[i] = 5;
break;
case 'f':
str[i] = 6;
break;
case 'g':
str[i] = 7;
break;
case 'h':
str[i] = 8;
break;
case 'i':
str[i] = 9;
break;
case 'j':
str[i] = 10;
break;
case 'k':
str[i] = 11;
break;
case 'l':
str[i] = 12;
break;
case 'm':
str[i] = 13;
break;
case 'n':
str[i] = 14;
break;
case 'o':
str[i] = 15;
break;
case 'p':
str[i] = 16;
break;
case 'q':
str[i] = 17;
break;
case 'r':
str[i] = 18;
break;
case 's':
str[i] = 19;
break;
case 't':
str[i] = 20;
break;
case 'u':
str[i] = 21;
break;
case 'v':
str[i] = 22;
break;
case 'w':
str[i] = 23;
break;
case 'x':
str[i] = 24;
break;
case 'y':
str[i] = 25;
break;
case 'z':
str[i] = 26;
break;
case '.':
str[i] = 27;
break;
}
}
}
void TwistCode2Char(char* str, int iStringLen)
{
for (int i = 0; i < iStringLen; i++)
{
str[i] = g_Code2CharTable[str[i]];
}
}
void TwistingDecription(char* strCiphertext, int iStringLen, char* strPlaintext, int iKey)
{
TwistChar2Code(strCiphertext, iStringLen);
for (int i = 0; i < iStringLen; i++)
{
// 遍历有效x
int y = 0;
int x = 0;
for (x = (-i/iStringLen); x<(iKey-i/iStringLen); x++ )
{
if((i + x*iStringLen)%iKey != 0)
{
continue;
}
// 寻找有效y
y = (-g_ciphertext[(i+x*iStringLen)/iKey]-(i+x*iStringLen)/iKey)/28;
break;
}
strPlaintext[i] = strCiphertext[(i + x*iStringLen)/iKey] + 28*y + (i + x*iStringLen)/iKey;
}
TwistCode2Char(strPlaintext, iStringLen);
strPlaintext[iStringLen] = 0;
}
int main()
{
int iKey;
while (scanf("%d %s", &iKey, g_ciphertext) != EOF && iKey != 0)
{
TwistingDecription(g_ciphertext, strlen(g_ciphertext), g_plaintext, iKey);
printf("%s\n", g_plaintext);
}
return 0;
}