Skip to content

Instantly share code, notes, and snippets.

@bitmaybewise
Created June 2, 2012 23:28
Show Gist options
  • Save bitmaybewise/2860461 to your computer and use it in GitHub Desktop.
Save bitmaybewise/2860461 to your computer and use it in GitHub Desktop.
Python Caesar Cipher
#!/usr/bin/python
# Filename: CaesarCipher.py
# Author: Hercules Lemke Merscher
class CaesarCipher(object):
''' Cifra de Cesar.'''
def encrypt(self, msg, key):
if msg == None: return None
size = len(msg)
encrypted_msg = []
for i in range(size):
character = ord(msg[i])
encrypted_msg.append(chr(character + key))
encrypted_msg = ''.join(encrypted_msg)
return encrypted_msg
def decrypt(self, encrypted_msg, key):
return self.encrypt(encrypted_msg, -key)
#!/usr/bin/python
# Filename: CaesarCipherTest.py
# Author: Hercules Lemke Merscher
import unittest
from CaesarCipher import *
class CaesarCipherTest(unittest.TestCase):
''' Testes da classe CaesarCipher.'''
cipher = CaesarCipher()
# testes para o metodo "encrypt"
def testEncryptWithNullString(self):
self.assertEqual(None, self.cipher.encrypt(None, 3))
def testEncryptWithEmptyString(self):
self.assertEqual("", self.cipher.encrypt("", 3))
def testEncryptWithKeyEqualZero(self):
self.assertEqual("abc", self.cipher.encrypt("abc", 0))
def testEncryptWithKeyEqualOne(self):
self.assertEqual("bcd", self.cipher.encrypt("abc", 1))
def testEncryptWithKeyEqualThree(self):
self.assertEqual("def", self.cipher.encrypt("abc", 3))
def testEncryptMyNameWithKeyEqualSix(self):
self.assertEqual("Nkxi{rky", self.cipher.encrypt("Hercules", 6))
# testes para o metodo "decrypt"
def testDecryptWithNullString(self):
self.assertEqual(None, self.cipher.decrypt(None, 3))
def testDecryptWithEmptyString(self):
self.assertEqual("", self.cipher.decrypt("", 3))
def testDecryptWithKeyEqualZero(self):
self.assertEqual("abc", self.cipher.decrypt("abc", 0))
def testDecryptWithKeyEqualOne(self):
self.assertEqual("abc", self.cipher.decrypt("bcd", 1))
def testDecryptWithKeyEqualThree(self):
self.assertEqual("abc", self.cipher.decrypt("def", 3))
def testDecryptMyNameEncryptedWithKeyEqualSix(self):
self.assertEqual("Hercules", self.cipher.decrypt("Nkxi{rky", 6))
# executando todos os testes
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment