def validate(self, key): if key is None: key = self.default if len(key) != 25: raise ScytaleError( "The Playfair key must be 25 letters long; a 5x5 grid") key = key.upper() for l in self.default: if l not in key: raise ScytaleError("Missing letter in key: {0}".format(l)) return key
def validate(self, key): if key is None: key = self.default key = self.clean(key) if len(key) != 27: raise ScytaleError( "The Trifid key must be 27 letters long; a 3x3x3 grid") key = key.upper() for l in self.default: if l not in key: raise ScytaleError("Missing letter in key: {0}".format(l)) return key
def validate(self, key): if key is None: key = self.default xo = set(list(key)) if xo != set(["X", "o"]): raise ScytaleError( "The Fleissner Grille key must be a string of X (cut) and o (don't cut) letters only" ) length = len(key) sqrt = int(math.sqrt(length)) if math.pow(sqrt, 2) != length: raise ScytaleError( "You cannot form a square from {0} cells".format(length)) return key
def validate_key(self, key): if key is None: key = self.default if len(key) < 2: raise ScytaleError( "The Permutation key needs to be at least 2 letters") return key
def validate_correctness(self, cipher): try: ciphertext = cipher.encrypt(self.plaintext.data) assert cipher.compare_ciphertext(ciphertext, self.ciphertext.data) except Exception: self.ciphertext.errors.append("Incorrect ciphertext") raise ScytaleError("Incorrect ciphertext")
def validate(self, key): if key is None: key = self.default try: return int(key) except: raise ScytaleError("The Rail Fence key should be a number")
def validate(self, table): if table is None: table = "RAIN OTS EQWYUPDFGHJKLZXCVBM_." table = table.upper() if len(table) != 30: raise ScytaleError("Checkerboard table needs to be 30 characters long") rows = [table[0:10], table[10:20], table[20:30]] if list(rows[0]).count(" ") != 2: raise ScytaleError( "The first row in the Checkerboard table must have 2 spaces" ) if len(set(table)) != 29: # Extra one for gaps in top row raise ScytaleError( "The Checkerboard table must have 28 unique letters in it" ) return rows
def validate(self, key): if key is None: key = self.default if len(key) < 2: raise ScytaleError( "The Myszkowski key needs to be at least 2 letters") return key
def validate(self, indexes): if indexes is None: indexes = list(range(0, 14)) random.shuffle(indexes) if len(set(indexes)) != len(indexes): raise ScytaleError( "The shuffle cipher must not have repeating numbers in its key" ) return indexes
def validate_not_multiple_keys(self): for p in getattr(current_user, "points", []): multiple_keys = all([ p.reason == "Sent Message", p.message.cipher == self.cipher.data, p.message.key != self.key.data, ]) if multiple_keys: self.key.errors.append("You can only use one key per cipher") raise ScytaleError("You can only use one key per cipher")
def __init__(self, key=None): self.key = self.validate(key) self.grille = self.init_grille(self.key) self.key_size = len(self.key) self.grille_size = len(self.grille) all_a = self.encrypt("A" * self.key_size) all_a = all_a.replace("X", "") if len(all_a) != self.key_size: raise ScytaleError( "Either a space in the grille overlaps another, or your gaps do not cover the grid." )
def validate_not_duplicate_message(self): for p in getattr(current_user, "points", []): multiple_keys = all([ p.reason == "Sent Message", p.message.cipher == self.cipher.data, p.message.plaintext == self.plaintext.data, ]) if multiple_keys: self.plaintext.errors.append( "You can't send the same message twice") raise ScytaleError("You can't send the same message twice")
def validate(self, key, wildcard=None): if key is None: key = "QWERTYUIOPASDFG_HJKLZXCVBNM" if len(key) != 27: raise ScytaleError( "The Mixed Alphabet key must be 27 letters long; A-Z plus a space" ) key = key.upper() if wildcard is not None: if len(wildcard) != 1: raise ScytaleError( "The Mixed Alphabet wildcard must be a single character") wildcard = wildcard.upper() else: if len(set(key)) != 27: raise ScytaleError( "The Mixed Alphabet key must have 27 unique letters in it; A-Z plus a space" ) return key, wildcard
def validate(self, offset): if 0 >= offset > 27: raise ScytaleError( "The Caesar key must be a number between 1 and 26") return offset
def encrypt(self, plaintext): if len(plaintext) > len(self.key): raise ScytaleError("Plaintext too big") plaintext = self.clean(plaintext.upper()) plaintext = self.pad(plaintext) return "".join(plaintext[i] for i in self.key)
def validate_plaintext(self, plaintext): if not all(p in self.alphabet for p in plaintext): raise ScytaleError( "Some plaintext letters are not in your alphabet")