-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstecker.c
92 lines (77 loc) · 1.57 KB
/
stecker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#include "key.h"
#include "stecker.h"
/* swaps letters */
void swap(int stbrett[], int i, int k)
{
int store;
store = stbrett[i];
stbrett[i] = stbrett[k];
stbrett[k] = store;
}
/* extracts stecker from key->stbrett to key->sf */
void get_stecker(Key *key)
{
int i, k = 25;
key->count = 0;
for (i = 0; i < 26; i++) {
if (key->stbrett[i] > i) {
key->sf[key->count++] = i;
key->sf[key->count++] = key->stbrett[i];
}
else if (key->stbrett[i] == i) {
key->sf[k--] = i;
}
}
}
/* get new order for testing stecker */
void rand_var(int var[])
{
int count;
int store;
int i;
for (count = 25; count > 0; count--) {
#ifndef WINDOWS
i = random() % (count+1);
#endif
#ifdef WINDOWS
i = rand() % (count+1);
#endif
store = var[count];
var[count] = var[i];
var[i] = store;
}
}
/* arrange var[] in order of frequency of letters in ciphertext */
void set_to_ct_freq(int var[], const int *ciphertext, int len)
{
int f[26] = {0};
int i, k, c;
int max, pos = -1;
int n = 0;
for (i = 0; i < len; i++) {
c = ciphertext[i];
f[c]++;
}
for (i = 0; i < 26; i++) {
max = 0;
for (k = 0; k < 26; k++)
if (f[k] >= max) {
max = f[k];
pos = k;
}
if (pos == -1)
return;
f[pos] = -1;
var[n++] = pos;
}
}
/*
* This file is part of enigma-suite-0.76, which is distributed under the terms
* of the General Public License (GPL), version 2. See doc/COPYING for details.
*
* Copyright (C) 2005 Stefan Krah
*
*/