Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfba1d9

Browse files
committed
Add Rail Fence Cipher.
1 parent71db2d2 commitcfba1d9

File tree

6 files changed

+254
-202
lines changed

6 files changed

+254
-202
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
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 FenceCypher](src/algorithms/cryptography/rail-fence-cipher) - a transposition cipher algorithm for encoding messages
146+
*`B`[Rail FenceCipher](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**
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
#Railfence Cipher
1+
#RailFence Cipher
22

3-
Thisis a[transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher) in which the message is splitaccross 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 splitacross 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 a3-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 theencode fence, with the same traversal pattern, except characters should only be added on one rail at a time. Toilustrate that, a dash can be added on the rails that are not supposed to bepoupated 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 theencoded fence, with the same traversal pattern, except characters should only be added on one rail at a time. Toillustrate that, a dash can be added on the rails that are not supposed to bepopulated 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-
importencodeRailFenceCipherfrom'../encodeRailFence';
2-
importdecodeRailFenceCipherfrom'../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

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp