If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Course: Computer science theory>Unit 2

Lesson 2: Ciphers

Ciphers vs. codes

To begin, let’s make sure we understand the difference between a cipher and a code. Actually, I dare you to get up and go ask someone the same question right now. While you do that I’ll wait here and admire this Lorenz cipher machine...
Did they stumble around for an answer? For most people, it’s as if you asked them what the difference is between mix and blend. Tough question. Luckily, we have a video on Morse Code which introduces the idea of a codebook—check it out! In the video we see how telegraph operators could save time by mapping entire sentences to shorter words.
Here, the word accountant is code for "Come at once. Do not delay." A code is a mapping from some meaningful unit—such as a word, sentence, or phrase— into something else—usually a shorter group of symbols. For example, we could make up a code where the word apple is written as 67. Generally codes are ways of saving time, and when sending messages around the world, time is money.
A codebook is simply a list of these mappings. Codebooks have been around ever since we began writing. Just remember,a code requires a codebook.
Most importantly, ciphers do not involve meaning. Instead they are mechanical operations, known as algorithms, that are performed on individual or small chunks of letters. For example, in the Caesar Cipher we saw how each letter in the alphabet was mapped to a different letter. For example, A→D,  B→E, and C→F, when we're using a shift of four. This kind of cipher is known as a shift cipher.
Review how this works using the interactive tool below:
In this case, we don’t need a codebook. Instead, we follow a series of instructions—also known as an algorithm—where we shift each letter by a certain number. The algorithm requires one piece of shared information known as a key. In the example above where A→D, the key is four. This shared key is required for two parties to encrypt messages: HELLO → KHOOR, and decrypt messages: KHOOR→HELLO.
So back to our question: What is the difference between codes and ciphers? Codes generally operate on semantics, meaning, while ciphers operate on syntax, symbols. A code is stored as a mapping in a codebook, while ciphers transform individual symbols according to an algorithm.
Now, let’s review the mechanics involved in the Caesar Cipher in the next exercise.

Want to join the conversation?

• I have heard of people in the military doing code braking but do people actually get pay to that for a living? And what are they called ?
• There are a lot of people who get paid to break codes. Just think about all the possible reasons people and organisations may have to try to keep their messages secret. The military and terrorists are given. Governments. Think about who would be interested in what they are trying to hide. Companies trying to keep business secrets, other companies trying to steal business secrets. Spies. Criminals. Paranoid people.

Tens of thousands of highly skilled mathematicians and computer scientists spend their entire careers on stuff like that.

It's a HUGE field, and without a doubt a colossal waste of human talent. A lot of it has to be done, though. It's a fact of life.
• I totally don't get can any one help me understand please
• A code involves simple translation. "ABC" is code for "Hello There!"
A cipher is like a mathematical function, you input something and get an output. I input "ABC" into the cipher and it outputs "Hello There!".
Code is more direct, you just look up what something means. A cipher is a way of translating the code without having everything listed out.
• how come when you type in your name, and then type in the output, the 2nd output is not your name

e.g if i type in 'clare' and get 'lujan'
if i type in 'lujan' i don't get 'clare' ?

please let me know if you have the answer to this question. if you don't get what i mean, or you don't know, please don't answer, just comment.
Thanks!
- a KA user.
• To return back to clare the system adheres via the following equation I devised (don't know if it has been already discovered by other mathematicians) : E=26/S where E is the number of entries required to return back to initial input and S is the shift used, which is assumed to be constant. When we talk about entries we mean that we enter our initial state, A for example, into our Caesar cipher machine , and get B, then we enter B into the Caesar cipher and so on for the different cases i.e enter input receive output then enter output in a sort of feedback loop. For example to have the situation where clare goes to lucan then back to clare we count that 2 entries were required for this; thus rearranging gives S=26/E or S=26/2 which gives us a shift of 13 that would be required to obtain the situation desired. More generally however this equation works in all situations as if we have a shift of 4 giving us a decimal answer for the number of entries required of 6.5. As we can't have 6.5 entries we must multiply 6.5 by 1 then ,individually- without the 1- by 2 to give us 6.5 and 13, if the answer is a whole number then that is our answer, if it is a decimal then we multiply by the individual natural number until we receive a whole number which will be our answer. In the case of shift 4 the answer would be 13 entries to return back to the initial input. To put it another way the equation E=26/S can be a whole number, decimal number or fractional number and if E= a fraction represented by 26/S then multiply by 1, 2, 3, 4, 5 .... and so on until it turns into a whole number which will be your answer.Thus from observation it is taken that the number of entries required to return back to the initial state will always be less than or equal to 26 i.e E<= 26.
• So basically a cipher has a specific rule, and that rule would work for whatever things you put into that message,
and a code would be a symbol/word/number that represents a meaning, right?
• Basically a cipher changes the literal characters typed in based on some algorithm while a code shortens linguistic constructs based on their meaning.
• how come I never get votes, and this is so cool.
• What? Wdym?
(1 vote)
• So any time you write code for a computer program it is automatically considered an algorithm?
• An algorithm is a set of steps required to complete a task. So an algorithm for you going to work in the morning would include stuff like, waking up, eating breakfast, brushing your teeth, getting in your car, etc., etc.

When computer code is written, every step has to be laid out for the computer because (without the use of 1000s of IF statements or advanced AI) it can't make any real decisions on its own. For instance, if the algorithm for you driving to work described a step to turn right on a given street, the computer will make that turn, even if the street is blocked for construction or an accident, because it won't deviate from the algorithm unless being programmed to. I hope this general example and simplified explanation helps.
• Who has read 'The Davinci code' or 'Angels and Demons'?( Both books by Dan Brown.) I love how both books are full of codes that show the past, give clues to the present, and show the future. Vote if you agree and if you think these books and the author who wrote them are A M A Z I N G :D :D
• I think I watched the videos. They're pretty interesting, like you say. I wasn't interested in the New Testament but I was using it to learn Greek and got a very strong impression about Christ from the Gospels. The church we have now is only one version of many that have disappeared. All that history is interesting and important. Elaine Pagels is a very good historian and writer on this topic and I recommend her many books.
• People say cracking a code when it is actually craking a cipher.