人気ブログランキング | 話題のタグを見る

新規ドキュメント


忘れていく脳のための備忘録
by w_l_s

[作って遊ぼう] エニグマ暗号を作ってみた

2次大戦中にドイツ軍が使ったという、エニグマ暗号と同じ方法で暗号化するものをC言語で書いてみた。エニグマ暗号についてはここが詳しい。
とある演習の課題用に書いてみたけど、お蔵入りしそうなので載せてみる。採用されるとうれしいな。

…こんな課題、出されるほうはたまったもんじゃないと思うけど。

CentOS5.2 + gcc4.3.2でコンパイルの確認はとってますが、コピペ中になにか起きてるかもしれないので、まんまコピペでほかでも動くか心配。

まだまだ不完全な部分もあるけれど、ちゃんと暗号化、復号化を行える。
実行例 --
 暗号化する
./a.out
I am a cat. // 平文(元の文章)入力
22 // 暗号化に使う3つの数字を入力
14
32
I et q pbx. // 暗号化された文

 元の文章に戻す
./a.out
I et q pbx. // 暗号化された文を入力
22 // 同じ順番で、同じ数字を入力
14
32
I am a cat. // 元に戻る
----------

大文字や記号、数字まで対応させれば、人じゃ解読できなくなりそう。

<---------------------------- ここから ---------------------------->

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

#define ALPHABET 26
#define CORRECT 0
#define ERROR 1
#define UNCHECK CORRECT
#define CHECKED ERROR
#define REF_OFF CORRECT
#define REF_ON ERROR

void rand_alphabet(char alphabetTable[ALPHABET], int seed);
void mirror_alphabet(char alphabetTable[ALPHABET],
char mirrorTable[ALPHABET]);

char reflector(char input);
char rotor(char input, int seed, int reflect_flag);

void encryption();
char convert(char input, int Fseed, int Sseed, int Mseed);
void seedcontrol(int *Fseed, int *Mseed, int *Sseed);

int main()
{
encryption();
return 0;
}

// 変換用アルファベット
void rand_alphabet(char alphabetTable[ALPHABET], int seed)
{
int code_num;
int count = 0;
char checkTable[ALPHABET] = "";

srand(seed);

while(count < ALPHABET)
{
code_num = rand() % 123;
if(code_num >= 97 && checkTable[code_num - 97] == UNCHECK)
{
alphabetTable[count] = code_num;
checkTable[code_num - 97] = CHECKED;
count++;
}
}
}

// リフレクタ通過後の変換用アルファベット
void mirror_alphabet(char alphabetTable[ALPHABET],
char mirrorTable[ALPHABET])
{
int idx = 0;

for(idx = 0; idx < ALPHABET; idx++)
{
mirrorTable[alphabetTable[idx] - 97] = 97 + idx;
}
}

// リフレクタ部分
char reflector(char input)
{
char output = ' ';

if(input == 'a'){
output = 'm';
} else if(input == 'b') {
output = 'e';
} else if(input == 'c') {
output = 'j';
} else if(input == 'd') {
output = 'u';
} else if(input == 'e') {
output = 'b';
} else if(input == 'f') {
output = 'z';
} else if(input == 'g') {
output = 'q';
} else if(input == 'h') {
output = 'k';
} else if(input == 'i') {
output = 't';
} else if(input == 'j') {
output = 'c';
} else if(input == 'k') {
output = 'h';
} else if(input == 'l') {
output = 'o';
} else if(input == 'm') {
output = 'a';
} else if(input == 'n') {
output = 'y';
} else if(input == 'o') {
output = 'l';
} else if(input == 'p') {
output = 'w';
} else if(input == 'q') {
output = 'g';
} else if(input == 'r') {
output = 'v';
} else if(input == 's') {
output = 'x';
} else if(input == 't') {
output = 'i';
} else if(input == 'u') {
output = 'd';
} else if(input == 'v') {
output = 'r';
} else if(input == 'w') {
output = 'p';
} else if(input == 'x') {
output = 's';
} else if(input == 'y') {
output = 'n';
} else if(input == 'z') {
output = 'f';
} else {
printf("input %d\n", input);
}

return output;
}

// ロータ
char rotor(char input, int seed, int reflect_flag)
{
char alphabetTable[ALPHABET] = "";
char mirrorTable[ALPHABET] = "";
char output;

rand_alphabet(alphabetTable, seed);
if(reflect_flag)
{
mirror_alphabet(alphabetTable, mirrorTable);
output = mirrorTable[input -97];
} else {
output = alphabetTable[input - 97];
}

return output;
}

// 暗号部
void encryption()
{
char input[100] = "";
char output[100] = "";
char tmp;
int strlength = 0;
int count = 0;
int Fseed, Sseed, Mseed;

Fseed = Sseed = Mseed = 0;
fgets(input, sizeof(input), stdin); // 平文入力
scanf("%d %d %d", &Fseed, &Mseed, &Sseed); // ロータ番号入力
Fseed = Fseed % 26;
Mseed = Mseed % 26 + 26;
Sseed = Sseed % 26 + (26 * 2);
strlength = strlen(input); // 平文の長さを調べる

for(count = 0; count < strlength; count++)
{
tmp = input[count];
if(tmp >= 97 && tmp <= 122) {
tmp = convert(tmp, Fseed, Mseed, Sseed);
seedcontrol(&Fseed, &Mseed, &Sseed);
}
output[count] = tmp;
}

printf("%s\n", output);
}

void seedcontrol(int *Fseed, int *Mseed, int *Sseed)
{
*Fseed += 1;
if(*Fseed > 26)
{
*Fseed = 0;
*Mseed += 1;
}
if(*Mseed > 52)
{
*Mseed = 26;
*Sseed += 1;
}
if(*Sseed > 78)
{
*Fseed = 0;
*Mseed = 26;
*Sseed = 52;
}
}

char convert(char input, int Fseed, int Mseed, int Sseed)
{
int reflect_flag = REF_OFF;
char tmp;
char output;

tmp = rotor(input, Fseed, reflect_flag);
tmp = rotor(tmp, Mseed, reflect_flag);
tmp = rotor(tmp, Sseed, reflect_flag);
tmp = reflector(tmp);
reflect_flag = REF_ON;
tmp = rotor(tmp, Sseed, reflect_flag);
tmp = rotor(tmp, Mseed, reflect_flag);
output = rotor(tmp, Fseed, reflect_flag);

return output;
}

<---------------------------- ここまで ---------------------------->

それににしても長い。
ブログにそのまま貼り付けるのは間違いだったかも。
全体をいくつかの関数の集まりに分割してメモを書いておくことにしよう。

TODO:
コマンドライン引数を使ってみる。
文字列や数字の入力に何も説明がないなんて、無愛想にも程がある。
数字の入力チェックを行う。ここに文字列を入力されると困る。
アルファベットのテーブルなんて持ちたくないなぁ。

######
081105 loter -> rotorに修正 恥ずかしい…
by w_l_s | 2008-11-05 00:18 | 作って遊ぼう