CS代考 COMP30023: Computer Systems

School of Computing and Information Systems
COMP30023: Computer Systems

Practical Week 5

Copyright By PowCoder代写 加微信 powcoder

1 Introduction
In this workshop, we’ll be looking at key generation, encryption, decryption and generating message digests using
the SHA256 hashing algorithm.
As mentioned in the lectures, it is uncommon to encrypt messages directly using asymmetric cryptography. What
we do instead is:

1. First exchange a symmetric key securely (we will use asymmetric key cryptography for this step)

2. Use this exchanged symmetric key for encrypting the actual messages
In this lab we will follow this process step by step using the openssl tool. This lab is best completed with a partner.

2 Asymmetric Encryption
2.1 Generating RSA Keys
We will first create public and private RSA keys using openssl

1. Generate a private key using 2048 bit RSA.
$ openssl genpkey -algorithm RSA -out rsa-private.pem -pkeyopt rsa_keygen_bits:2048

2. View the private key file contents.
$ cat rsa-private.pem

3. Output the corresponding RSA public key.
$ openssl rsa -pubout -in rsa-private.pem -out rsa-public.pem

4. View the public key file contents.
$ cat rsa-public.pem

5. Share your public key with a friend (e.g. email it to them).

3 Symmetric Encryption
3.1 Generating a secret key
To participate in any encryption/decryption scheme, we must first generate a secret key.

1. Create a 256 bit CBC mode AES key (this is a symmetric key).
$ openssl enc -aes-256-cbc -k secret -P -md sha1

2. Save the string listed as ‘key’ to a file named key.txt.

3. You may want to save the initialisation vector (iv) shown to be used when encrypting your message later.
However, remember that initialisation vectors need to be random and not reused. They however do not need
to be kept secret.

3.2 Exchanging the secret key
Let’s encrypt the symmetric key using RSA, such that our friend can receive a copy of the secret key.

1. Encrypt the symmetric key you generated above using your friend’s public key.
The idea is that only your friend can decrypt the ciphertext containing the secret key (since only they should
have access to their private key file).
$ openssl rsautl -encrypt -in key.txt -pubin -inkey friend-public.pem > aes-encrypted-rsa.dat

2. View the encrypted AES key – the bytes should look like gibberish.
$ xxd aes-encrypted-rsa.dat

3. Email the encrypted AES key to your friend. They should be able to decrypt the key using their private RSA
key. Try to determine the command to decrypt (if you are stuck – see1).

3.3 Encryption
Using the material generated above, we can now encrypt our messages.

1. Create a file message.txt with an interesting message.

2. You may want to use the command $ openssl rand -hex 96 > iv.txt to generate a new IV.

3. Encrypt the file using the AES symmetric key. The command will be something like
$ openssl enc -aes-256-cbc -in message.txt -out ciphertext.enc -K $(cat key.txt) -iv $(cat iv.txt)

4. Send this ciphertext and the IV to your friend.

5. As a challenge, you are now tasked to figure out how to do decryption using the shared secret key (which you
should have obtained in 3.2) and the AES encrypted message (which your friend just sent you).
Hint: $ man openssl enc

4 SHA256 Message Digests
In this section, we create SHA256 message digests to commit messages. To verify the commitment, it is necessary
for an adversary/verifier to reveal the message and hash it to check if the digest is equal to the received digest.

1. Lets reuse our message.txt file from the previous section to produce a sha256 message digest.
$ sha256sum message.txt

2. Create another message file message2.txt and produce a manifest of digests for all the text files.
$ sha256sum *.txt > manifest

3. You can now easily verify all the files have not been tampered with by checking the manifest file.
$ sha256sum -c manifest .

4. Try tampering message.txt and you should see that $ sha256sum -c manifest errors out.

Think about the following scenario and how it relates to the above:
Alice knows Bob’s public key (this is an assumption) and wants to communicate with Bob. After establishing a
connection, how does she know that the party on the other end is Bob?

5 Bonus: putting it all together
Obviously software integrity is very important nowadays and software authors go to great lengths to ensure that
their users actually use untampered copy of their software. Discuss with your demonstrator how you would design
a safe way to distribute software using what you learnt above.

1From your perspective, you should use your private key to decrypt what your friend sent you, i.e.
$ openssl rsautl -decrypt -in aes-encrypted-rsa.dat -inkey rsa-private.pem .

A Sample solutions
#!/bin/bash
# Parties: Alice, Bob
# Goal – Alice wants to securely send a message to Bob

# Bob generates RSA key pair
openssl genpkey -algorithm RSA -out bob-private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in bob-private.pem -out bob-public.pem
# Bob shares his public key bob-public.pem to Alice (and perhaps rest of the world)

# Alice generates secret key
aes_out=$(openssl enc -aes-256-cbc -k secret -P -md sha1)
echo “$aes_out” | grep “key=” | sed “s/key=//” > alice-secret.txt

# Alice encrypts secret key with Bob’s public key (which Bob sent), Alice sends ciphertext to Bob
openssl rsautl -encrypt -in alice-secret.txt -pubin -inkey bob-public.pem > aes-encrypted-rsa.dat
# Bob decrypts ciphertext from Alice with Bob’s private key, gets secret key
openssl rsautl -decrypt -in aes-encrypted-rsa.dat -inkey bob-private.pem > bob-secret.txt

# Alice encrypts message with secret key and new IV, IV is shared with Bob
echo “Hello World!” > message-alice.txt
openssl rand -hex 96 > iv.txt
openssl enc -aes-256-cbc -in message-alice.txt -out ciphertext.enc \

-K “$(cat alice-secret.txt)” -iv “$(cat iv.txt)”

# Bob decrypts message with the same secret key and IV
openssl enc -aes-256-cbc -d -in ciphertext.enc -K “$(cat bob-secret.txt)” \

-iv “$(cat iv.txt)” -out message-bob.txt

# The messages should match
diff message-alice.txt message-bob.txt
echo “Messages match, all good!”

Alice can send a nonce (number only used once) as the message, encrypted with Bob’s public key.
Bob sends the decrypted message back, which Alice can then compare (e.g. with SHA256 checksum) to verify
whether she’s talking to Bob.

The followup question: What if Alice didn’t have Bob’s public key beforehand? How does she know that the public
key that she has is Bob’s?

Introduction
Asymmetric Encryption
Generating RSA Keys

Symmetric Encryption
Generating a secret key
Exchanging the secret key
Encryption

SHA256 Message Digests
Bonus: putting it all together
Sample solutions

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com