25th February, 2017

Rail fence ciphering in Python

A rail fence algorithm in Python 3.5.

This is a Python 3.5 script for ciphering strings using rail fence cipher.
https://en.wikipedia.org/wiki/Rail_fence_cipher. You can find this project on my GitHub by clicking here.

This is a Python function that uses multidimensional arrays to create a matrix at X length (the length of the string) and Y depth (the key specified) to encipher and decipher data using rail fence - a method for arranging data in a zig-zag pattern using a grid.

Rail fence doesn't require a symmetric key in the same way ROT13 does; rail fence has a set matrix depth - which controls the depth / height of the zig-zag (a greater depth will result in higher obfuscation). This makes rail fence a very insecure ciphering algorithm, as it can easily be brute-forced. Rail fence is best used as a simple text obfuscate-er / scrambler.

Source code

  
string = ""
key = 3
mode = 0


def cipher(s, k, m):
    w = len(s)
    h = k
    matrix = [[0 for x in range(w)] for y in range(h)]
    output = []
    replace = []
    def c(string):
        cur_x, cur_y, not_0 = 0, 0, 0
        for char in string:
            matrix[cur_y][cur_x] = char
            cur_x += 1
            if cur_y < h -1 and not_0 == 0:
                cur_y +=1
            else:
                if cur_y > 0:
                    cur_y -=1
                    not_0 = 1
                else:
                    cur_y += 1
                    not_0 = 0
        return matrix
    def d(string):
        pointer = 0
        for i in range(len(string)):
            replace.append("r")
        for row in c(''.join(replace)):
            for pos, char in enumerate(row):
                if char == "r":
                    row[pos] = list(string)[pointer]
                    pointer +=1
        return matrix
    if m == 0:
        for row in c(string):
            print(row)
            for char in row:
                if char is not 0:
                    output.append(char)
        return ''.join(output)
    if m == 1:
        cur_x, cur_y, not_0 = 0, 0, 0
        matrix = d(string)
        for row in matrix:
            print(row)
        while len(output) < len(list(string)):
            output.append(matrix[cur_y][cur_x])
            cur_x += 1
            if cur_y < h - 1 and not_0 == 0:
                cur_y += 1
            else:
                if cur_y > 0:
                    cur_y -= 1
                    not_0 = 1
                else:
                    cur_y += 1
                    not_0 = 0
        return ''.join(output)


print(string, "->", cipher(string, key, mode))
  

Very inefficient, but it works.

Example usage

  
## This is an example output from ciphering the sentence "Hello world this is a test message":

['H', 0, 0, 0, 0, 0, 0, 0, 0, 0, 'd', 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a', 0, 0, 0, 0, 0, 0, 0, 0, 0, 's', 0, 0, 0]
[0, 'e', 0, 0, 0, 0, 0, 0, 0, 'l', 0, ' ', 0, 0, 0, 0, 0, 0, 0, ' ', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 's', 0, 'a', 0, 0]
[0, 0, 'l', 0, 0, 0, 0, 0, 'r', 0, 0, 0, 't', 0, 0, 0, 0, 0, 's', 0, 0, 0, 't', 0, 0, 0, 0, 0, 'e', 0, 0, 0, 'g', 0]
[0, 0, 0, 'l', 0, 0, 0, 'o', 0, 0, 0, 0, 0, 'h', 0, 0, 0, 'i', 0, 0, 0, 0, 0, 'e', 0, 0, 0, 'm', 0, 0, 0, 0, 0, 'e']
[0, 0, 0, 0, 'o', 0, 'w', 0, 0, 0, 0, 0, 0, 0, 'i', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 's', 0, ' ', 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 's', 0, 0, 0, 0, 0, 0, 0, 0, 0, 't', 0, 0, 0, 0, 0, 0, 0, 0]
Hello world this is a test message -> Hdasel   salrtsteglohiemeowi s  st
  

Thank you for reading.

🥝🥝🥝


Leave a comment

Invalid or missing field(s).
Comment sent successfully, please wait for it to be approved.

This post has no comments