Skip to content

Commit c5b1239

Browse files
committed
Add up/down parameter for encoding and exemples
1 parent 33a10be commit c5b1239

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

codext/crypto/railfence.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414

1515

1616
__examples__ = {
17-
'enc(rail-5-3)': {'this is a test' : 'it sss etiath '},
18-
'dec(rail-7-4)': {'a stiet shsti': 'this is a test'}
17+
'enc(rail-5-3|rail_5_3)': {'this is a test' : 'it sss etiath '},
18+
'enc(railup-5-3|railup_5_3)' :{'this is a test': 'h tiats e ssit'},
19+
'dec(rail-7-4|rail_7_4)': {'a stiet shsti': 'this is a test'}
1920
}
2021

2122

2223

23-
def __buildf(text, rails, offset = 0) :
24-
l, rail, dr = len(text), offset, 1
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
2531
f = [["#"] * l for i in range(rails)]
2632
for x in range(l) :
2733
f[rail][x] = text[x]
@@ -30,26 +36,22 @@ def __buildf(text, rails, offset = 0) :
3036
elif rail <= 0:
3137
dr = 1
3238
rail += dr
33-
for elem in f :
34-
print(elem)
3539
return f
3640

37-
def railfence_encode(rails, offset = 0) :
41+
def railfence_encode(up = 0, rails = 3, offset = 0) :
3842
def encode(text, errors="strict") :
39-
print(len(text))
40-
4143
c,l = '', len(text)
42-
f = __buildf(text,rails,offset)
44+
f = __buildf(text,rails,offset, up)
4345
for r in range(rails) :
4446
for x in range(l) :
4547
if f[r][x] != '#' :
4648
c += f[r][x]
4749
return c, l
4850
return encode
4951

50-
def railfence_decode(rails, offset = 0) :
52+
def railfence_decode(up = 0,rails = 3, offset = 0) :
5153
def decode(text, errors = 'strict') :
52-
f = __buildf("x" * len(text), rails, offset)
54+
f = __buildf("x" * len(text), rails, offset, up)
5355
plain, i = '', 0
5456
ra, l = range(rails), range(len(text))
5557

@@ -69,7 +71,4 @@ def decode(text, errors = 'strict') :
6971

7072
return decode
7173

72-
add("rail", railfence_encode, railfence_decode, r"rail-(\d+)\-(\d+)$")
73-
74-
#rail-(\d+)\-(\d+)
75-
#rail-(\d+)(\-*(\d+))
74+
add("rail", railfence_encode, railfence_decode, r"rail(up)?[-_](\d+)[-_](\d+)$")

0 commit comments

Comments
 (0)