-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrail_fence_cipher.py
69 lines (57 loc) · 2.23 KB
/
rail_fence_cipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/python
# -*- coding: utf-8 -*-
#############################################################################
### ###
### Description: Functions for encrypting and decrypting a message ###
### using the zigzag method. Which produces an array ###
### that if written out properly will look as though ###
### the word was written in a zigzag format ###
### ###
#############################################################################
__author__ = "Tyler Hall"
__copyright__ = "Copyright 2017"
###################
# IMPORTS #
###################
###################
# GLOBAL #
###################
###################
# FUNCTIONS #
###################
# Method: takes a message and produces a zigzag array
# Param: text: message string
# key: The number of rows that the cipher will
# produce for the zigzag
# Returns: returns an array of the zigzag text
def fence(text, key):
#Variables
fence = [[None] * len(text) for n in range(key)]
rails = range(key - 1) + range(key - 1, 0, -1)
for n, x in enumerate(text):
fence[rails[n % len(rails)]][n] = x
if 0: # debug
for rail in fence:
print ''.join('.' if c is None else str(c) for c in rail)
return [c for rail in fence for c in rail if c is not None]
# Method: takes a message and uses function fence
# to produce the ciphered string
# Param: raw: plain text
# key: The number of rows that the cipher will
# produce for the zigzag
# Returns: the complete ciphered text
def encrypt(raw, key):
return ''.join(fence(raw, key))
# Method: takes a ciphered message and uses function fence
# to produce the plain text
# Param: decrypt: cipher text
# key: The number of rows that was used in the
# original cipher
# Returns: the original plain text
def decrypt(message, key):
rng = range(len(message))
pos = fence(rng, key)
return ''.join(message[pos.index(key)] for key in rng)
###################
# MAIN #
###################