pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that...

40
pset 2: Crypto Zamyla Chan | [email protected]

Transcript of pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that...

Page 1: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

pset 2: Crypto

Zamyla Chan | [email protected]

Page 2: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

0. A Section of Questions 1. Caesar 2. Vigenère

pset 2

Page 3: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Toolbox

  sudo  yum  –y  update  

  ASCII Chart

  Command Line Arguments

  Arrays

  Strings

Page 4: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

ASCII Chart

A B C D E F G H I ...

65 66 67 68 69 70 71 72 73 ...

a b c d e f g h i ...

97 98 99 100 101 102 103 104 105 ...

Page 5: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Caesar

Page 6: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

k = 0

A A B B C C ... X X Y Y Z Z

img src: http://www.maths-resources.net

Page 7: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

k = 3

A D B E C F ... X A Y B Z C

img src: http://www.maths-resources.net

Page 8: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Example

./caesar  3  ABCDEFGHIJKL  DEFGHIJKLMNO  

./caesar  3  This  is  CS50!  Wklv  lv  FV50!  

Page 9: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

TODO

  Get the key  2nd command line argument

Page 10: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

argc, argv

int  main  (int  argc,  string  argv[])    argc

 int  the number of arguments passed

  argv  array of strings  the list of arguments passed

Page 11: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Arrays

  Data structures that hold multiple values of the same type

  Entries are zero-indexed

[0] [1] [2] [3]

Page 12: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Creating an Array

double  mailbox[3];  mailbox[0]  =  1.2;  mailbox[1]  =  2.4;  mailbox[2]  =  4.3;  

1.2 2.4 4.3

[0] [1] [2]

Page 13: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

argc, argv

./hello  David  Malan    argc 3   argv[0] “./hello”   argv[1] “David”   argv[2] “Malan”

Page 14: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

args.c

Page 15: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

TODO

  Get the key  In the command line argument  Check for correct usage

 They must give you one key! No more, no less.  Convert key to an int

Page 16: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

atoi

  Converts a string to an integer

string  num  =  "50";    int  i  =  atoi(num);    

i 50

  Not part of stdio or cs50 libraries...  man  atoi

Page 17: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

TODO

  Get the key   Get the plaintext

 GetString()  All user inputted strings are “correct”

Page 18: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

TODO

  Get the key   Get the plaintext   Encipher

Page 19: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Strings

  A string is just an array of characters

string  text  =  "This  is  CS50";  

 text[0] T   text[1] h   text[2] i   etc...  

Page 20: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Length of String

string  text  =  "This  is  CS50";  int  length  =  strlen(text);  

  How would you iterate over each character in a string?  strlen.c

Page 21: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

k = 6

I   '   m   d   i   z   z   y   !  

73 39 109 32 100 105 122 122 121 33

O  

79

O   '  

79 39

Page 22: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Reminders

  Only encipher letters  non-letters (symbols) remain unchanged

  Preserve capitalization

Page 23: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

isalpha, isupper, islower

bool  alpha  =  isalpha('T');  alpha true

bool  upper  =  isupper('h');  upper false

bool  lower=  islower('i');  lower true

Page 24: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

ASCII Math

  Enclose a character with a single quote to use its ASCII number  ‘C’ 67  ‘S’ 83  ‘f’ 102

  You can subtract, add, etc.  asciimath.c

Page 25: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

k = 6

I   '   m   d   i   z   z   y   !  

73 39 109 32 100 105 122 122 121 33

O   '   s   j   o   Ç   Ç  

79 39 115 32 106 111 128 128

Page 26: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Reminders

  Only encipher letters  Non-letters (symbols) remain unchanged

  Preserve capitalization   Wraparound the alphabet

 Stay within A-Z and a-z  Letters can’t become symbols

Page 27: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Modulus %

  x % y gives the remainder of x / y

int  ans1  =  27  %  15;  ans1 12

int  ans2  =  17  %  2;  ans2 1

Page 28: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Modulus %

  What’s “leftover” after you subtract y from x as many times as possible

  Useful for wrapover. How?

Page 29: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

k = 6

I   '   m   d   i   z   z   y   !  

73 39 109 32 100 105 122 122 121 33

O   '   s   j   o   f   f   e   !  

79 39 115 32 106 111 102 102 101 33

Page 30: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Enciphering

  ci : ith letter of ciphertext

  pi : ith letter of plaintext

  k : key   % 26

ci = (pi + k) % 26

Page 31: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

‘z’ ‘f’

z = 122 f = 102

(‘z’ + 6) % 26 = 24

  ... Should be 102 for ‘f’

ASCII Values Alphabetical Index

z = 25 f = 5

(25 + 6) % 26 = 5

  ... This makes sense, but it still isn’t ‘f’

Page 32: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

TODO

  Get the key   Get the plaintext   Encipher   Print ciphertext

Page 33: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Vigenère

Page 34: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Vigenère

  Get the keyword   Get the plaintext   Encipher   Print ciphertext

Page 35: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Vigenère

  Similar to Caesar! Except the shift is represented by the keyword letters  ‘A’ and ‘a’ 0  ‘F’ and ‘f’ 5  ‘Z’ and ‘z’ 25

Page 36: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

ci = (pi + kj) % 26

  ci : ith letter of ciphertext   pi : ith letter of plaintext   kj : jth letter of the key   %26

Page 37: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Vigenère

jharvard@appliance  (~/Dropbox/pset2):  ./vigenere  ohai  

This...  is  CS50!  

Hoia...  wz  CA50!  

  keyword in the command line  Already a string

  Preserve capitalization   Only encipher letters

Page 38: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

./vigenere  ohai  

T  h  i  s  .    .  .   i  s   C  S  5  0  !  

+  +  +  +   +  +   +  +  

o  h  a  i   o  h   a  i  

⇩  ⇩  ⇩  ⇩   ⇩  ⇩   ⇩  ⇩  

H  o  i  a  .    .  .   w  z   C  A  5  0  !  

Page 39: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

Reminders

  Only advance to the next letter in the keyword if the character in plaintext is a letter

  Wraparound to beginning of keyword when end of keyword

  Keep track of:  position in plaintext  position in keyword

Page 40: pset 2: Crypto - CS50cdn.cs50.net/2012/fall/psets/2/walkthrough2.pdf · Arrays Data structures that hold multiple values of the same type Entries are zero-indexed [0] [1] [2] [3]

this was walkthrough 2