Skip to content

Commit cfba1d9

Browse files
committed
Add Rail Fence Cipher.
1 parent 71db2d2 commit cfba1d9

File tree

6 files changed

+254
-202
lines changed

6 files changed

+254
-202
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ a set of rules that precisely define a sequence of operations.
143143
* `A` [Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
144144
* **Cryptography**
145145
* `B` [Polynomial Hash](src/algorithms/cryptography/polynomial-hash) - rolling hash function based on polynomial
146-
* `B` [Rail Fence Cypher](src/algorithms/cryptography/rail-fence-cipher) - a transposition cipher algorithm for encoding messages
146+
* `B` [Rail Fence Cipher](src/algorithms/cryptography/rail-fence-cipher) - a transposition cipher algorithm for encoding messages
147147
* `B` [Caesar Cipher](src/algorithms/cryptography/caesar-cipher) - simple substitution cipher
148148
* `B` [Hill Cipher](src/algorithms/cryptography/hill-cipher) - substitution cipher based on linear algebra
149149
* **Machine Learning**
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# Rail fence Cipher
1+
# Rail Fence Cipher
22

3-
This is a [transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher) in which the message is split accross a set of rails on a fence for encoding. The fence is populated with the message's characters, starting at the top left and adding a character on each position, traversing them diagonally to the bottom. Upon reaching the last rail, the direction should then turn diagonal and upwards up to the very first rail in a zig-zag motion. Rinse and repeat until the message is fully disposed across the fence. The encoded message is the result of concatenating the text in each rail, from top to bottom.
3+
The **rail fence cipher** (also called a **zigzag cipher**) is a [transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher) in which the message is split across a set of rails on a fence for encoding. The fence is populated with the message's characters, starting at the top left and adding a character on each position, traversing them diagonally to the bottom. Upon reaching the last rail, the direction should then turn diagonal and upwards up to the very first rail in a zig-zag motion. Rinse and repeat until the message is fully disposed across the fence. The encoded message is the result of concatenating the text in each rail, from top to bottom.
44

5-
From [wikipedia](https://en.wikipedia.org/wiki/Rail_fence_cipher), this is what the message `WE ARE DISCOVERED. FLEE AT ONCE` looks like on a 3-rail fence:
5+
From [wikipedia](https://en.wikipedia.org/wiki/Rail_fence_cipher), this is what the message `WE ARE DISCOVERED. FLEE AT ONCE` looks like on a `3`-rail fence:
66

77
```
88
W . . . E . . . C . . . R . . . L . . . T . . . E
99
. E . R . D . S . O . E . E . F . E . A . O . C .
1010
. . A . . . I . . . V . . . D . . . E . . . N . .
1111
-------------------------------------------------
12-
ECRLTEERDSOEEFEAOCAIVDEN
12+
WECRLTEERDSOEEFEAOCAIVDEN
1313
```
1414

15-
The message can then be decoded by re-creating the encode fence, with the same traversal pattern, except characters should only be added on one rail at a time. To ilustrate that, a dash can be added on the rails that are not supposed to be poupated yet. This is what the fence would look like after populating the first rail, the dashes represent positions that were visited but not populated.
15+
The message can then be decoded by re-creating the encoded fence, with the same traversal pattern, except characters should only be added on one rail at a time. To illustrate that, a dash can be added on the rails that are not supposed to be populated yet. This is what the fence would look like after populating the first rail, the dashes represent positions that were visited but not populated.
1616

1717
```
1818
W . . . E . . . C . . . R . . . L . . . T . . . E
@@ -22,4 +22,7 @@ W . . . E . . . C . . . R . . . L . . . T . . . E
2222

2323
It's time to start populating the next rail once the number of visited fence positions is equal to the number of characters in the message.
2424

25-
[Learn more](https://crypto.interactive-maths.com/rail-fence-cipher.html)
25+
## References
26+
27+
- [Rail Fence Cipher on Wikipedia](https://en.wikipedia.org/wiki/Rail_fence_cipher)
28+
- [Rail Fence Cipher Calculator](https://crypto.interactive-maths.com/rail-fence-cipher.html)
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
1-
import encodeRailFenceCipher from '../encodeRailFence';
2-
import decodeRailFenceCipher from '../decodeRailFence';
1+
import { encodeRailFenceCipher, decodeRailFenceCipher } from '../railFenceCipher';
32

4-
describe('rail fence cipher', () => {
3+
describe('railFenceCipher', () => {
54
it('encodes a string correctly for base=3', () => {
65
expect(encodeRailFenceCipher('', 3)).toBe('');
7-
expect(encodeRailFenceCipher('WEAREDISCOVEREDFLEEATONCE', 3)).toBe('WECRLTEERDSOEEFEAOCAIVDEN');
8-
expect(encodeRailFenceCipher('Hello, World!', 3)).toBe('Hoo!el,Wrdl l');
6+
expect(encodeRailFenceCipher('12345', 3)).toBe(
7+
'15243',
8+
);
9+
expect(encodeRailFenceCipher('WEAREDISCOVEREDFLEEATONCE', 3)).toBe(
10+
'WECRLTEERDSOEEFEAOCAIVDEN',
11+
);
12+
expect(encodeRailFenceCipher('Hello, World!', 3)).toBe(
13+
'Hoo!el,Wrdl l',
14+
);
915
});
1016

1117
it('decodes a string correctly for base=3', () => {
1218
expect(decodeRailFenceCipher('', 3)).toBe('');
13-
expect(decodeRailFenceCipher('WECRLTEERDSOEEFEAOCAIVDEN', 3)).toBe('WEAREDISCOVEREDFLEEATONCE');
14-
expect(decodeRailFenceCipher('Hoo!el,Wrdl l', 3)).toBe('Hello, World!');
19+
expect(decodeRailFenceCipher('WECRLTEERDSOEEFEAOCAIVDEN', 3)).toBe(
20+
'WEAREDISCOVEREDFLEEATONCE',
21+
);
22+
expect(decodeRailFenceCipher('Hoo!el,Wrdl l', 3)).toBe(
23+
'Hello, World!',
24+
);
25+
expect(decodeRailFenceCipher('15243', 3)).toBe(
26+
'12345',
27+
);
1528
});
1629

1730
it('encodes a string correctly for base=4', () => {
1831
expect(encodeRailFenceCipher('', 4)).toBe('');
19-
expect(encodeRailFenceCipher('THEYAREATTACKINGFROMTHENORTH', 4)).toBe('TEKOOHRACIRMNREATANFTETYTGHH');
32+
expect(encodeRailFenceCipher('THEYAREATTACKINGFROMTHENORTH', 4)).toBe(
33+
'TEKOOHRACIRMNREATANFTETYTGHH',
34+
);
2035
});
2136

2237
it('decodes a string correctly for base=4', () => {
2338
expect(decodeRailFenceCipher('', 4)).toBe('');
24-
expect(decodeRailFenceCipher('TEKOOHRACIRMNREATANFTETYTGHH', 4)).toBe('THEYAREATTACKINGFROMTHENORTH');
39+
expect(decodeRailFenceCipher('TEKOOHRACIRMNREATANFTETYTGHH', 4)).toBe(
40+
'THEYAREATTACKINGFROMTHENORTH',
41+
);
2542
});
2643
});

src/algorithms/cryptography/rail-fence-cipher/decodeRailFence.js

-108
This file was deleted.

src/algorithms/cryptography/rail-fence-cipher/encodeRailFence.js

-52
This file was deleted.

0 commit comments

Comments
 (0)