Rail Fence Cipher- Looking for a better solution
Asked Answered
S

3

9

I have coded Rail Fence Cipher in Python. I was wondering if there could be a better solution.

For those who don't know what rail fence cipher is, it is basically a method of writing plain text in a way it creates linear pattern in a spiral way. Example - when "FOOBARBAZ" rail-fenced using key of 3.

F . . . A . . . Z . . .
. O . B . R . A . Q . X
. . O . . . B . . . U .

Reading the above spiral line-by-line, the cipher text becomes "FAZOBRAQXOBU". Read more at - Rail fence - Wikipedia.

def cipher(s, key, graph=False) :
    down=True
    raw_out=[]
    out=''
    i=0
    for x in range(key) :
        raw_out.append({})
    for pos in range(len(s)) :
        raw_out[i][pos]=s[pos]
        if i==key-1 :
            down=False
        if i==0 :
            down=True
        if down :
            i=i+1
        else :
            i=i-1
    for p in raw_out :
        for q in p :
            out+=p[q]
    if graph :
        return raw_out
    return out

def decipher(s, key) :
    map_list=cipher(s, key, True) #CREATING JUST FOR MAPPING - WHICHth CHARACTER OF THE STRING - IS WHICHth CHARACTER OF THE CIPHER
    new={}
    out=''
    s_counter=0
    for x in map_list :
        for y in x :
            new[y]=s[s_counter]
            s_counter+=1
    for p in new :
        out+=new[p]
    return map_list

I was wondering if there was any better way of doing this, since my procedure is very costly, it a uses couple of dictionaries.

Code in any language is welcomed.

Sundae answered 25/1, 2013 at 9:51 Comment(0)
C
14

Just for the heck of it...

def fence(lst, numrails):
    fence = [[None] * len(lst) for n in range(numrails)]
    rails = range(numrails - 1) + range(numrails - 1, 0, -1)
    for n, x in enumerate(lst):
        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]

def encode(text, n):
    return ''.join(fence(text, n))

def decode(text, n):
    rng = range(len(text))
    pos = fence(rng, n)
    return ''.join(text[pos.index(n)] for n in rng)

z = encode('ATTACK.AT.DAWN', 3)    
print z # ACTWTAKA.ANT.D
y = decode(z, 3)
print y # ATTACK.AT.DAWN
Creative answered 25/1, 2013 at 10:46 Comment(3)
return [c for rail in fence for c in rail if c is not None] - Gosh this is why I hate PythonJudyjudye
return [c for rail in fence for c in rail if c is not None] that's the same reason why I love pythonLymphadenitis
if you understand it, its dope.. I bet!Lymphadenitis
R
2

See an explanation at my blog.

(define (waves str h)
  (define (down str)
    (if (>= h (length str))
        (list (fill h str))
        (cons (take h str) (up (drop h str)))))
  (define (up str)
    (if (>= (- h 2) (length str))
        (list (pad (fill (- h 2) str)))
        (cons (pad (take (- h 2) str)) (down (drop (- h 2) str)))))
  (define (pad str) (append (list X) (reverse str) (list X)))
  (define (fill h str) (append str (make-list (- h (length str)) X)))
  (down str))

(define (fence lox h)
  (define a (apply append (transpose (waves lox h))))
  (filter (lambda (e) (not (eq? X e))) a))

(define (encipher str h)
  (list->string (fence (string->list str) h)))

(define (decipher str h)
  (define e (fence (range (string-length str)) h))
  (define x (map list e (string->list str)))
  (define y (sort (lambda (i j) (<= (car i) (car j))) x))
  (define z (map cadr y))
  (list->string z))
Rightminded answered 25/1, 2013 at 13:43 Comment(0)
A
0

I've recently implemented the encryption operation in Python 3 and I believe the solution is worth sharing:

from itertools import chain, cycle


def rail_fence(plaintext: str, n: int) -> str:
    rows = [""] * n
    zigzag = cycle(chain(range(n - 1), range(n - 1, 0, -1)))
    for i, c in zip(zigzag, plaintext):
        rows[i] += c
    
    return "".join(rows)

Example:

>>> rail_fence("FOOBARBAZQUX", 3) 
'FAZOBRAQXOBU'
Ambages answered 31/1, 2022 at 1:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.