算法题 Enigma

Enigma from ZOJ(浙大OJ) Problem Set - 1009,链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1009

题目如下:

In World War II, Germany once used an electronic encryption machine called Enigma, which played a decisive role in the initial victories of Nazi Germany. It was proved to be one of the most reliable encryption systems in history. However, it was the blind trust on the reliability of the machine that brought about the doom of its user.

The structure of a one-rotor Enigma is shown as follows (the Enigma has only six keys):

The key element of the Enigma is the rotor, as shown in the second figure, which uses electronic circuits to transform plaintext (input from keyboard) into cryptograph (output on screen). When one key on the keyboard is pressed, the corresponding cryptograph is shown on screen. Then the rotor will automatically revolve a one-letter-step to a different position. The following figures illustrate how the rotor works when letter "b" is pressed three successively times:
When letter "b" is pressed for the first time, the signal goes through the circuit and "A" is shown on screen. When the key is released, the rotor revolves one-letter-step to a different position that changes all the corresponding circuits so that each letter now has a different cryptograph. When letter "b" is pressed for the second time, the corresponding cryptograph is "C". So when letter "b" is pressed for the third time, the cryptograph is "E" according to the principle specified above. Now the following figure shows the structure of a two-rotor Enigma.
The difference is that when a key is released, the second rotor won't revolve a step until the first one has finished one circle and returns to the original position. This is also the same in the case of three-rotor Enigma. That is: Only after the first rotor has finished one circle and return to the initial status, the second rotor will revolve a step. And only after the second rotor has finish one circle, the third rotor will revolve a step. However, how did the Allied Forces obtain the information encrypted by Enigma? A person named Hans-Thilo Schimdt was very essential. He acted as a spy and provided the initial status of the three rotors in each Enigma to the Allied Forces once a month. The Allied Forces thus got everything they wanted by deciphering the intercepted cryptograph using the information offered by the spy. Now, please design a program to obtain the plaintexts using the information offered by the Allied Forces. Input The input file contains several test cases representing several three-rotor Enigmas. The last test case in the input file is followed by a line containing a number 0. Each case begins with a line containing an integer m (1 <= m <= 26) which indicates the number of sequential letters each rotor has. The first letter will always be A. (for example, m = 6 tells each rotor has 6 keys from A to F). The following three lines describe the initial status of the three rotors respectively. Each of them contains a string consisting of m capital character. For instance, a rotor with the initial status "BADFEC" indicates that the initial encrypt mechanism is to convert "abcdef" to "BADFEC", that is, original letter "a" corresponding to cryptograph letter "B", "b" to "A", "c" to "D", "d" to "F", "e" to "E" and "f" to "C". The forth line of each case contains an integer n which tells the number of cryptographs generated by the above Enigma. Then the following n lines are the n cryptographs respectively, which consist of m capital characters each. Output For each test case, the output should consist of two parts. The first line is the number of Enigma and a colon. The following lines are the plaintexts deciphered from the corresponding cryptographs. Each plaintext should be printed in one line. Note: The characters in the plaintext should be converted to the corresponding lowercases before they are printed. Insert a blank line between test cases. Sample Input 6 BADFEC ABCDEF ABCDEF 1 ACE 0 Output for the Sample Input Enigma 1: bbb

题目简述:

给出恩尼格码的的三个轮转子的初始状态和加密后的密文,求明文。

思路

这就是一个简单的编程题,并不涉及太多的算法知识,主要考验编程人员的编程功底,我的思路是基于cipher = rotor.mapping[content - offset] + content,用这个加密公式就行了,由于该加密方法是一一对应的,甚至连逆公式都不用推导,可以直接进行爆破。

部分代码功能介绍:

generate_rotor

用输入的映射规则计算轮转子的初始形态。

void generate_rotor(int m, char buf[3][MAX + 1], rotor *r)
{
    int i, ii;
    for (i = 0; i < 3; i++)
    {
        for (ii = 0; ii < m; ii++)
        {
            r[i].mapping[ii] = (buf[i][ii] - ('A' + ii) + m) % m;
        }
    }
}

traverse_content

核心代码,对所有可能的情况进行尝试,最后得出原来的明文。

int traverse_content(int m, rotor *r, int offset, int cipher)
{
    int i;
    for (i = 0; i < m; i++)
    {
        if (((r->mapping[(i - offset + m) % m] + i + m) % m) == cipher)
        {
            return i;
        }
    }
    fprintf(stderr, "Error\n");
    exit(-1);
    return -1;
}

完整代码

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

#define MAX 26

typedef struct rotor
{
    int mapping[MAX];
} rotor;

void generate_rotor(int m, char buf[3][MAX + 1], rotor *r)
{
    int i, ii;
    for (i = 0; i < 3; i++)
    {
        for (ii = 0; ii < m; ii++)
        {
            r[i].mapping[ii] = (buf[i][ii] - ('A' + ii) + m) % m;
        }
    }
}

// encode   cipher = rotor.mapping[content - offset] + content

int traverse_content(int m, rotor *r, int offset, int cipher)
{
    int i;
    for (i = 0; i < m; i++)
    {
        if (((r->mapping[(i - offset + m) % m] + i + m) % m) == cipher)
        {
            return i;
        }
    }
    fprintf(stderr, "Error\n");
    exit(-1);
    return -1;
}

void decode(int m, rotor *r, char *in, int in_length, char *out)
{
    int arr[3], i;
    int temp;
    temp = in_length;

    arr[0] = temp % m;
    temp = temp / m;
    arr[1] = temp % m;
    temp = temp / m;
    arr[2] = temp % m;

    for (i = in_length - 1; i >= 0; i--)
    {
        arr[0]--;
        if (arr[0] < 0)
        {
            arr[1]--;
            arr[0] = m - 1;
        }
        if (arr[1] < 0)
        {
            arr[2]--;
            arr[1] = m - 1;
        }
        temp = traverse_content(m, &r[2], arr[2], (in[i] - 'A'));
        temp = traverse_content(m, &r[1], arr[1], (temp));
        temp = traverse_content(m, &r[0], arr[0], (temp));
        out[i] = temp + 'a';
    }
    out[in_length] = '\0';
}

int main()
{
    int m, n, i, amount;
    rotor rotor_array[3];
    char rotor_buf[3][MAX + 1], in_buf[0x1000], out_buf[0x1000];
    amount = 0;
    while (scanf("%d", &m) != EOF && m != 0)
    {
        amount++;
        // 每个test case之间插入一个空行
        if (amount != 1)
        {
            puts("");
        }

        scanf("%s %s %s", rotor_buf[0], rotor_buf[1], rotor_buf[2]);

        generate_rotor(m, rotor_buf, rotor_array);

        printf("Enigma %d:\n", amount);

        scanf("%d", &n);
        for (i = 0; i < n; i++)
        {
            scanf("%s", in_buf);
            decode(m, rotor_array, in_buf, strlen(in_buf), out_buf);
            printf("%s\n", out_buf);
        }
    }

    return 0;
}