modulo - Can someone explain to me how ( (c-65+k)%26)+65) works in a caesar cypher? -


if c numerical value of uppercase character (i.e. b 66) , sake of argument, k key value of 2? i'm new programming , don't understand how modulo works in this. know takes value of remainder, wouldn't simplify this?

c = b = 66 k = 2 imagine result should 'd'  (66 - 65 +2)%26 +65  (3)%26 +65  0 + 65  65 = 'a' 

i must not understand way % works.

key fact - ascii code of letter"a" 65.

here how cypher works - original expression in question title.

  1. take ascii value of letter, subtract value of "a" giving 0 based number.
  2. add key value number shifting k places.
  3. now divide number got above 26, discard quotient , use remainder. modulo operator %. keeps numbers in 0-25 range, since dividing 26 never have remainder great 25.
  4. add 65 convert "encrypted" uppercase letter.

this allows key number , still keeps "encrypted" output within ascii range of a-z.

you interpreting % operator division. in reality, it's modulo or forget-the-quotient-i want-the-remainder operator.

example

  1. 0%2 0
  2. 1%2 1
  3. 2%2 0
  4. 3%2 1

and on. modulo cyclic.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -