#!/usr/bin/env python ########################################################################### # Copyright (C) 2007 Ryan Kavanagh # # This program is free software; you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 2 of the License, or # # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # # with this program; if not, write to the Free Software Foundation, Inc., # # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # You may contact the author at ryanakca@kubuntu@org # ########################################################################### class KCipherUtilities: """Class with utilities for the Encrypt and Decrypt face.""" def set_phrase(self, string): """Define the phrase to encrypt or decrypt""" self.phrase = string def is_vowel(self, letter): """Determines if 'letter' is a vowel or not. Usefull for pig latin.""" return letter in ['a', 'e', 'i', 'o', 'u'] def is_punctuation(self, character): """Determines of 'character' is punctuation""" return character in ['!', '?', '.', ',', ';', ':', '"', "'"] class KCipherEncrypt(KCipherUtilities): """Class to encrypt a sentence into pig latin, Vigenere""" def pig(self): """Ciphers self.phrase into pig latin.""" self.phrase = self.phrase.split() self.encrypted_phrase = "" for word in self.phrase: # Is the first character a vowel? If so, just add 'ay' if self.is_vowel(word[0]): # Is the last character punctuation? If so, we need to put it # at the end. if self.is_punctuation(word[-1]): self.encrypted_phrase = self.encrypted_phrase + \ word[:-1] + 'ay' + word[-1] + ' ' # Last character in word is not punctuation else: self.encrypted_phrase = self.encrypted_phrase + \ word + 'ay ' # The first character is a consonnant. Move last char to end and # add 'ay' else: # Is the last character punctuation? If so, we need to put it # at the end. if self.is_punctuation(word[-1]): self.encrypted_phrase = self.encrypted_phrase + \ word[1:-1] + word[0] + 'ay' + word[-1] + ' ' # Last character in word is not punctuation else: self.encrypted_phrase = self.encrypted_phrase + \ word[1:] + word[0] + 'ay ' # remove the space at the end self.encrypted_phrase = self.encrypted_phrase[:-1] return self.encrypted_phrase def vigenere(self): """Ciphers self.phrase into the vigenere cipher.""" class KCipherDecrypt(KCipherUtilities): """Class to decrypt a sentence from pig latin, vigenere."""