Skip to content

Commit

Permalink
Add Rail Fence Cipher.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Dec 20, 2020
1 parent 71db2d2 commit cfba1d9
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 202 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ a set of rules that precisely define a sequence of operations.
* `A` [Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
* **Cryptography**
* `B` [Polynomial Hash](src/algorithms/cryptography/polynomial-hash) - rolling hash function based on polynomial
* `B` [Rail Fence Cypher](src/algorithms/cryptography/rail-fence-cipher) - a transposition cipher algorithm for encoding messages
* `B` [Rail Fence Cipher](src/algorithms/cryptography/rail-fence-cipher) - a transposition cipher algorithm for encoding messages
* `B` [Caesar Cipher](src/algorithms/cryptography/caesar-cipher) - simple substitution cipher
* `B` [Hill Cipher](src/algorithms/cryptography/hill-cipher) - substitution cipher based on linear algebra
* **Machine Learning**
Expand Down
15 changes: 9 additions & 6 deletions src/algorithms/cryptography/rail-fence-cipher/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Rail fence Cipher
# Rail Fence Cipher

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

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:
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:

```
W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
-------------------------------------------------
ECRLTEERDSOEEFEAOCAIVDEN
WECRLTEERDSOEEFEAOCAIVDEN
```

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

```
W . . . E . . . C . . . R . . . L . . . T . . . E
Expand All @@ -22,4 +22,7 @@ W . . . E . . . C . . . R . . . L . . . T . . . E

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.

[Learn more](https://crypto.interactive-maths.com/rail-fence-cipher.html)
## References

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

describe('rail fence cipher', () => {
describe('railFenceCipher', () => {
it('encodes a string correctly for base=3', () => {
expect(encodeRailFenceCipher('', 3)).toBe('');
expect(encodeRailFenceCipher('WEAREDISCOVEREDFLEEATONCE', 3)).toBe('WECRLTEERDSOEEFEAOCAIVDEN');
expect(encodeRailFenceCipher('Hello, World!', 3)).toBe('Hoo!el,Wrdl l');
expect(encodeRailFenceCipher('12345', 3)).toBe(
'15243',
);
expect(encodeRailFenceCipher('WEAREDISCOVEREDFLEEATONCE', 3)).toBe(
'WECRLTEERDSOEEFEAOCAIVDEN',
);
expect(encodeRailFenceCipher('Hello, World!', 3)).toBe(
'Hoo!el,Wrdl l',
);
});

it('decodes a string correctly for base=3', () => {
expect(decodeRailFenceCipher('', 3)).toBe('');
expect(decodeRailFenceCipher('WECRLTEERDSOEEFEAOCAIVDEN', 3)).toBe('WEAREDISCOVEREDFLEEATONCE');
expect(decodeRailFenceCipher('Hoo!el,Wrdl l', 3)).toBe('Hello, World!');
expect(decodeRailFenceCipher('WECRLTEERDSOEEFEAOCAIVDEN', 3)).toBe(
'WEAREDISCOVEREDFLEEATONCE',
);
expect(decodeRailFenceCipher('Hoo!el,Wrdl l', 3)).toBe(
'Hello, World!',
);
expect(decodeRailFenceCipher('15243', 3)).toBe(
'12345',
);
});

it('encodes a string correctly for base=4', () => {
expect(encodeRailFenceCipher('', 4)).toBe('');
expect(encodeRailFenceCipher('THEYAREATTACKINGFROMTHENORTH', 4)).toBe('TEKOOHRACIRMNREATANFTETYTGHH');
expect(encodeRailFenceCipher('THEYAREATTACKINGFROMTHENORTH', 4)).toBe(
'TEKOOHRACIRMNREATANFTETYTGHH',
);
});

it('decodes a string correctly for base=4', () => {
expect(decodeRailFenceCipher('', 4)).toBe('');
expect(decodeRailFenceCipher('TEKOOHRACIRMNREATANFTETYTGHH', 4)).toBe('THEYAREATTACKINGFROMTHENORTH');
expect(decodeRailFenceCipher('TEKOOHRACIRMNREATANFTETYTGHH', 4)).toBe(
'THEYAREATTACKINGFROMTHENORTH',
);
});
});
108 changes: 0 additions & 108 deletions src/algorithms/cryptography/rail-fence-cipher/decodeRailFence.js

This file was deleted.

52 changes: 0 additions & 52 deletions src/algorithms/cryptography/rail-fence-cipher/encodeRailFence.js

This file was deleted.

Loading

0 comments on commit cfba1d9

Please sign in to comment.