Shift cipher (article) | Ciphers | Khan Academy (2024)

Want to join the conversation?

Log in

  • Ken Sommerville

    11 years agoPosted 11 years ago. Direct link to Ken Sommerville's post “I wrote a small vbscript ...”

    I wrote a small vbscript program to perform the modulo shift, and for most cases it worked.
    However, if the input minus the shift value results in a negative number, vbscript's mod function outputs a negative remainder. The work around I had to implement was ultimately found from a quote from news:borland.public.delphi.objectpascal which stated: "Henrick Hellström wrote: I would strongly prefer that (X mod Y) > 0 if Y > 0. The expression (((X mod Y) + Y) mod Y) one has to write from time to time just looks awful!"

    This really seems like a hack to get the mod function to work, however, I noticed in several google searches, that not every programming language treats modulo the same. SO if you try to code up a encrypt/decrypt routine based on the mod function, you've been warned.

    (25 votes)

    • Cameron

      11 years agoPosted 11 years ago. Direct link to Cameron's post “Here's an alternate appro...”

      Here's an alternate approach.

      Since A mod B is the remainder R when we divide A by B and all integers can be written as A=B*Q+R (where Q is the quotient which is floor(A/B) )

      A mod B is:
      A-floor(A/B)*B

      Without getting too deep into it, the quirky behavior behind mod in many programming languages has its roots in how computers represent negative numbers and how integer division is done on computers (truncating integer division).

      Hope this makes sense

      (8 votes)

  • KHAN ACE

    10 years agoPosted 10 years ago. Direct link to KHAN ACE's post “since there are 26 letter...”

    since there are 26 letters in the alphabet
    Why isn't A= 1, B = 2, C = 3 ... Y = 25, Z = 26?

    (9 votes)

    • Lucie le Blanc

      10 years agoPosted 10 years ago. Direct link to Lucie le Blanc's post “In cryptography, we use A...”

      Shift cipher (article) | Ciphers | Khan Academy (7)

      In cryptography, we use A=0 ... Z=25. This makes sense, because if a shift is 0 you can assume that the letters were shifted by 0, which means they were shifted by nothing. Any letter shifted by 0 is that letter. Think of it like addition: you are adding a number. Any number + 0 is that number, therefore A+0 is A, right?
      Also, if we used A=1, it would get very confusing because some people would think that A shifted by 1 is A and others would disagree. This would make it very hard to decrypt an encrypted message because you would have to decrypt the message twice, once for each possibility!

  • mike boy

    10 years agoPosted 10 years ago. Direct link to mike boy's post “I may be missing somethin...”

    I may be missing something. Under 'decryption', using k=19, 19 is subtracted from the cryptic message (3, 0, 19, 6) to give the result of -16, -19, 0, -13. Under that result, a new result of 10, 7, 0 13 is provided to give us the message 'khan'. Specifically, I'm unclear how -16 & -19 converted to 10 and 7 to give us the letters k & h in the message. Please explain and clarify this result.

    (14 votes)

    • John Devoy

      8 years agoPosted 8 years ago. Direct link to John Devoy's post “I wasted hours wondering ...”

      I wasted hours wondering why i couldn't decrypt properly, turns out c# doesn't have a proper modulo command, luckily i found this. My c# code was ignoring % when used with negative values.
      float nfmod(float a, float b)
      {
      return a - b * Mathf.Floor(a / b);
      }

      (5 votes)

  • Roger Andersson

    6 years agoPosted 6 years ago. Direct link to Roger Andersson's post “Decryption. How does divi...”

    Decryption. How does dividing -16 by 26 give us a remainder of 10?
    26 goes into -16 how many times?

    I understand the logic in the encryption when the numerator is positive, but I'm lost in the decryption.

    (5 votes)

  • Timothy (Read my Bio) #ThanksPamela

    8 years agoPosted 8 years ago. Direct link to Timothy (Read my Bio) #ThanksPamela's post “I am still really confuse...”

    I am still really confused on how to do this

    (5 votes)

    • >: #BringBackBackgrounds

      10 months agoPosted 10 months ago. Direct link to >: #BringBackBackgrounds's post “What are you confused abo...”

      What are you confused about?

      (1 vote)

  • Patrick Vermeulen

    8 years agoPosted 8 years ago. Direct link to Patrick Vermeulen's post “Correct me if i'm wrong. ...”

    Correct me if i'm wrong. But with mod=26, and A=0; you end up with 27 alphabetical characters...

    (2 votes)

    • Cameron

      8 years agoPosted 8 years ago. Direct link to Cameron's post “Sorry, you're wrong. An i...”

      Sorry, you're wrong. An integer mod 26 will give you integers from 0-25 inclusive, for a total of 26 possible numbers.

      (8 votes)

  • Medha_V

    8 years agoPosted 8 years ago. Direct link to Medha_V's post “In the encryption proces...”

    In the encryption process, mod 26 is mentioned. How does that apply to encrypt the message. For example, in 29 mod 26, how is the 3 obtained? Also, how is 19 mod 26, 19?

    (1 vote)

    • Neil And Chase

      8 years agoPosted 8 years ago. Direct link to Neil And Chase's post “The mod operator gives th...”

      The mod operator gives the remainder of the top number divided by the bottom. For example, if we want 10 mod 3, we will start by dividing and getting 10 / 3 = 3 R 1 (read: 3 remainder 1) because the 10 is 1 over being divisible by 3 (9 is divisible by 3 and 1 less than 10). The remainder that we get, in this case, 1, is mod, so 10 mod 3 = 1.
      For the examples you gave:

      29 mod 26
      29 / 26 = 1 R 3 because 29 = 1*26 + 3
      ^
      This is the mod
      29 mod 26 = 3

      And
      19 mod 26
      19 / 26 = 0 R 19 because 19 = 0*26 + 19
      ^
      This is the mod
      19 mod 26 = 19

      Hope this helps!
      Neil

      (7 votes)

  • Sam D

    8 years agoPosted 8 years ago. Direct link to Sam D's post “I understand that the cae...”

    I understand that the caesar cipher can be broken in just 26 or less tries but here, in this shift cipher we're taking the letter positions in the alphabet and then adding it to a shift number. next we use modulus 26. How is the Cryptographer going to know it was mod 26 and not mod 24 or 22? Or, how is he even going to know we did something like Modulus? And, can't we use mod 22 or any other number closer to 26?

    (3 votes)

    • CZ

      8 years agoPosted 8 years ago. Direct link to CZ's post “Here are some issues to t...”

      Here are some issues to think about. What symbols will you use to send the text? The reason we use modulus 26 is we are limited to only 26 recognizable symbols to use. We could expand that list, however, the difficulty to guess it only increases linearly...and we have introduced more symbols which may make it more difficult to decrypt at the other end for the intended person. A similar problem arises when we use a smaller modulus. If we have only 22 symbols to use then we can really only encrypt 22 letters. What about the other 4? Also this makes it easier to check. In theory an interceptor would probably be already trying 26 shift keys. So 22 does not make the message more secure. With regards to the knowing we are using a modulus. You are right, he might not know that, but it would not be very hard to guess that we are using a shift key and see if the message decrypts. In the age of computers, he could write a simple program to try all 26 shift possibilities (or 110 if you want to add more symbols) and see if any make intelligible print.

      (3 votes)

  • shrumms

    10 years agoPosted 10 years ago. Direct link to shrumms's post “If I were to decrypt B us...”

    If I were to decrypt B using k=3:
    B=2, so 2- 3 = -1, mod(-1); wouldn't the answer still be -1? not 25 which is Z, the correct answer?
    I'm sorry for this noob question.

    (2 votes)

    • M A

      10 years agoPosted 10 years ago. Direct link to M A's post “Maybe I understand you wr...”

      Maybe I understand you wrong, but you have to calculate (2-3) mod 26 for your example. That is -1 mod 26 = 25 => Z.

      (3 votes)

  • galvanek.m

    9 years agoPosted 9 years ago. Direct link to galvanek.m's post “Why we cannot comment als...”

    Why we cannot comment also under the exercises or games? It would be more convenient to discuss possible setbacks right there, won´t it?

    (3 votes)

    • jon.email

      9 years agoPosted 9 years ago. Direct link to jon.email's post “I have a feeling it might...”

      I have a feeling it might be to discourage people from finding the answers they need in the comments. You could click on the "request for features" link on the right if you wanted to ask them though!

      (2 votes)

Shift cipher (article) | Ciphers | Khan Academy (2024)

References

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5355

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.