4 from szyfrow
.keyword_cipher
import *
5 from szyfrow
.support
.utilities
import *
7 def test_cipher_alphabet():
8 assert keyword_cipher_alphabet_of('bayes') == 'bayescdfghijklmnopqrtuvwxz'
9 assert keyword_cipher_alphabet_of('bayes', KeywordWrapAlphabet
.from_a
) == 'bayescdfghijklmnopqrtuvwxz'
10 assert keyword_cipher_alphabet_of('bayes', KeywordWrapAlphabet
.from_last
) == 'bayestuvwxzcdfghijklmnopqr'
11 assert keyword_cipher_alphabet_of('bayes', KeywordWrapAlphabet
.from_largest
) == 'bayeszcdfghijklmnopqrtuvwx'
14 def test_encipher_message():
15 enciphered
= keyword_encipher('test message', 'bayes')
16 expected
= 'rsqr ksqqbds'
17 assert enciphered
== expected
19 enciphered
= keyword_encipher('test message', 'bayes', KeywordWrapAlphabet
.from_a
)
20 expected
= 'rsqr ksqqbds'
21 assert enciphered
== expected
23 enciphered
= keyword_encipher('test message', 'bayes', KeywordWrapAlphabet
.from_last
)
24 expected
= 'lskl dskkbus'
25 assert enciphered
== expected
27 enciphered
= keyword_encipher('test message', 'bayes', KeywordWrapAlphabet
.from_largest
)
28 expected
= 'qspq jsppbcs'
29 assert enciphered
== expected
32 def test_decipher_message():
33 plaintext
= 'test message'
34 for key
in ['bayes', 'samplekey']:
35 for wrap
in KeywordWrapAlphabet
:
36 enciphered
= keyword_encipher(plaintext
, key
, wrap
)
37 deciphered
= keyword_decipher(enciphered
, key
, wrap
)
38 assert deciphered
== plaintext
41 def test_keyword_break_single_thread():
42 plaintext
= 'this is a test message for the keyword breaking routine'
43 expected_key
= 'elephant'
44 expected_wrap
= KeywordWrapAlphabet
.from_last
45 ciphertext
= keyword_encipher(plaintext
, expected_key
, expected_wrap
)
46 (key
, wrap
), score
= keyword_break_single_thread(ciphertext
,
47 wordlist
=['cat', 'elephant', 'kangaroo'])
48 assert key
== expected_key
49 assert wrap
== expected_wrap
50 assert score
== pytest
.approx(Pletters(sanitise(plaintext
)))
52 def test_keyword_break():
53 plaintext
= 'this is a test message for the keyword breaking routine'
54 expected_key
= 'elephant'
55 expected_wrap
= KeywordWrapAlphabet
.from_last
56 ciphertext
= keyword_encipher(plaintext
, expected_key
, expected_wrap
)
57 (key
, wrap
), score
= keyword_break(ciphertext
,
58 wordlist
=['cat', 'elephant', 'kangaroo'])
59 assert key
== expected_key
60 assert wrap
== expected_wrap
61 assert score
== pytest
.approx(Pletters(sanitise(plaintext
)))
64 # def test_simulated_annealing_break():
66 # plaintext = '''You will rejoice to hear that no disaster has accompanied the
67 # commencement of an enterprise which you have regarded with such evil
68 # forebodings. I arrived here yesterday, and my first task is to assure
69 # my dear sister of my welfare and increasing confidence in the success
72 # I am already far north of London, and as I walk in the streets of
73 # Petersburgh, I feel a cold northern breeze play upon my cheeks, which
74 # braces my nerves and fills me with delight. Do you understand this
75 # feeling? This breeze, which has travelled from the regions towards
76 # which I am advancing, gives me a foretaste of those icy climes.
77 # Inspirited by this wind of promise, my daydreams become more fervent
80 # I try in vain to be persuaded that the pole is the seat of
81 # frost and desolation; it ever presents itself to my imagination as the
82 # region of beauty and delight. There, Margaret, the sun is for ever
83 # visible, its broad disk just skirting the horizon and diffusing a
84 # perpetual splendour. There—for with your leave, my sister, I will put
85 # some trust in preceding navigators—there snow and frost are banished;
86 # and, sailing over a calm sea, we may be wafted to a land surpassing in
87 # wonders and in beauty every region hitherto discovered on the habitable
90 # Its productions and features may be without example, as the
91 # phenomena of the heavenly bodies undoubtedly are in those undiscovered
92 # solitudes. What may not be expected in a country of eternal light? I
93 # may there discover the wondrous power which attracts the needle and may
94 # regulate a thousand celestial observations that require only this
95 # voyage to render their seeming eccentricities consistent for ever. I
96 # shall satiate my ardent curiosity with the sight of a part of the world
97 # never before visited, and may tread a land never before imprinted by
98 # the foot of man. These are my enticements, and they are sufficient to
99 # conquer all fear of danger or death and to induce me to commence this
100 # laborious voyage with the joy a child feels when he embarks in a little
101 # boat, with his holiday mates, on an expedition of discovery up his
104 # But supposing all these conjectures to be false, you cannot contest the
105 # inestimable benefit which I shall confer on all mankind, to the last
106 # generation, by discovering a passage near the pole to those countries, to
107 # reach which at present so many months are requisite; or by ascertaining the
108 # secret of the magnet, which, if at all possible, can only be effected by an
109 # undertaking such as mine.'''
111 # expected_key = keyword_cipher_alphabet_of('abced')
112 # ciphertext = keyword_encipher(sanitise(plaintext), expected_key)
113 # expected_score = Ptrigrams(sanitise(plaintext))
114 # actual_key, actual_score = monoalphabetic_sa_break(ciphertext,
115 # plain_alphabet=string.ascii_lowercase,
116 # cipher_alphabet=string.ascii_lowercase,
117 # workers=10, max_iterations=1000,
120 # assert expected_key == actual_key
121 # assert expected_score == pytest.approx(actual_score)