Created
May 8, 2016 19:44
-
-
Save kalpetros/fc4e562f3271100c9d2cbb51749574e1 to your computer and use it in GitHub Desktop.
ROT13 Cipher Decryptor
This file contains hidden or 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
| function rot13(str) { | |
| var array = []; | |
| var splittedArray = []; | |
| var decryptedArray = []; | |
| array = str.split(' '); | |
| // Split each word in letters and store | |
| // it in a sub-array | |
| for (var i = 0; i < array.length; i++) { | |
| splittedArray.push(array[i].split('')); | |
| } | |
| // Decryption | |
| // Convert each letter in the splittedArray | |
| // to a UTF-16 code and then convert the | |
| // UTF-16 code to a string | |
| for (var j = 0; j < splittedArray.length; j++) { | |
| for (var k = 0; k < splittedArray[j].length; k++) { | |
| splittedArray[j][k] = splittedArray[j][k].charCodeAt(); | |
| // Only get values between 65 and 90 | |
| // 65 = A | |
| // 90 = Z | |
| // Characters stay the same | |
| if (splittedArray[j][k] < 65) { | |
| // Don't shift characters | |
| splittedArray[j][k] = splittedArray[j][k]; | |
| } else if (splittedArray[j][k] <= 77) { | |
| // Subtract value from 65 to get how close is the value from 65 | |
| // and subtract the remaining (13 - value from 65) from 90 in order | |
| // to get the correct UTF-16 code | |
| splittedArray[j][k] = 90 - (12 - (splittedArray[j][k] - 65)); | |
| } else { | |
| // Subtract 13 from each UTF-16 code | |
| splittedArray[j][k] = splittedArray[j][k] - 13; | |
| } | |
| // Convert UTF-16 code to string | |
| splittedArray[j][k] = String.fromCharCode(splittedArray[j][k]); | |
| } | |
| // Join each sub-array letter together and | |
| // push them to the decryptedArray array | |
| decryptedArray.push(splittedArray[j].join('')); | |
| } | |
| // Create a string from the decryptedArray array strings | |
| var decryptedString = decryptedArray.join(' '); | |
| return decryptedString; | |
| } | |
| // Change the inputs below to test | |
| rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment