#!/usr/bin/python from sys import argv, stdin # This will be different for everyone! # rowlen and rows_per_block form the key rowlen, rows_per_block = [int(n) for n in argv[1].split("x")] blocklen = rowlen * rows_per_block fulltext = stdin.read().lower() ciphertext = "" # This operates on one block at a time block = "" # Encrypts whatever is in block. # If the block[row * rowlen + col] doesn't make sense, # draw a block of numbers on paper, starting at 0, and # observe that the formula row*rowlen + col comes out right # assuming you number rows and columns starting at 0 def encrypt_block(): encrypted_block = "" for col in range(rowlen): for row in range(rows_per_block): encrypted_block += block[row * rowlen + col] return encrypted_block for letter in fulltext: letter_number = ord(letter) - ord('a') + 1 if letter_number < 1 or letter_number > 26: continue # Fill block with appropriate letters block += letter if len(block) == blocklen: ciphertext += encrypt_block() block = "" # Maybe there was something in block when the end of # the ciphertext was reached, and if so, it needs to # be encrypted. Think about the implications of using # the beginning of the ciphertext for padding! if len(block) > 0: block += ciphertext ciphertext += encrypt_block() print ciphertext.upper()