Created
February 10, 2014 04:11
-
-
Save jameslyons/8910253 to your computer and use it in GitHub Desktop.
porta cipher in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| implements porta cipher | |
| Author: James Lyons | |
| Created: 2012-06-27 | |
| ''' | |
| from .base import Cipher | |
| #################################################################################### | |
| class Porta(Cipher): | |
| def __init__(self,key='CODE'): | |
| self.key = [k.upper() for k in key] | |
| def encipher(self,string): | |
| string = self.remove_punctuation(string) | |
| ret = '' | |
| for (i,c) in enumerate(string): | |
| i = i%len(self.key) | |
| if self.key[i] in 'AB': ret += 'NOPQRSTUVWXYZABCDEFGHIJKLM'[self.a2i(c)] | |
| elif self.key[i] in 'CD': ret += 'ZNOPQRSTUVWXYBCDEFGHIJKLMA'[self.a2i(c)] | |
| elif self.key[i] in 'EF': ret += 'YZNOPQRSTUVWXCDEFGHIJKLMAB'[self.a2i(c)] | |
| elif self.key[i] in 'GH': ret += 'XYZNOPQRSTUVWDEFGHIJKLMABC'[self.a2i(c)] | |
| elif self.key[i] in 'IJ': ret += 'WXYZNOPQRSTUVEFGHIJKLMABCD'[self.a2i(c)] | |
| elif self.key[i] in 'KL': ret += 'VWXYZNOPQRSTUFGHIJKLMABCDE'[self.a2i(c)] | |
| elif self.key[i] in 'MN': ret += 'UVWXYZNOPQRSTGHIJKLMABCDEF'[self.a2i(c)] | |
| elif self.key[i] in 'OP': ret += 'TUVWXYZNOPQRSHIJKLMABCDEFG'[self.a2i(c)] | |
| elif self.key[i] in 'QR': ret += 'STUVWXYZNOPQRIJKLMABCDEFGH'[self.a2i(c)] | |
| elif self.key[i] in 'ST': ret += 'RSTUVWXYZNOPQJKLMABCDEFGHI'[self.a2i(c)] | |
| elif self.key[i] in 'UV': ret += 'QRSTUVWXYZNOPKLMABCDEFGHIJ'[self.a2i(c)] | |
| elif self.key[i] in 'WX': ret += 'PQRSTUVWXYZNOLMABCDEFGHIJK'[self.a2i(c)] | |
| elif self.key[i] in 'YZ': ret += 'OPQRSTUVWXYZNMABCDEFGHIJKL'[self.a2i(c)] | |
| return ret | |
| def decipher(self,string): | |
| return self.encipher(string) | |
| if __name__ == '__main__': | |
| print 'use "import pycipher" to access functions' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment