Skip to content

Commit a3a309d

Browse files
added ciphers
1 parent ca5ca5b commit a3a309d

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

encryption/caesars_cipher.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var cipherText;
2+
3+
function caesarsCipherCrypt(str, key) {
4+
var i = 0, toUp = str.toUpperCase(), size = toUp.length, newStr = [];
5+
for(; i < size; i += 1) {
6+
var toCode = toUp[i].charCodeAt(0);
7+
if(toCode < 65 || toCode > 90) {
8+
newStr[i] = String.fromCharCode(toCode);
9+
} else if(toCode < 78) {
10+
newStr[i] = String.fromCharCode(toCode + key);
11+
} else {
12+
newStr[i] = String.fromCharCode(toCode - key);
13+
}
14+
}
15+
16+
cipherText = newStr.join('');
17+
return cipherText;
18+
}
19+
20+
caesarsCipherCrypt('HELLO', 10);
21+
22+
function caesarsCipherEncrypt(str) {
23+
var i = 0, toUp = str.toUpperCase(), size = toUp.length, newStr = [];
24+
for(; i < size; i += 1) {
25+
var toCode = toUp[i].charCodeAt(0);
26+
if(toCode < 65 || toCode > 90) {
27+
newStr[i] = String.fromCharCode(toCode);
28+
} else if(toCode < 78) {
29+
newStr[i] = String.fromCharCode(toCode + 10);
30+
} else {
31+
newStr[i] = String.fromCharCode(toCode - 10);
32+
}
33+
}
34+
35+
cipherText = newStr.join('');
36+
return cipherText;
37+
}
38+
39+
caesarsCipherEncrypt(cipherText);
File renamed without changes.

encryption/substitution_cipher.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var map = {
2+
a: 'q', b: 'w', c: 'e',
3+
d: 'r', e: 't', f: 'y',
4+
g: 'u', h: 'i', i: 'o',
5+
j: 'p', k: 'a', l: 's',
6+
m: 'd', n: 'f', o: 'g',
7+
p: 'h', q: 'j', r: 'k',
8+
s: 'l', t: 'z', u: 'x',
9+
v: 'c', w: 'v', x: 'b',
10+
y: 'n', z: 'm'
11+
}, text = 'Hello World!';
12+
13+
function substitutionCipherCrypt(text) {
14+
return text.split('').filter(function(v) {
15+
return map.hasOwnProperty(v.toLowerCase());
16+
}).map(function(v) {
17+
return map[v.toLowerCase()].toUpperCase();
18+
}).join('');
19+
}
20+
21+
substitutionCipherCrypt(text);

encryption/transposition.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// function reverseInPlace(str){
2-
// return str.split(' ').reverse().join(' ').split('').reverse().join('');
3-
// }
4-
//
5-
// reverseInPlace('I am the good boy');
1+
function reverseInPlace(str){
2+
return str.split(' ').reverse().join(' ').split('').reverse().join('');
3+
}
4+
5+
reverseInPlace('I am the good boy');

encryption/vigenere_cipher.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var tabulaRecta = {
2+
a: "abcdefghijklmnopqrstuvwxyz",
3+
b: "bcdefghijklmnopqrstuvwxyza",
4+
c: "cdefghijklmnopqrstuvwxyzab",
5+
d: "defghijklmnopqrstuvwxyzabc",
6+
e: "efghijklmnopqrstuvwxyzabcd",
7+
f: "fghijklmnopqrstuvwxyzabcde",
8+
g: "ghijklmnopqrstuvwxyzabcdef",
9+
h: "hijklmnopqrstuvwxyzabcdefg",
10+
i: "ijklmnopqrstuvwxyzabcdefgh",
11+
j: "jklmnopqrstuvwxyzabcdefghi",
12+
k: "klmnopqrstuvwxyzabcdefghij",
13+
l: "lmnopqrstuvwxyzabcdefghijk",
14+
m: "mnopqrstuvwxyzabcdefghijkl",
15+
n: "nopqrstuvwxyzabcdefghijklm",
16+
o: "opqrstuvwxyzabcdefghijklmn",
17+
p: "pqrstuvwxyzabcdefghijklmno",
18+
q: "qrstuvwxyzabcdefghijklmnop",
19+
r: "rstuvwxyzabcdefghijklmnopq",
20+
s: "stuvwxyzabcdefghijklmnopqr",
21+
t: "tuvwxyzabcdefghijklmnopqrs",
22+
u: "uvwxyzabcdefghijklmnopqrst",
23+
v: "vwxyzabcdefghijklmnopqrstu",
24+
w: "wxyzabcdefghijklmnopqrstuv",
25+
x: "xyzabcdefghijklmnopqrstuvw",
26+
y: "yzabcdefghijklmnopqrstuvwx",
27+
z: "zabcdefghijklmnopqrstuvwxy"
28+
};
29+
30+
function vigenereCipherCrypt(encryptWord, key) {
31+
var i, size = encryptWord.length,
32+
keySize = key.length,
33+
encryptedText = '',
34+
specialCharacterCount = 0;
35+
for(i = 0; i < size; i += 1) {
36+
var keyLetter = (i - specialCharacterCount) % keySize,
37+
keywordIndex = tabulaRecta.a.indexOf(key[keyLetter]);
38+
if(tabulaRecta[encryptWord[i]]){
39+
encryptedText += tabulaRecta[encryptWord[i]][keywordIndex];
40+
} else {
41+
encryptedText += encryptWord[i];
42+
specialCharacterCount++;
43+
}
44+
}
45+
return encryptedText;
46+
}
47+
48+
vigenereCipherCrypt('steve', 'abc');

0 commit comments

Comments
 (0)