def decodeFirstPass(stream): decimal_num = [] for symbol in stream: if symbol == ' ': yield chr(int(tools.concat(decimal_num))) decimal_num.clear() else: decimal_num.append(symbol) if len(decimal_num) > 0: yield chr(int(tools.concat(decimal_num)))
# Test the polyalphabetic analysis module from cryptoamino import tools from cryptoamino.analysis import characterisation from cryptoamino.analysis import polyalphabetic from cryptoamino.cipher.vigenere import Vigenere # # Test the likely substitution periods detector # # Any periodic polyalphabetic cipher cipher = Vigenere(tools.add, tools.subtract) # Test case plaintext = ("assdassddadsdasdasddddsadasdsdasdsdasdsassssdaasdsd" + "asdddsasasdsdsdsssasdasdadssssdasdaasdasdassssdadad" + "asssdsddsadasdsdaaddsaddsdssdsasdadsssdsdasdsdsddsd") key = "udgnshb" correct_period = len(key) cipher.key(key) ciphertext = tools.concat(cipher.encrypt(plaintext)) # Run the detector detected = polyalphabetic.likely_substitution_periods(ciphertext, 2, 16) print("Periods detected:", detected) if detected[0] != correct_period: raise Exception("Likely substitution period detector failed.")
from cryptoamino import tools from cryptoamino.encoding import * encodings = [ # G.I.O.I. 1 encoding (gioi1.Gioi1(), "Some ascii text will do it!") ] for codec, sample in encodings: encoded = list(codec.encode(sample)) print("{0} -> {1}".format(sample, tools.concat(encoded))) sample2 = tools.concat(codec.decode(encoded)) if list(sample2) != list(sample): raise Exception( "Encoding failed: sample {0} was decoded to {1}".format( sample, sample2))
from cryptoamino.analysis import characterisation from cryptoamino.analysis import distribution from cryptoamino.cipher.vigenere import * from cryptoamino import tools # # Brute Force test, using a cipher # # Test case cipher = Vigenere(tools.add, tools.subtract) plaintext = tools.normalise_text( "abbabaabababbabbbabbababababababbbbabaaabababbabbbabababbaabababbbab") correct_key = "ht" cipher.key(correct_key) ciphertext = tools.concat(cipher.encrypt(plaintext)) # Prepare the attack keys = itertools.product(tools.letters, repeat=2) quadgram_freq = distribution.freq(characterisation.quads(plaintext)) text_fitness = characterisation.quadgram_scorer(quadgram_freq) def key_fitness(key): cipher.key(key) return text_fitness(cipher.decrypt(ciphertext)) # Perform the attack recovered_key = tools.concat( heuristic.brute_force(keys=keys, fitness=key_fitness))
from cryptoamino.cipher import * from cryptoamino import tools # Get a random letter substitution shuffled_letters = list(tools.letters).copy() random.shuffle(shuffled_letters) random_substitution = {a: b for a, b in zip(tools.letters, shuffled_letters)} ciphers = [ # Simple substitution over lowercase letters, ignoring other symbols. (simple_substitution.SimpleSubstitution(), random_substitution, "plaintext plaintext plaintext"), # Vigenere over lowercase letters only. (vigenere.Vigenere(tools.add, tools.subtract), "key", "plaintextplaintextglorp") ] # Run all ciphers in encrypt then decrypt mode to ensure they completely undo # themselves. for cipher, key, plaintext in ciphers: cipher.key(key) ciphertext = tools.concat(cipher.encrypt(plaintext)) print("{0} -> {1}".format(plaintext, tools.concat(ciphertext))) plaintext2 = tools.concat(cipher.decrypt(ciphertext)) if plaintext2 != plaintext: raise Exception( "Cipher failed: plaintext {0} was decrypted to {1}".format( plaintext, plaintext2))
def decodeSecondPass(stream): for hex_num in tools.group(stream, 2): yield chr(int(tools.concat(hex_num), 16))