Skip to content
Book 3: Advanced Theory

Book 3 · Chapter 17

Security

§17.1Regular · ≈ 5 m · ↘B3·17

This chapter is in the ebook.

Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.

Open in the ebook

For the whole chapter

§17.1 Encryption · Encryption protocols and digital certificates

Short code B3·17·1

Plaintext, ciphertext and keys

Short code B3·17·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Symmetric encryption: one shared key

Short code B3·17·11

FUNCTION Shift(Text : STRING, By : INTEGER) RETURNS STRING
   DECLARE Abc : STRING
   DECLARE Result : STRING
   DECLARE i : INTEGER
   DECLARE Pos : INTEGER
   DECLARE NewPos : INTEGER
   Abc ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   Result ← ""
   FOR i ← 1 TO LENGTH(Text)
      Pos ← 1
      WHILE MID(Abc, Pos, 1) <> MID(Text, i, 1)
         Pos ← Pos + 1
      ENDWHILE
      NewPos ← (Pos - 1 + By) MOD 26 + 1
      Result ← Result & MID(Abc, NewPos, 1)
   NEXT i
   RETURN Result
ENDFUNCTION
DECLARE Key : INTEGER
DECLARE Cipher : STRING
Key ← 4
Cipher ← Shift("YARD", Key)
OUTPUT Cipher
OUTPUT Shift(Cipher, 26 - Key)
A shift cipher as a function. Shift("YARD", 4) encrypts; shifting the ciphertext by 26 − 4 = 22 decrypts it. Notice the same key, 4, is needed both ways. That is what makes it symmetric.

Output

CEVH
YARD
Run and checked before printing

Asymmetric encryption: a pair of keys

Short code B3·17·12

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

A verified message to the public

Short code B3·17·13

DECLARE C : INTEGER
C ← Pow(4, 3)
OUTPUT "public key on 4: ", C
OUTPUT "private key on ", C, ": ", Pow(C, 7)
C ← Pow(4, 7)
OUTPUT "private key on 4: ", C
OUTPUT "public key on ", C, ": ", Pow(C, 3)
A toy key pair: public key (3, 33), private key (7, 33). Pow(M, E) multiplies E copies of M together, keeping the remainder after dividing by 33 after each step (the function is online at B3·17·13). Notice each key undoes the other, in either order. Real keys are hundreds of digits long.

Output

public key on 4: 31
private key on 31: 4
private key on 4: 16
public key on 16: 4
Run and checked before printing

Symmetric or asymmetric? Comparing the two

Short code B3·17·14

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Quantum cryptography

Short code B3·17·15

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

SSL/TLS: purpose and where it is used

Short code B3·17·16

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

The TLS handshake

Short code B3·17·17

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Digital certificates

Short code B3·17·18

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Hashing and message digests

Short code B3·17·19

FUNCTION Digest(Msg : STRING) RETURNS INTEGER
   DECLARE Abc : STRING
   DECLARE Total : INTEGER
   DECLARE i : INTEGER
   DECLARE Pos : INTEGER
   Abc ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   Total ← 0
   FOR i ← 1 TO LENGTH(Msg)
      FOR Pos ← 1 TO 26
         IF MID(Abc, Pos, 1) = MID(Msg, i, 1)
            THEN
               Total ← Total + Pos
         ENDIF
      NEXT Pos
   NEXT i
   RETURN Total MOD 33
ENDFUNCTION
OUTPUT "SEND SIX -> ", Digest("SEND SIX")
OUTPUT "SEND TEN -> ", Digest("SEND TEN")
OUTPUT "SIX SEND -> ", Digest("SIX SEND")
A toy hash: add the letters' alphabet positions (spaces ignored), keep the remainder after dividing by 33. Notice one changed word changes the digest. An anagram gives the same one, a collision a real hash function must never allow.

Output

SEND SIX -> 28
SEND TEN -> 15
SIX SEND -> 28
Run and checked before printing

Digital signatures

Short code B3·17·20

PROCEDURE Check(Msg : STRING, Sig : INTEGER)
   DECLARE Sent : INTEGER
   DECLARE Now : INTEGER
   Sent ← Pow(Sig, 3)
   Now ← Digest(Msg)
   IF Sent = Now
      THEN
         OUTPUT Msg, ": ", Sent, " = ", Now, " accept"
      ELSE
         OUTPUT Msg, ": ", Sent, " <> ", Now, " reject"
   ENDIF
ENDPROCEDURE
DECLARE Sig : INTEGER
Sig ← Pow(Digest("SEND SIX"), 7)
OUTPUT "Signature: ", Sig
CALL Check("SEND SIX", Sig)
CALL Check("SEND TEN", Sig)
A toy digital signature. The sender encrypts the digest of SEND SIX with the private key (7, 33); Check decrypts the signature with the public key (3, 33) and compares it with a digest of what arrived. Pow and Digest are the functions above (online at B3·17·20). Notice the altered message is rejected.

Output

Signature: 19
SEND SIX: 28 = 28 accept
SEND TEN: 28 <> 15 reject
Run and checked before printing

Which key, for which job?

Short code B3·17·21

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Past-paper practice

Short code B3·17·90

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook 3: Security

Short code W3·17 · Advanced Theory Workbook

The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Ask Z about Security
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook