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

Skip to content

Added Caesar Cipher to Cryptograpy section #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ a set of rules that precisely define a sequence of operations.
* `A` [Strongly Connected Components](src/algorithms/graph/strongly-connected-components) - Kosaraju's algorithm
* `A` [Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
* **Cryptography**
* `B` [Polynomial Hash](src/algorithms/cryptography/polynomial-hash) - rolling hash function based on polynomial
* `B` [Polynomial Hash](src/algorithms/cryptography/polynomial-hash) - rolling hash function
* `B` [Caesar Cipher](src/algorithms/cryptography/caesar-cipher) - caesar-cipher
* **Uncategorized**
* `B` [Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower)
* `B` [Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm
Expand Down
46 changes: 46 additions & 0 deletions src/algorithms/cryptography/caesar-cipher/CaesarCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export default class CaesarCipher {
/**
* Function that encrypts the word
* @assumption : The version of the function assumes that the inputs are wording.
* No provision or testing was done for other types of input
* @param {wording} word : The text to encrypt
* @param {number} shifts : By how much amount the text should be shifted
* @return {wording}
*/

encrypt(word, shifts) {
if (shifts < 0) {
return this.encrypt(word, shifts + 26);
}

let output = '';
for (let i = 0; i < word.length; i += 1) {
let c = word[i];
if (c.match(/[a-z]/i)) {
const code = word.charCodeAt(i);
if ((code >= 65) && (code <= 90)) {
c = String.fromCharCode(((code - 65 + shifts) % 26) + 65);
} else if ((code >= 97) && (code <= 122)) {
c = String.fromCharCode(((code - 97 + shifts) % 26) + 97);
}
}

output += c;
}
return output;
}

/**
* Function that encrypts the word
* @assumption : The version of the function assumes that the inputs are wording.
* No provision or testing was done for other types of input
* @param {wording} word : The text to decrypt
* @param {number} shifts : By how much amount the text should be shifted back to
* retrive the original text
* @return {wording}
*/

decrypt(word, shifts) {
return (shifts < 0) ? this.encrypt(word, 26 - shifts) : this.encrypt(word, -shifts);
}
}
15 changes: 15 additions & 0 deletions src/algorithms/cryptography/caesar-cipher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Ceasar Cipher
**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.


## Encryption Function
e(x) = (x+k) mod(26)
## Decryption Function
e(x) = (x-k) mod(26)

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

## References

- [Ceasar Cipher](http://practicalcryptography.com/ciphers/caesar-cipher/)

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import CaesarCipher from '../CaesarCipher';

describe('ceasarCipher', () => {
it('ciphers and decyphers the text', () => {
const cipher = new CaesarCipher();

expect(cipher.encrypt('ABC', 3)).toBe('DEF');
expect(cipher.decrypt('DEF', 3)).toBe('ABC');
});
});