def _iterator(self): block = Block() valid_chars = re.compile(r"^[a-z]$") for char in self._plain_text: if not valid_chars.match(char): block.add_special(char) continue if char == self._playfair_key.substitute_char: char = self._playfair_key.substitute_by if char == block.current_char: block.add_char(self._playfair_key.padding_char) yield block block = Block() block.add_char(char) if block.ready: yield block block = Block() # if block ends with a single character, add padding char and yield it if block.current_char: block.add_char(self._playfair_key.padding_char) yield block # if block has no chars but has special chars, yield it elif block.has_content: yield block
def test_03_add_special(self): block = Block() block.add_special("!") self.assertEqual(block._index, 1) self.assertEqual(block._specials[0].char, "!") self.assertEqual(block._specials[0].index, 0) self.assertEqual(len(block._chars), 0)
def test_07_has_content(self): block = Block() self.assertFalse(block.has_content) block = Block() block.add_char("a") self.assertTrue(block.has_content) block = Block() block.add_special(".") self.assertTrue(block.has_content)
def test_04_add_both(self): block = Block() block.add_char("a") block.add_special("!") block.add_char("b") self.assertEqual(block._chars[0].char, "a") self.assertEqual(block._chars[0].index, 0) self.assertEqual(block._specials[0].char, "!") self.assertEqual(block._specials[0].index, 1) self.assertEqual(block._chars[1].char, "b") self.assertEqual(block._chars[1].index, 2) self.assertEqual(block._index, 3)
def _iterator(self): block = Block() valid_chars = re.compile(r"^[a-z]$") for char in self._cipher_text: if not valid_chars.match(char): block.add_special(char) continue block.add_char(char) if block.ready: yield block block = Block() # if block ends with a single character, add padding char and yield it if block.current_char: raise ValueError("Invalid cipher_text, couldn't decrypt block!") # if block has no chars but has special chars, yield it elif block.has_content: yield block
def test_03_construct_cipher_text(self): playfair_encrypt = PlayFairEncrypt(self._playfair_key) playfair_encrypt._blocks = [] block = Block() block.add_char("a") block.add_char("b") playfair_encrypt._blocks.append(block) block = Block() block.add_special(",") block.add_char("c") block.add_special(".") block.add_char("d") block.add_special("1") playfair_encrypt._blocks.append(block) self.assertEqual(playfair_encrypt._construct_cipher_text(), "ab,c.d1")
def test_03_construct_cipher_text(self): playfair_decrypt = PlayFairDecrypt(self._playfair_key) playfair_decrypt._blocks = [] # add plain block block = Block() block.add_char("a") block.add_char("b") playfair_decrypt._blocks.append(block) # add block with specials block = Block() block.add_special(",") block.add_char("c") block.add_special(".") block.add_char("d") block.add_special("1") playfair_decrypt._blocks.append(block) # add blocks with padding chars (foo) block = Block() block.add_char("f") block.add_char("o") playfair_decrypt._blocks.append(block) block = Block() block.add_char("x") block.add_char("o") playfair_decrypt._blocks.append(block) # add block with substitute_by block = Block() block.add_char("i") block.add_char("a") playfair_decrypt._blocks.append(block) # add block ending with padding_char block = Block() block.add_char("o") block.add_char("x") playfair_decrypt._blocks.append(block) plain_text = "ab,c.d1foojao" self.assertEqual(playfair_decrypt._construct_plain_text(), plain_text)
def test_09_text(self): # test 2 char 2 special block block = Block() block.add_char("a") block.add_special(",") block.add_char("b") block.add_special(".") self.assertEqual(block.text, "a,b.") # test 2 char block block = Block() block.add_char("a") block.add_char("b") self.assertEqual(block.text, "ab") # test 2 special block block = Block() block.add_special("!") block.add_special("@") self.assertEqual(block.text, "!@")