Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b56915f

Browse files
committed
Caesar Cipher
1 parent dc1047d commit b56915f

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default class CaesarCipher {
2+
/**
3+
* Function that encrypts the word
4+
* @assumption : The version of the function assumes that the inputs are wording.
5+
* No provision or testing was done for other types of input
6+
* @param {wording} word : The text to encrypt
7+
* @param {number} shifts : By how much amount the text should be shifted
8+
* @return {wording}
9+
*/
10+
11+
encrypt(word, shifts) {
12+
if (shifts < 0) {
13+
return this.encrypt(word, shifts + 26);
14+
}
15+
16+
let output = '';
17+
for (let i = 0; i < word.length; i + 1) {
18+
let c = word[i];
19+
if (c.match(/[a-z]/i)) {
20+
const code = word.charCodeAt(i);
21+
if ((code >= 65) && (code <= 90)) {
22+
c = String.fromCharCode(((code - 65 + shifts) % 26) + 65);
23+
} else if ((code >= 97) && (code <= 122)) {
24+
c = String.fromCharCode(((code - 97 + shifts) % 26) + 97);
25+
}
26+
}
27+
28+
output += c;
29+
}
30+
return output;
31+
}
32+
33+
/**
34+
* Function that encrypts the word
35+
* @assumption : The version of the function assumes that the inputs are wording.
36+
* No provision or testing was done for other types of input
37+
* @param {wording} word : The text to decrypt
38+
* @param {number} shifts : By how much amount the text should be shifted back to
39+
* retrive the original text
40+
* @return {wording}
41+
*/
42+
43+
decrypt(word, shifts) {
44+
return (shifts < 0) ? this.encrypt(word, 26 - shifts) : this.encrypt(word, -shifts);
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Ceasar Cipher
2+
**Caesar cipher**, also known as the shift cipher 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.
3+
4+
5+
## Encryption Function
6+
e(x) = (x+k) mod(26)
7+
## Decryption Function
8+
e(x) = (x-k) mod(26)
9+
10+
Where x is the text to be encrypted/decrypted and k is the private key or the amount by which the characters need to be shifted
11+
12+
## References
13+
14+
- [Where to Use Polynomial String Hashing](https://www.mii.lt/olympiads_in_informatics/pdf/INFOL119.pdf)
15+
- [Hashing on uTexas](https://www.cs.utexas.edu/~mitra/csSpring2017/cs313/lectures/hash.html)
16+
- [Hash Function on Wikipedia](https://en.wikipedia.org/wiki/Hash_function)
17+
- [Rolling Hash on Wikipedia](https://en.wikipedia.org/wiki/Rolling_hash)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import CaesarCipher from '../CaesarCipher';
2+
3+
describe('ceasarCipher', () => {
4+
it('shifts the character by a particular amount as specified by the user', () => {
5+
const cipher = new CaesarCipher();
6+
const encText = cipher.encrypt('ABC', 3);
7+
const decText = cipher.decrypt('DEF', 3);
8+
9+
expect(encText).toEqual('XYZ');
10+
expect(decText).toEqual('ABC');
11+
});
12+
});

0 commit comments

Comments
 (0)