Day 9: Elliptic Curves
Resources (optional)
- lecture notes (Saba Eskandarian; Stanford CS 355, Spring 2021): Saba’s notes
Lecture Topics
- notes
- elliptic curves
- hobbies in antiquity
- finding rational points
- constructing a group
Problems
Today, we’ll move away from Replit and program on our own computers.
You’ll see that the programs we’ve been writing (encryption, decryption, key generation, signatures, etc) can be used on your own files!
We’ll be working in the command prompt/terminal. You’ll need the following commands on Linux/Mac (windows in parentheses):
ls
(windows:dir
): list files in current directoryecho TEXT > FILE
(windows:echo TEXT > FILE
): writeTEXT
toFILE
diff FILE1 FILE2
(windows:fc /B FILE1 FILE2
): compare two files and print something if they differcat FILE
(windows:print FILE
): print a filerm FILE
(windows:del FILE
): remove a filepython3 FILE ARG1 ARG2 ..
(windows:python3 FILE ARG1 ARG2 ..
): run the python scriptFILE
with argumentsARG1 ARG2 ...
You’ll be working in cryptotool.py
. Download it here
and put it on your desktop. You’ll be writing you code in the main
function.
Look for the # TODO
comments.
These functions might be helpful:
write_bytes_to_file(bytes, file)
: creates a new file and writes bytes to itread_bytes_from_file(file)
: read the contents of a file, as bytesany_object_to_bytes
: formats any Python object as bytesany_object_from_bytes
: the inverse of the above
Each problem today involves implementing a new subcommand for cryptotool.py
that will allow it to do some cryptographic operation. Usually, the command
will take command like arguments, and may read/write files.
For example, once you’ve implemented the keygen
, enc
, and dec
subcommands, you should be able to run the following in your terminal:
python3 cryptotool.py keygen -k mykey # creates a new file 'mykey' with a random key
echo hi there > mymsg # creates a new file 'mymsg' with 'hi there'
python3 cryptotool.py enc -k mykey -m mymsg -c myciphertext # creates a new file 'myciphertext'
python3 cryptotool.py dec -k mykey -m mydecodedmsg -c myciphertext # creates a new file 'mydecodedmsg'
cat mydecodedmsg # prints out that message!
I recommend doing the problems in this order:
- Symmetric key generation (required):
python3 cryptotool.py keygen -k FILE
: generate a random symmetric key and store it in a file- use
random_bytes
- use
python3 cryptotool.py passwd-to-key -p PASSWORD -k FILE
: hash a password to a key (and store it in a file)- encode the password as bytes using
passwd.encode('ascii')
and then hash it
- encode the password as bytes using
- Symmetric ciphers (required):
python3 cryptotool.py enc -k FILE -m FILE -c FILE
: encryptpython3 cryptotool.py dec -k FILE -m FILE -c FILE
: decrypt
- Decoding your secret message! (optional)
- Public-Key encryption: (optional)
python3 cryptotool.py keygen -sk FILE -pk FILE
: generate a keypairpython3 cryptotool.py enc -pk FILE -m FILE -c FILE
: encryptpython3 cryptotool.py dec -sk FILE -m FILE -c FILE
: decrypt- now, try encrypting a message to yourself and then decrypting it!
- Signatures: (optional)
python3 cryptotool.py sign -sk FILE -m FILE -s FILE
: signpython3 cryptotool.py verify -pk FILE -m FILE -s FILE
: verify, prints ‘True’ or ‘False’- now, try creating a signatures and then checking it!
Fun things to do:
- Try encrypting some of your normal files (e.g. MS word files) and then decrypting them. Can you open the decrypted files?
- send a message to your group-mates using a password-derived key