Advertisement
UF6

1.5 Problem 7

UF6
Jun 17th, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. class cipher(object):
  2.     alphabet = 'abcdefghijklmnopqrstuvwxyz'
  3.     cipheralphabet = 'abcdefghijklmnopqrstuvwxyz'
  4.  
  5.     def __init__(self, key='', type='Null'):
  6.         self.key = key
  7.         self.type = type
  8.        
  9.     def __str__(self):
  10.         return self.type +  " cipher"
  11.  
  12.     def encrypt(self, message):
  13.         result = ''
  14.         for ch in message:
  15.             x = self.alphabet.find(ch)
  16.             if x != -1:
  17.                 result += self.cipheralphabet[x]
  18.             else:
  19.                 result += ch
  20.         return result
  21.  
  22.     def decrypt(self, message):
  23.         result = ''
  24.         for ch in message:
  25.             x = self.cipheralphabet.find(ch)
  26.             if x != -1:
  27.                 result += self.alphabet[x]
  28.             else:
  29.                 result += ch
  30.         return result
  31.  
  32. class cipher (Cipher):
  33.  
  34.     def __init__(self, key=3, type="Caesar"):
  35.         self.key = key
  36.         self.type = type
  37.         self.cipher_alphabet = self.alphabet[self.key:] + self.alphabet[0:self.key]
  38.  
  39. caesar = CaesarCipher(3, "Caesar")
  40. secret = caesar.encrypt("hello this is a test")
  41.  
  42. print (secret)
  43. #khoor wklv lv d whvw
  44.  
  45. print ( caesar.decrypt(secret))
  46. #hello this is a test
  47.  
  48. secret = caesar.encrypt("abcdefghijklmnopqrstuvwxyz")
  49.  
  50. print (secret)
  51.  
  52. #defghijklmnopqrstuvwxyzabc
  53.  
  54. print (caesar.decrypt(secret))
  55. #abcdefghijklmnopqrstuvwxyz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement