├── README.md ├── Screen Shot 2018-03-12 at 6.20.14 AM.png └── ceasarCipher ├── js ├── ceasar.js └── main.js └── notes ├── ascii : ceaser cipher.rtf └── ceasarCipherNotes.rtf /README.md: -------------------------------------------------------------------------------- 1 | # Caesar Cipher Shift Encoder 2 | If you're interested in learning Cryptography, the Caesar Cipher is a great example to start with. If you're a person who enjoys ancient history and tradition, the Caesar cipher is one of the oldest and simplest forms of encrypting a message. It is also named after Julius Caesear who, according to Suetonius (a Roman historian), used it with a shift of 3 to encrypt military messages. 3 | 4 | ![alt tag](https://github.com/karina001/caesarCipher/blob/master/Screen%20Shot%202018-03-12%20at%206.20.14%20AM.png) 5 | 6 | ## How It Works: 7 | 8 | **Tech used:** JavaScript 9 | 10 | Essentially, the Caesear cipher is a type of substitution, where each letter in an original message (which in cryptography is called "plaintext"), is replaced with another letter. The letter that replaces the plaintext letter, is derived from a shift of N number, that shifts up or down the alphabet (in this case, English alphabet). 11 | 12 | For each letter of the alphabet, you would take its position in the alphabet, say 3 for the letter 'C', and shift it by the N number. If we had a key of +3, that 'C' would be shifted down to an 'F' - and that same process would be applied to every letter in the plaintext. In doing this, we convert a message that initially was readable, to a form that cannot be understood at first glance. 13 | 14 | To work with the number shift and conduct math operations more easily, I leveraged the Ascii values for each character in the alphabet. To start, the idea was to allow a user to input 2 arguments into a function: the first argument takes in a word or letter, and the second sets the number of shifts. This function subsequently converts the word or letter into its Ascii value to evaluate whether the letter should be uppercase or lowercase (based on the shift number) and apply the shift in the right direction. The next step was to create a function that actually encrypts the message by applying the shift, and the final step was to create a function that decrypts the message by subtracting the shift. Most of the code for this program can be viewed in the screen shot above. 15 | 16 | ## Optimizations 17 | When I have more time, I'd like to go back and actually add an interface so users can interact with the cipher and encrypt or decrypt their messages for fun. I plan on adding HTML / CSS to incorporate styling and complete the project from a 360 perspective. At the moment, there is no live demo but the .js file can be copied and pasted into a https://jsbin.com/ or https://repl.it/ if you want to give it a go! 18 | 19 | ## Lessons Learned: 20 | In completing this project I learned a lot about Cryptography in terms of its general history, future and use cases. I also enjoyed getting my feet in python when writing this program. 21 | 22 | ## Examples: 23 | Take a look at other examples that I have in my own portfolio: 24 | 25 | **Spanish Colors Memory Game:** https://github.com/karina001/spanishColorsMemoryGame 26 | 27 | **WuTangClan Name Generator:** https://github.com/karina001/toDoList 28 | 29 | **Speech Recognition App:** https://github.com/karina001/speechRecognitionApp 30 | -------------------------------------------------------------------------------- /Screen Shot 2018-03-12 at 6.20.14 AM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/girl4tech/caesarCipher/dc9f42277317fdc965b01bef15bbeb01202e7e63/Screen Shot 2018-03-12 at 6.20.14 AM.png -------------------------------------------------------------------------------- /ceasarCipher/js/ceasar.js: -------------------------------------------------------------------------------- 1 | function shift_letter(letter, shift){//parameter variables are defined when the function is called 2 | //convert letter to ascii 3 | // - capital letters stay as capital letters 4 | // - lowercase letters stay as lowercase letters 5 | // - all other characters stay as themselves 6 | var asciiVal = letter.charCodeAt(0);//gives us the ascii value of the character 7 | //check if uppercase 8 | if(asciiVal >= 65 && asciiVal <= 90){ 9 | //letter is uppercase, do some math 10 | asciiVal = ((asciiVal - 65 + shift) % 26); 11 | if (asciiVal < 0) asciiVal = 91 + asciiVal; 12 | else asciiVal += 65; 13 | } 14 | if(asciiVal >= 97 && asciiVal <= 122){ 15 | //letter is lowercase, do other math 16 | asciiVal = ((asciiVal - 97 + shift) % 26); 17 | if (asciiVal < 0) asciiVal = 123 + asciiVal; 18 | else asciiVal += 97; 19 | } 20 | //return char representation of ascii_val 21 | return String.fromCharCode(asciiVal);//gives us the character from the ascii value 22 | } 23 | 24 | function code(text, shift){ 25 | var codedText = ""; 26 | for (var i=0; i= 65 && asciiVal <= 90){ 9 | //letter is uppercase, do some math 10 | asciiVal = ((asciiVal - 65 + x) % 26) + 65; 11 | } 12 | //check if lowercase 13 | if(asciiVal >= 97 && asciiVal <= 122){ 14 | //letter is lowercase, do other math 15 | asciiVal = ((asciiVal - 97 + x) % 26) + 97; 16 | } 17 | //return char representation of ascii_val 18 | return String.fromCharCode(asciiVal);//gives us the character from the ascii value 19 | } 20 | 21 | function code(text, shift){ 22 | var codedText = ""; 23 | for (var i=0; i= 65 or ascii_val <= 90): 50 | # a. Do some math 51 | ascii_val = ((ascii_val - 65 + shift ) % 26) + 65 52 | else: 53 | # b. Letter is lowercase. do other math 54 | ascii_val = ((ascii_val - 97 + shift ) % 26) + 97 55 | 56 | # Return char representation of ascii_val 57 | return chr(ascii_val) 58 | 59 | 60 | def code( text, shift ): 61 | # 1. Start with empty string 62 | coded_text = "" 63 | 64 | # Loop over each letter in text 65 | for letter in text: 66 | coded_text += shift_letter(letter, shift) 67 | return coded_text 68 | 69 | 70 | 71 | def decode( text, shift ): 72 | # 1. Start with empty string 73 | coded_text = "" 74 | # 2. Since we are decode, we want to shift in opposite way 75 | shift = shift * -1 76 | 77 | # 3. Loop over letter and decode 78 | for letter in text: 79 | coded_text += shift_letter(letter, shift) 80 | 81 | return coded_text 82 | */ 83 | -------------------------------------------------------------------------------- /ceasarCipher/notes/ascii : ceaser cipher.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470 2 | {\fonttbl\f0\fswiss\fcharset0 Helvetica;} 3 | {\colortbl;\red255\green255\blue255;} 4 | \margl1440\margr1440\vieww10800\viewh8400\viewkind0 5 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 6 | 7 | \f0\fs24 \cf0 write alphabet in an array \ 8 | project euler - 44 & 78\ 9 | PE - 66 diophantine\ 10 | PE - 68 N-GON\ 11 | numerical lowest external node\ 12 | \ 13 | 1-10\ 14 | 10,9,8;7,6,5;4,3,2;\ 15 | \ 16 | //user see\ 17 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 18 | \cf0 see her toppings placed on her pizza in real time (as toppings get added)\ 19 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 20 | \cf0 \ 21 | //user expect\ 22 | ability to select toppings from 3+ options\ 23 | ability to choose if toppings go on one half or other half of pizza\ 24 | \ 25 | //user do \ 26 | select choice of toppings from at least 3 options\ 27 | \ 28 | cheese\ 29 | pepperoni\ 30 | mushroom\ 31 | green pepper\ 32 | pine apple\ 33 | olives\ 34 | onions\ 35 | banana peppers} -------------------------------------------------------------------------------- /ceasarCipher/notes/ceasarCipherNotes.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470 2 | {\fonttbl\f0\fswiss\fcharset0 Helvetica;} 3 | {\colortbl;\red255\green255\blue255;} 4 | \margl1440\margr1440\vieww25100\viewh13320\viewkind0 5 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 6 | 7 | \f0\fs24 \cf0 //input\ 8 | s = "cat"\ 9 | shift_val = 1\ 10 | \ 11 | // Algorithm to code string s given shift_val \ 12 | // For each letter in s:\ 13 | // 1. convert letter to ascii\ 14 | // 2. add shift_val to ascii code\ 15 | // 3. convert ascii code back to letter \ 16 | // 4. Once loop is done, output new coded string \ 17 | \ 18 | \ 19 | //output\ 20 | coded_s = "dbu"\ 21 | \ 22 | // Clue, how to loop over a string\ 23 | string1 = "cats"\ 24 | for(x = 0; x < string1.length; x++)\{\ 25 | console.log(string1[x])\ 26 | \}\ 27 | \ 28 | // Clue 2, how to build a new string\ 29 | s = ""\ 30 | s2 = "b"\ 31 | s3 = "c"\ 32 | s = s2 + s3\ 33 | console.log(s)\ 34 | \ 35 | // convert string to ascii\ 36 | "a".charCodeAt(0)\ 37 | \ 38 | // convert ascii to string\ 39 | String.fromCharCode(97)} --------------------------------------------------------------------------------