Skip to content

Commit 6522ed9

Browse files
authored
Merge pull request #3 from smarbal/master
Added codec: railfence
2 parents 9e4d71e + 9efecc5 commit 6522ed9

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ o
258258
- [X] `atbash`: aka Atbash Cipher
259259
- [X] `bacon`: aka Baconian Cipher
260260
- [X] `barbie-N`: aka Barbie Typewriter (*N* belongs to [1, 4])
261-
- [X] `citrix`: aka Citrix CTX1 passord encoding
261+
- [X] `citrix`: aka Citrix CTX1 password encoding
262+
- [X] `rail`: aka Rail Fence Cipher
262263
- [X] `rotN`: aka Caesar cipher (*N* belongs to [1,25])
263264
- [X] `scytaleN`: encrypts using the number of letters on the rod (*N* belongs to [1,[)
264265
- [X] `shiftN`: shift ordinals (*N* belongs to [1,255])

codext/crypto/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .bacon import *
55
from .barbie import *
66
from .citrix import *
7+
from .railfence import *
78
from .rot import *
89
from .scytale import *
910
from .shift import *

codext/crypto/railfence.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Rail Fence Cipher Codec - rail fence encoding.
3+
4+
This codec:
5+
- en/decodes strings from str to str
6+
- en/decodes strings from bytes to bytes
7+
- decodes file content to str (read)
8+
- encodes file content from str to bytes (write)
9+
"""
10+
11+
12+
13+
from ..__common__ import *
14+
15+
16+
__examples__ = {
17+
'enc(rail-5-3|rail_5_3)': {'this is a test' : 'it sss etiath '},
18+
'enc(rail-5-3-up|rail_5_3-up)' :{'this is a test': 'h tiats e ssit'},
19+
'dec(rail-7-4|rail_7_4)': {'a stiet shsti': 'this is a test'}
20+
}
21+
22+
23+
24+
def __buildf(text, rails, offset = 0, up = 0) :
25+
l, rail = len(text), offset
26+
if up != '' :
27+
dr = -1
28+
rail = rails - offset - 1
29+
else :
30+
dr = 1
31+
32+
f = [["#"] * l for i in range(rails)]
33+
34+
for x in range(l) :
35+
f[rail][x] = text[x]
36+
if rail >= rails - 1:
37+
dr = -1
38+
elif rail <= 0:
39+
dr = 1
40+
rail += dr
41+
return f
42+
43+
def railfence_encode(rails = 3, offset = 0, up = 0) :
44+
def encode(text, errors="strict") :
45+
c,l = '', len(text)
46+
f = __buildf(text, rails, offset, up)
47+
for r in range(rails) :
48+
for x in range(l) :
49+
if f[r][x] != '#' :
50+
c += f[r][x]
51+
return c, l
52+
return encode
53+
54+
def railfence_decode(rails = 3, offset = 0, up = 0) :
55+
def decode(text, errors = 'strict') :
56+
f = __buildf("x" * len(text), rails, offset, up)
57+
plain, i = '', 0
58+
ra, l = range(rails), range(len(text))
59+
60+
#Put the characters in the right place
61+
for r in ra:
62+
for x in l :
63+
if f[r][x] == "x" :
64+
f[r][x] = text[i]
65+
i += 1
66+
#Read the characters in the right order
67+
for x in l :
68+
for r in ra:
69+
if f[r][x] != '#' :
70+
plain += f[r][x]
71+
72+
return plain, len(plain)
73+
74+
return decode
75+
76+
add("rail", railfence_encode, railfence_decode, r"rail[-_](\d+)[-_](\d+)[-_]?(up)?$")

docs/enc/crypto.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ This implements the Citrix CTX1 password encoding algorithm.
128128

129129
-----
130130

131+
### Rail Fence Cipher
132+
133+
This implements the Rail Fence encoding algorithm.
134+
135+
**Codec** | **Conversions** | **Aliases** | **Comment**
136+
:---: | :---: | --- | ---
137+
`rail` | text <-> rail fence ciphertext, X rails and Y offset | `rail-X-Y`, `rail_X_Y`, `rail-X-Y-up`| The encoding fence is built from the top. Careful to trailing whitespaces. The `up` flag is used to build the fence from the bottom to the top.
138+
```python
139+
>>> codext.encode("this is a test", "rail-5-3")
140+
'it sss etiath '
141+
>>> codext.decode("it sss etiath ", "rail-5-3")
142+
'this is a test'
143+
```
144+
145+
-----
131146
### ROT N
132147

133148
This is a dynamic encoding, that is, it can be called with an integer to define the ROT offset. Encoding will apply a positive offset, decoding will apply a negative one.

0 commit comments

Comments
 (0)