# USE THIS WORKSHEET TO DO THE IN CLASS PROJECT on 11/2/17. If you don't finish it in class finish it before Class on Tuesday!

## Copy the functions powmod, binary and fermat from your SMC Assignment 4. If you don't have them, get them from someone else!

## Check that your functions work by testing whether 8675309 and 8675307 are prime using the fermat function and several different bases. Which one is prime?

## Try out the command randint(a,b) which generates a random number between a and b.

## Use randint and your fermat code (you may need to use a while loop or something else!) to generate two random prime numbers p and q with 60 digits.

## Use p and q to compute n and phi(n).

## Sage has a built in xgcd command you can use to compute inverses. Test it out by using it to compute the inverse of 17 mod 67. (Note: you should get 4)

## Now use your xgcd function in the same way to compute your decryption exponent, by finding the inverse of e modulo phi(n)

## Now use the info in the spreadsheet to send others in the class a message. Use the following method to convert letters into numbers: let a->00 b->01 ... z->25 and write a message by concatenating the digits. So "HI" becomes 0708=708, Write a message with 6-10 letters in it. Then encrypt it using someone else's public key (You'll need the powmod function again) in the spreadsheet and send it to them by pasting it in columns D or E.

## Once someone sends you an encrypted text (ask someone else to if no one has) decrypt the message by raising the message they sent to your secret decryption key mod n. Verify that the message actually decrypts to someones name!