Skip to content

Commit 38675a3

Browse files
committed
Added caesar cipher algorithm
1 parent 2a7061f commit 38675a3

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

algorithm/category.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"bridges": "Find-Bridges"
1212
}
1313
},
14+
"cryptography": {
15+
"name": "Cryptography",
16+
"list": {
17+
"caesar_cipher": "Caesar Cipher"
18+
}
19+
},
1420
"mst": {
1521
"name": "Minimum Spanning Tree",
1622
"list": {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function getPosUp(pos) {
2+
if (pos === alphabet.length - 1) {
3+
return 0;
4+
} else {
5+
return pos + 1;
6+
}
7+
}
8+
9+
function getPosDown(pos) {
10+
if (pos === 0) {
11+
return alphabet.length - 1;
12+
} else {
13+
return pos - 1;
14+
}
15+
}
16+
17+
function getNextChar(currChar, direction) {
18+
var pos = alphabetMap[currChar];
19+
var nextPos = direction === 'up' ? getPosUp(pos) : getPosDown(pos);
20+
var nextChar = alphabet.charAt(nextPos);
21+
logger._print(currChar + ' -> ' + nextChar);
22+
return nextChar;
23+
}
24+
25+
function cipher(str, rotation, direction, cipherTracer) {
26+
if (!str) return '';
27+
28+
for (var i = 0; i < str.length; i++) {
29+
30+
var currChar = str.charAt(i);
31+
var r = rotation;
32+
33+
logger._print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
34+
cipherTracer._notify(i)._wait();
35+
36+
// perform given amount of rotations in the given direction
37+
while (--r > 0) {
38+
currChar = getNextChar(currChar, direction);
39+
cipherTracer._notify(i, currChar)._wait();
40+
}
41+
42+
logger._print('Rotation result: ' + currChar);
43+
44+
str = str.substring(0, i) + currChar + str.substring(i + 1);
45+
46+
logger._print('Current result: ' + str);
47+
}
48+
49+
cipherTracer._select(0, i);
50+
return str;
51+
}
52+
53+
function encrypt(str, rotation) {
54+
logger._print('Encrypting: ' + str);
55+
return cipher(str, rotation, 'down', encryptTracer);
56+
}
57+
58+
function decrypt(str, rotation) {
59+
logger._print('Decrypting: ' + str);
60+
return cipher(str, rotation, 'up', decryptTracer);
61+
}
62+
63+
var encrypted = encrypt(string, rotation);
64+
logger._print('Encrypted result: ' + encrypted);
65+
66+
decryptTracer._setData(encrypted);
67+
var decrypted = decrypt(encrypted, rotation);
68+
logger._print('Decrypted result: ' + decrypted);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var string = 'secret';
2+
var rotation = 5;
3+
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
4+
// create a map of char -> position to improve run time
5+
// otherwise we would have to search the alphabet each
6+
// time to find the character position
7+
var alphabetMap = alphabet.split('').reduce(function(map, curr, idx) {
8+
map[curr] = idx;
9+
return map;
10+
}, {});
11+
12+
var logger = new LogTracer();
13+
var encryptTracer = new Array1DTracer('Encryption');
14+
var decryptTracer = new Array1DTracer('Decryption');
15+
16+
encryptTracer._setData(string);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Caesar Cipher": "In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.",
3+
"Applications": [
4+
"Often incorporated as part of more complex schemes, such as the Vigenère cipher"
5+
],
6+
"Complexity": {
7+
"time": "best O(N * #ofRotations), worst O(N * #ofRotations * alphabetSize)",
8+
"space": "best O(1), worst O(alphabetSize)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Caesar_cipher'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"basic": "Encrypting and Decrypting a string using character rotation"
15+
}
16+
}

0 commit comments

Comments
 (0)