Cipher

Daniel Weschke

November 22, 2013

1 Reverse Cipher

encrypted_decrypted_string = message_string[::-1]
let encrypted_decrypted_string = reverse message_string

2 Caesar Cipher

Only letters.

import string
           # ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CSS = string.ascii_uppercase      # cipher's symbol set
message = message.upper()
num = key if isinstance(key, int) else CSS.find(key.upper())
encrypted = ''.join([
  CSS[(CSS.find(i)+num)%len(CSS)] if i in CSS else i
  for i in message])
decrypted = ''.join([
  CSS[(CSS.find(i)-num)%len(CSS)] if i in CSS else i
  for i in message])

For upper case letters and lower case letters delete line 4 and replace line 3 with the following CSS definition

# ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
CSS = string.ascii_letters        # cipher's symbol set

For letters and non-letter characters delete line 4 and replace line 3 with the following CSS definition

# printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
CSS = string.printable            # cipher's symbol set
import Data.List    -- elemIndex
import Data.Maybe   -- isJust, fromJust
import Data.Char    -- toUpper
import Text.Read    -- readMaybe

css = ['A'..'Z']
num = fromJust (readMaybe key :: Maybe Int)
encrypted = [if isJust $ toUpper i `elemIndex` css
    then css!!(mod ((fromJust $ toUpper i `elemIndex` css) + num) (length css))
    else i
  | i

2.1 Hacking the Caesar cipher with the brute-force technique

The technique of trying every possible decryption key is called a brute-force attack.

Only letters.

1import string
2           # ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
3CSS = string.ascii_uppercase      # cipher's symbol set
4message = message.upper()
5result = [''.join([
6  CSS[(CSS.find(i)+CSS.find(j))%len(CSS)] if i in CSS else i
7  for i in message]) for j in CSS]
8print('\n'.join(result))
5result_with_key = [(j, ''.join([
6  CSS[(CSS.find(i)+CSS.find(j))%len(CSS)] if i in CSS else i
7  for i in message])) for j in CSS]

3 Vigenère Cipher

Only letters.

"""
'message' non-letters are ignored
'key' string must be sub set of CSS
"""
import string
from itertools import cycle
           # ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CSS = string.ascii_uppercase      # cipher's symbol set
message = message.upper()
encrypted = ''.join([
  CSS[(CSS.find(i)+CSS.find(j))%len(CSS)] if i in CSS else i
  for i,j in zip(message,cycle(key))])
decrypted = ''.join([
  CSS[(CSS.find(i)-CSS.find(j))%len(CSS)] if i in CSS else i
  for i,j in zip(message,cycle(key))])
{-
'message' non-letters are ignored
'key' string must be sub set of css
-}
import Data.List    -- elemIndex
import Data.Maybe   -- isJust, fromJust
import Data.Char    -- toUpper

css = ['A'..'Z']
message = map toUpper message
encrypted = [if isJust $ i `elemIndex` css
    then css!!(mod
      ((fromJust $ i `elemIndex` css) + (fromJust $ j `elemIndex` css))
      (length css))
    else i
  | (i,j)