Skip to content

Commit ff9e013

Browse files
author
duaraghav8@gmail
committed
Merge remote-tracking branch 'upstream/gh-pages'
2 parents 110973a + 982022a commit ff9e013

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function getPosUp(pos) {
2+
return (pos === alphabet.length - 1) ? 0 : pos + 1;
3+
}
4+
5+
function getPosDown(pos) {
6+
return (pos === 0) ? alphabet.length - 1 : pos - 1;
7+
}
8+
9+
function getNextChar(currChar, direction) {
10+
var pos = alphabetMap[currChar];
11+
var nextPos = direction === 'up' ? getPosUp(pos) : getPosDown(pos);
12+
var nextChar = alphabet.charAt(nextPos);
13+
14+
logger._print(currChar + ' -> ' + nextChar);
15+
return nextChar;
16+
}
17+
18+
function cipher(str, rotation, direction, cipherTracer) {
19+
if (!str) return '';
20+
21+
for (var i = 0; i < str.length; i++) {
22+
23+
cipherTracer._wait();
24+
25+
var currChar = str.charAt(i);
26+
var r = rotation;
27+
28+
logger._print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
29+
cipherTracer._notify(i)._wait();
30+
31+
// perform given amount of rotations in the given direction
32+
while (--r > 0) {
33+
currChar = getNextChar(currChar, direction);
34+
cipherTracer._notify(i, currChar)._wait();
35+
}
36+
37+
str = str.substring(0, i) + currChar + str.substring(i + 1);
38+
logger._print('Current result: ' + str);
39+
}
40+
41+
cipherTracer._select(0, i);
42+
return str;
43+
}
44+
45+
function encrypt(str, rotation) {
46+
logger._print('Encrypting: ' + str);
47+
return cipher(str, rotation, 'down', encryptTracer);
48+
}
49+
50+
function decrypt(str, rotation) {
51+
logger._print('Decrypting: ' + str);
52+
return cipher(str, rotation, 'up', decryptTracer);
53+
}
54+
55+
var encrypted = encrypt(string, rotation);
56+
logger._print('Encrypted result: ' + encrypted);
57+
58+
decryptTracer._setData(encrypted);
59+
var decrypted = decrypt(encrypted, rotation);
60+
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)