Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ o
- [X] `atbash`: aka Atbash Cipher
- [X] `bacon`: aka Baconian Cipher
- [X] `barbie-N`: aka Barbie Typewriter (*N* belongs to [1, 4])
- [X] `citrix`: aka Citrix CTX1 passord encoding
- [X] `citrix`: aka Citrix CTX1 password encoding
- [X] `rail`: aka Rail Fence Cipher
- [X] `rotN`: aka Caesar cipher (*N* belongs to [1,25])
- [X] `scytaleN`: encrypts using the number of letters on the rod (*N* belongs to [1,[)
- [X] `shiftN`: shift ordinals (*N* belongs to [1,255])
Expand Down
1 change: 1 addition & 0 deletions codext/crypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .bacon import *
from .barbie import *
from .citrix import *
from .railfence import *
from .rot import *
from .scytale import *
from .shift import *
Expand Down
76 changes: 76 additions & 0 deletions codext/crypto/railfence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: UTF-8 -*-
"""Rail Fence Cipher Codec - rail fence encoding.

This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""



from ..__common__ import *


__examples__ = {
'enc(rail-5-3|rail_5_3)': {'this is a test' : 'it sss etiath '},
'enc(rail-5-3-up|rail_5_3-up)' :{'this is a test': 'h tiats e ssit'},
'dec(rail-7-4|rail_7_4)': {'a stiet shsti': 'this is a test'}
}



def __buildf(text, rails, offset = 0, up = 0) :
l, rail = len(text), offset
if up != '' :
dr = -1
rail = rails - offset - 1
else :
dr = 1

f = [["#"] * l for i in range(rails)]

for x in range(l) :
f[rail][x] = text[x]
if rail >= rails - 1:
dr = -1
elif rail <= 0:
dr = 1
rail += dr
return f

def railfence_encode(rails = 3, offset = 0, up = 0) :
def encode(text, errors="strict") :
c,l = '', len(text)
f = __buildf(text, rails, offset, up)
for r in range(rails) :
for x in range(l) :
if f[r][x] != '#' :
c += f[r][x]
return c, l
return encode

def railfence_decode(rails = 3, offset = 0, up = 0) :
def decode(text, errors = 'strict') :
f = __buildf("x" * len(text), rails, offset, up)
plain, i = '', 0
ra, l = range(rails), range(len(text))

#Put the characters in the right place
for r in ra:
for x in l :
if f[r][x] == "x" :
f[r][x] = text[i]
i += 1
#Read the characters in the right order
for x in l :
for r in ra:
if f[r][x] != '#' :
plain += f[r][x]

return plain, len(plain)

return decode

add("rail", railfence_encode, railfence_decode, r"rail[-_](\d+)[-_](\d+)[-_]?(up)?$")
15 changes: 15 additions & 0 deletions docs/enc/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ This implements the Citrix CTX1 password encoding algorithm.

-----

### Rail Fence Cipher

This implements the Rail Fence encoding algorithm.

**Codec** | **Conversions** | **Aliases** | **Comment**
:---: | :---: | --- | ---
`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.
```python
>>> codext.encode("this is a test", "rail-5-3")
'it sss etiath '
>>> codext.decode("it sss etiath ", "rail-5-3")
'this is a test'
```

-----
### ROT N

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.
Expand Down