Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · A Level 19

Compression, Encryption and Hashing

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 24 marks. Total 34 marks.

Name

Class

Date

Mark  / 34

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What is the difference between lossless and lossy compression?1
    1. ALossless lets the original be rebuilt exactly; lossy discards data for good
    2. BLossless files are always smaller
    3. CLossy compression is used for text and lossless for images
    4. DLossy compression needs a key
  2. 2Run length encoding turns AAABBBBCC into which of these?1
    1. A3A4B2C
    2. BA3B4C2
    3. C3A3B3C
    4. D9ABC
  3. 3When would run length encoding make a file larger?1
    1. AWhen the data is mostly zeros
    2. BWhen the data is a text file
    3. CWhen the data has few or no repeated runs
    4. DWhen the file is very large
  4. 4What is the main idea of Huffman coding?1
    1. AData is encrypted with a public key
    2. BMore frequent symbols get shorter codes
    3. CEvery symbol gets a code of the same length
    4. DRepeated words are replaced by a dictionary reference
  5. 5Why must no Huffman code be the start of another?1
    1. AOtherwise the decoder could not tell where one symbol ends
    2. BOtherwise the tree would be too tall
    3. COtherwise the file would not be lossless
    4. DOtherwise it would be slow to build
  6. 6What is the weakness of a Caesar cipher?1
    1. AIt can only be used once
    2. BIt cannot encrypt spaces
    3. CThere are only 25 possible keys and letter frequencies show through
    4. DIt needs a very long key
  7. 7Which cipher is perfectly secure if used correctly?1
    1. AThe Caesar cipher
    2. BThe Vigenere cipher
    3. CAny cipher with a 128 bit key
    4. DThe one-time pad
  8. 8In public key encryption, how does Ali send Bea a message that only she can read?1
    1. AEncrypt it with Ali's public key
    2. BEncrypt it with Ali's private key
    3. CEncrypt it with Bea's private key
    4. DEncrypt it with Bea's public key
  9. 9What does a digital signature prove?1
    1. AThat the message was compressed
    2. BWho sent the message, and that it was not changed
    3. CThat the message is secret
    4. DThat the sender has a strong password
  10. 10Why is a salt added to a password before hashing it?1
    1. ASo the hash becomes shorter
    2. BSo the password is encrypted
    3. CSo the password can be decrypted later
    4. DSo identical passwords give different hashes and prepared tables are useless

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1ShowThe data AAAAABBBCCDDDDDD is stored using run length encoding. Show the result of encoding it, and state one disadvantage of run length encoding.4
  2. 2ExplainExplain two differences between lossless compression and lossy compression. Give an example of a file type for which each is suitable.4
  3. 3DescribeThe table shows how often each of four characters appears in a message of 8 characters. Describe how a Huffman tree is built for this data, and calculate the total number of bits needed to store the message using Huffman codes of A = 0, B = 10, C = 110 and D = 111.5
    character:  A   B   C   D
    frequency:  4   2   1   1
  4. 4ExplainExplain the difference between symmetric and asymmetric encryption, and describe how asymmetric encryption can be used to send a message that only the receiver can read.5
  5. 5DiscussA website stores the passwords of its users. Discuss how the website should protect the passwords in case its database is stolen.6

Mark scheme

Section A

1A2A3C4B5A6C7D8D9B10D

Section B

1 The data AAAAABBBCCDDDDDD is stored using run length encoding. Show the result of encoding it, and state one disadvantage of run length encoding. 4

One mark for each of the following, to a maximum of 4.

  • Counts the runs correctly: 5 A, 3 B, 2 C, 6 D
  • Encoded result 5A3B2C6D (a count and a character for each run)
  • Disadvantage: where there are few or no repeated characters the data can become larger
  • So it is only effective on data containing long runs of the same value

Model answer. The data has a run of 5 As, then 3 Bs, 2 Cs and 6 Ds, so it is encoded as 5A3B2C6D. One disadvantage is that where the data has few or no repeated characters, each single character is replaced by a count and the character, so the encoded data can be larger than the original. It is only effective for data that contains long runs of the same value.

2 Explain two differences between lossless compression and lossy compression. Give an example of a file type for which each is suitable. 4

One mark for each of the following, to a maximum of 4.

  • Lossless allows the original data to be rebuilt exactly, but lossy permanently removes some data
  • Lossy files are usually much smaller than lossless
  • An example where lossless is needed: text, program or data files (or PNG)
  • An example where lossy is suitable: photographs, music or video (JPEG, MP3, MP4)

Model answer. Lossless compression allows the original data to be rebuilt exactly, whereas lossy compression permanently discards some of the data so that the original cannot be recovered. Lossy compression can usually reduce a file to a much smaller size than lossless compression. Lossless compression is needed for files such as text documents and programs, where any change to the data would damage them. Lossy compression is suitable for photographs, music and video, as in JPEG and MP3 files, where the detail removed is hard for people to notice.

3 The table shows how often each of four characters appears in a message of 8 characters. Describe how a Huffman tree is built for this data, and calculate the total number of bits needed to store the message using Huffman codes of A = 0, B = 10, C = 110 and D = 111. 5

One mark for each of the following, to a maximum of 5.

  • The two characters with the lowest frequencies (C and D) are joined first, to make a node with frequency 2
  • The process is repeated, always joining the two lowest frequencies, until there is one tree
  • Left branches are labelled 0 and right branches 1, and the path from the root gives the code
  • Bits used: A 4 x 1 = 4, B 2 x 2 = 4, C 1 x 3 = 3, D 1 x 3 = 3
  • Total = 14 bits

Model answer. The two characters with the lowest frequencies, C and D, are joined under a new node with a frequency of 2. The process is repeated, each time joining the two lowest frequencies (B with the CD node, giving 4, then A with that node, giving 8), until there is a single tree. The branches are labelled 0 and 1, and the code for each character is the path from the root to it. The message needs A: 4 x 1 = 4 bits, B: 2 x 2 = 4 bits, C: 1 x 3 = 3 bits and D: 1 x 3 = 3 bits, a total of 14 bits.

4 Explain the difference between symmetric and asymmetric encryption, and describe how asymmetric encryption can be used to send a message that only the receiver can read. 5

One mark for each of the following, to a maximum of 5.

  • Symmetric: the same key is used to encrypt and to decrypt
  • Asymmetric uses a pair of keys, a public key and a private key
  • The sender encrypts the message using the receiver's public key
  • Only the receiver's private key can decrypt it
  • The private key is never shared, so the problem of sending a key securely is avoided

Model answer. In symmetric encryption the same key is used to encrypt and to decrypt the message, so the key has to be shared securely first. In asymmetric encryption there is a pair of keys, a public key and a private key. To send a message only the receiver can read, the sender encrypts it using the receiver's public key, which anyone is allowed to have. Only the receiver's private key can decrypt the message. The private key is kept secret and never shared, so there is no need to send a secret key across the network.

5 A website stores the passwords of its users. Discuss how the website should protect the passwords in case its database is stolen. 6

One mark for each of the following, to a maximum of 6.

  • The passwords should not be stored as plain text, but as a hash
  • A hash cannot be reversed, so a thief cannot read the passwords; at login the typed password is hashed and compared
  • A unique random salt should be added before hashing, so that identical passwords have different hashes
  • The salt defeats precomputed tables of hashes (rainbow tables)
  • A slow hashing method (key stretching, such as bcrypt or PBKDF2) makes each guess take longer, slowing brute force attacks
  • A reasoned conclusion, such as also requiring strong passwords or two factor authentication

Model answer. The website should not store the passwords as plain text, because a thief with the database could read every one. It should store only a hash of each password. A hash cannot be reversed, and when a user logs in the password they type is hashed and compared with the stored value. A unique, random salt should be added to each password before it is hashed, so that two users with the same password have different hashes and a precomputed table of hashes of common passwords is of no use. A slow hashing method such as bcrypt or PBKDF2, which repeats the hash many times, should be used so that each guess takes a long time and brute force attacks are impractical. To strengthen this further the site should insist on long passwords and offer two factor authentication, because even a well hashed weak password can be guessed.