Skip to content

Commit c6e6de4

Browse files
authored
Prepare for addition of cipher types outside of current cipher groups
This commit future proofs the code for the addition of new cipher types outside of the currently supported cipher groups (e.g. 'aca' and 'rotor'). It therefore: - Improves detection of rotor ciphers in provided `--ciphers` argument - Ensures `CipherStatisticsDataset` works with missing cipher types - Future proofs selection of cipher group 'all' in `expand_cipher_groups` (Squashed merge of PR #14.)
1 parent d861bbe commit c6e6de4

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

cipherTypeDetection/cipherStatisticsDataset.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def _rearrange_ciphertexts(self, ciphertext_label_pairs):
362362
@property
363363
def is_initialized(self):
364364
"""Indicates whether the dataset was initialized with input data."""
365-
return self._number_of_cipher_classes != 0
365+
return self._number_of_cipher_classes != 0 and self._batch_size > 0
366366

367367
def _are_files_closed(self):
368368
"""Returns True if at least one ciphertext file is closed."""
@@ -452,7 +452,7 @@ def __init__(self, plaintext_paths, dataset_params, logger):
452452

453453
key_lengths_count = 0
454454
for cipher_t in self._cipher_types:
455-
index = self._cipher_types.index(cipher_t)
455+
index = config.CIPHER_TYPES.index(cipher_t)
456456
if isinstance(config.KEY_LENGTHS[index], list):
457457
key_lengths_count += len(config.KEY_LENGTHS[index])
458458
else:
@@ -472,7 +472,7 @@ def key_lengths_count(self):
472472
@property
473473
def is_initialized(self):
474474
"""Indicates whether the dataset was initialized with input data."""
475-
return len(self._plaintext_paths) != 0 and len(self._cipher_types) != 0
475+
return len(self._plaintext_paths) != 0 and len(self._cipher_types) != 0 and self._batch_size > 0
476476

477477
def __iter__(self):
478478
return self
@@ -610,15 +610,14 @@ def perform(self, plaintexts):
610610

611611
for line in plaintexts:
612612
for cipher_type in self._cipher_types:
613-
index = config.cipher_types.index(cipher_type)
614-
label = self._cipher_types.index(cipher_type)
613+
label = config.cipher_types.index(cipher_type)
615614
if isinstance(config.key_lengths[label], list):
616615
key_lengths = config.key_lengths[label]
617616
else:
618617
key_lengths = [config.key_lengths[label]]
619618
for key_length in key_lengths:
620619
try:
621-
ciphertext = encrypt(line, index, key_length, self._keep_unknown_symbols)
620+
ciphertext = encrypt(line, label, key_length, self._keep_unknown_symbols)
622621
except:
623622
multiprocessing_logger.error(f"Could not encrypt line with cipher "
624623
f"'{cipher_type}'. and key length {key_length}. "

cipherTypeDetection/train.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ def load_plaintext_datasets_from_disk(args, requested_cipher_types, batch_size):
485485
`CipherStatisticsDataset`s for training and testing.
486486
"""
487487
# Filter cipher_types to exclude non-plaintext ciphers
488-
aca_cipher_types = [config.CIPHER_TYPES[i] for i in range(56)]
489-
cipher_types = [type for type in requested_cipher_types if type in aca_cipher_types]
488+
cipher_types = [type for type in requested_cipher_types if type not in config.ROTOR_CIPHER_TYPES]
490489

491490
# Get all paths to plaintext files in the `plaintext_input_directory`
492491
plaintext_files = []
@@ -1031,8 +1030,7 @@ def expand_cipher_groups(cipher_types):
10311030
expanded.append(config.CIPHER_TYPES[i])
10321031
elif config.ALL in expanded:
10331032
del expanded[expanded.index(config.ALL)]
1034-
for i in range(61):
1035-
expanded.append(config.CIPHER_TYPES[i])
1033+
expanded.extend(config.CIPHER_TYPES)
10361034
return expanded
10371035

10381036
def main():

0 commit comments

Comments
 (0)