forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#58 from jonathangomz/patch-1
Create vigenereCipher.js
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Check if the Character is letter or not | ||
* @param {String} character - character to check | ||
* @return {object} An array with the character or null if isn't a letter | ||
*/ | ||
function isLetter(str) { | ||
return str.length === 1 && str.match(/[a-zA-Z]/i); | ||
} | ||
|
||
/** | ||
* Check if is Uppercase or Lowercase | ||
* @param {String} character - character to check | ||
* @return {Boolean} result of the checking | ||
*/ | ||
function isUpperCase(character){ | ||
if (character == character.toUpperCase()) { | ||
return true; | ||
} | ||
if (character == character.toLowerCase()){ | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Encrypt a Vigenere cipher | ||
* @param {String} message - string to be encrypted | ||
* @param {String} key - key for encrypt | ||
* @return {String} result - encrypted string | ||
*/ | ||
function encrypt(message, key) | ||
{ | ||
|
||
let result = ""; | ||
|
||
for (let i = 0, j = 0; i < message.length; i++) { | ||
let c = message.charAt(i); | ||
if (isLetter(c)){ | ||
if(isUpperCase(c)) { | ||
result += String.fromCharCode((c.charCodeAt(0) + key.toUpperCase().charCodeAt(j) - 2 * 65) % 26 + 65); // A: 65 | ||
} else { | ||
result += String.fromCharCode((c.charCodeAt(0) + key.toLowerCase().charCodeAt(j) - 2 * 97) % 26 + 97); // a: 97 | ||
} | ||
} else { | ||
result+=c; | ||
} | ||
j = ++j % key.length; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Decrypt a Vigenere cipher | ||
* @param {String} message - string to be decrypted | ||
* @param {String} key - key for decrypt | ||
* @return {String} result - decrypted string | ||
*/ | ||
function decrypt(message, key) | ||
{ | ||
let result =""; | ||
|
||
for(let i = 0, j = 0; i < message.length; i++){ | ||
let c = message.charAt(i); | ||
if (isLetter(c)){ | ||
if(isUpperCase(c)) { | ||
result += String.fromCharCode(90-(25-(c.charCodeAt(0)-key.toUpperCase().charCodeAt(j)))%26); | ||
} else { | ||
result += String.fromCharCode(122-(25-(c.charCodeAt(0)-key.toLowerCase().charCodeAt(j)))%26); | ||
} | ||
} else { | ||
result+=c; | ||
} | ||
j = ++j % key.length; | ||
} | ||
return result; | ||
} | ||
|
||
let messageEncrypt = encrypt('Hello World!', 'code'); | ||
console.log(messageEncrypt); // "Jhpnr Yrvng!" | ||
|
||
let messageDecrypt = decrypt('Jsopq Zstzg!', 'code'); | ||
console.log(messageDecrypt); // "Hello World!" |