This repository was archived by the owner on Sep 7, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed
Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ def railencrypt (st ,k ):
2+ c = 0
3+ x = 0
4+ m = [[0 ] * (len (st )) for i in range (k )]
5+ for r in range (len (st )):
6+ m [c ][r ] = st [r ]
7+ if x == 0 :
8+ if c == (k - 1 ):
9+ x = 1
10+ c -= 1
11+ else :
12+ c += 1
13+ else :
14+ if c == 0 :
15+ x = 0
16+ c += 1
17+ else :
18+ c -= 1
19+
20+ result = []
21+ for i in range (k ):
22+ for j in range (len (st )):
23+ if m [i ][j ] != 0 :
24+ result .append (m [i ][j ])
25+ print ("CipherText:" ,"" . join (result ))
26+
27+ def raildecrypt (st ,k ):
28+ c , x = 0 , 0
29+ m = [[0 ] * (len (st )) for i in range (k )]
30+ for r in range (len (st )):
31+ m [c ][r ] = 1
32+ if x == 0 :
33+ if c == (k - 1 ):
34+ x = 1
35+ c -= 1
36+ else :
37+ c += 1
38+ else :
39+ if c == 0 :
40+ x = 0
41+ c += 1
42+ else :
43+ c -= 1
44+ result = []
45+ c , x = 0 , 0
46+ for i in range (k ):
47+ for j in range (len (st )):
48+ if m [i ][j ] == 1 :
49+ m [i ][j ] = st [x ]
50+ x += 1
51+ for r in range (len (st )):
52+ if m [c ][r ] != 0 :
53+ result .append (m [c ][r ])
54+ if x == 0 :
55+ if c == (k - 1 ):
56+ x = 1
57+ c -= 1
58+ else :
59+ c += 1
60+ else :
61+ if c == 0 :
62+ x = 0
63+ c += 1
64+ else :
65+ c -= 1
66+ print ("PlainText:" ,"" . join (result ))
67+
68+ if __name__ == "__main__" :
69+ string = input ("Enter the Message:" )
70+ string = string .upper ()
71+ key = int (input ("Enter the Key:" ))
72+ n = int (input ("1.Encryption\n 2.Decryption\n Input Your choice:" ))
73+ if (n == 1 ):
74+ railencrypt (string ,key )
75+ elif (n == 2 ):
76+ raildecrypt (string ,key )
77+ else :
78+ print ("Error" )
You can’t perform that action at this time.
0 commit comments