Skip to content

Commit 8e15231

Browse files
authored
crypto
1 parent 8101813 commit 8e15231

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

Diff for: learning_crypto/caesar_cipher.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Simplest form of cryptography
2+
// shift alphabetic letters by a number
3+
// ex: ABC -> DEF (shift by 3)
4+
5+
const encrypt = (text, shift) => {
6+
let cipher = ''
7+
for (let i = 0; i < text.length; i++) {
8+
let charCode = text.charCodeAt(i)
9+
if (charCode === 32) {
10+
cipher += ' '
11+
} else {
12+
if (charCode < 65 && charCode > 90) break
13+
charCode = charCode + shift
14+
if (charCode > 90) charCode = 64 + (charCode - 90)
15+
cipher += String.fromCharCode(charCode)
16+
}
17+
}
18+
19+
return cipher
20+
}
21+
22+
const decrypt = (cipher, shift) => {
23+
let text = ''
24+
25+
for (let i = 0; i < cipher.length; i++) {
26+
let charCode = cipher.charCodeAt(i)
27+
if (charCode === 32) {
28+
text += ' '
29+
} else {
30+
if (charCode < 65 && charCode > 90) break
31+
charCode = charCode - shift
32+
if (charCode < 65) charCode = 90 - (64 - charCode)
33+
text += String.fromCharCode(charCode)
34+
}
35+
}
36+
37+
return text
38+
}
39+
40+
41+
module.exports = {
42+
encrypt,
43+
decrypt
44+
}

Diff for: learning_crypto/caesar_cipher.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const caesor_sypher = require('./caesar_cipher')
2+
3+
const shift = 3
4+
const text = 'HELLO WORLD'
5+
6+
console.log(`Text: ${text}`)
7+
8+
const cipher = caesor_sypher.encrypt(text, shift)
9+
console.log(`Cipher: ${cipher}`)
10+
11+
const decrypted = caesor_sypher.decrypt(cypher, shift)
12+
console.log(`Decrypt: ${decrypted}`)

Diff for: learning_crypto/vigenere_cipher.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Improve caesar cipher cryptographic - vigenere cipher
2+
// Shifted by values defined by a key
3+
4+
const SPACE_KEY = 32
5+
6+
const isAlphabet = (value) => {
7+
return value.split('').every((letter) => {
8+
const charCode = letter.charCodeAt()
9+
return charCode > 65 && charCode < 90
10+
})
11+
}
12+
13+
const encrypt = (text, key) => {
14+
if (!isAlphabet(key)) return new Error('Key must be alphabet.')
15+
16+
let keyCursor = 0
17+
let cipher = ''
18+
for (let i = 0; i < text.length; i++) {
19+
let charCode = text.charCodeAt(i)
20+
if (charCode === SPACE_KEY) {
21+
cipher += ' '
22+
continue
23+
}
24+
25+
if (charCode < 65 && charCode > 90) break
26+
27+
const keyCharCode = key.charCodeAt(keyCursor)
28+
const shift = keyCharCode - 65
29+
charCode = charCode + shift
30+
if (charCode > 90) charCode = 64 + (charCode - 90)
31+
cipher += String.fromCharCode(charCode)
32+
33+
keyCursor++
34+
if (keyCursor === key.length) keyCursor = 0
35+
}
36+
37+
return cipher
38+
}
39+
40+
const decrypt = (cipher, key) => {
41+
if (!isAlphabet(key)) return new Error('Key must be alphabet.')
42+
43+
let keyCursor = 0
44+
let text = ''
45+
for (let i = 0; i < cipher.length; i++) {
46+
let charCode = cipher.charCodeAt(i)
47+
if (charCode === SPACE_KEY) {
48+
text += ' '
49+
continue
50+
}
51+
52+
if (charCode < 65 && charCode > 90) break
53+
54+
const keyCharCode = key.charCodeAt(keyCursor)
55+
const shift = keyCharCode - 65
56+
charCode = charCode - shift
57+
if (charCode < 65) charCode = 90 - (64 - charCode)
58+
59+
keyCursor++
60+
if (keyCursor === key.length) keyCursor = 0
61+
text += String.fromCharCode(charCode)
62+
}
63+
64+
return text
65+
}
66+
67+
module.exports = {
68+
encrypt,
69+
decrypt
70+
}

Diff for: learning_crypto/vigenere_cipher.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const vigenere_cipher = require('./vigenere_cipher')
2+
3+
const key = 'DUH'
4+
const text = 'THEY DRINK THE TEA'
5+
6+
console.log(`Text: ${text}`)
7+
8+
const cipher = vigenere_cipher.encrypt(text, key)
9+
console.log(`Cipher: ${cipher}`)
10+
11+
const decrypt = vigenere_cipher.decrypt(cipher, key)
12+
console.log(`Decrypt: ${decrypt}`)

0 commit comments

Comments
 (0)