Some practices of encrypting and decrypting, including numbers and characters.

Exercise 1 – RSA cryptosystem:

The role of 2 people tries to encrypt and decrypt the message the message from public and private key.

  • Person 1 generates the public key and private key using different prime numbers

    • Generating public and private keys
    • Pick two prime numbers, like p = 3 and q = 13
    • Calculate n = p * q = 3 * 13 = 39
    • Calculate z = (p – 1) * (q – 1) = (3-1) * (13 -1) = 2 * 12 = 24
    • Choose a prime number k, such that k is co-prime to z, i.e…, z is not divisible by k
      • We have several choices for k: 5, 7, 11, 13, 17, 19, 23 (we cannot use 3, because 24 is divisible by that numbers). => Let’s pick k = 5
      • That means the number n = 39 and k = 5 become the public key
  • Person 1 passes the public key to Person 2

    • give Person 2: n = 39 and k = 5
  • Person 2 chooses a number between 1 and 50 as the message: Ex. Person 2 chooses 20 as the message

    • ->Plain message: P = 20
  • Person 2 encrypts the message and passes the encrypted message to Person 1

    • Calculate the private key. Here is how:
      • k * j = 1 (mod z) –> 5 * j = 1 (mod 24) –> (5 * j) / 24 =? (with the remainder of 1 – are only interested in the remainder)
      • We can easily conclude that 25/24 = 1 (with the remainder of 1), so that means j = 5, which is our secret key. We MUST NOT give this key away!
      • Now, we can begin our message transmission from our browser to the server. First, the browser requests the public key from the server, which was the server sends. It sends n = 39 and k = 5 back to the browser.
      • Now, we assume that the browser has a plain message P = 20, and it wants to encrypt it before sending it to the server.
    • Let’s Encrypting the message
      • Here is the formula encryption math that browser executes: P ^ k = E (mod n)
      • 20 ^ 5 = E (mod 39) –> E = (20 ^ 5) / 39 = 3200000/39 = 11
  • Person 1 decrypts the message

    • here is the decryption math the server executes to recover the original plain text message which the browser started with: E ^ j = P (mod n)
    • Plugging in the values
      • 11 ^ 5 = P (mod 39)
      • (11 ^ 5) / 39 = 161051 / 39 = 4129.51282051…
      • 4129 * 39 = 161031 
      • P = 161051 – 161031 = 20 
      • That means P = 20 is the exactly plain text message that the browser started with!

Notes:

‘^’ means “to the power of)

P is the plain message we want to encrypt

n and k are server’s public key

E is our encrypted message we want to generate

P: plain message

j: the server’s secret key

Exercise 2 – transposition cipher:

See if you can decrypt the message below. It uses a simple substitution cipher where the letters of the alphabet have been shifted a number of places in a similar way to this:

Alphabet: A B C … Z

Cipher: B C … A

The message is

UIF NFTTBHF JT OPX EFDSZQUFE CZ QPSUFS

Answer:

THE MESSAGE IS NOW DECRYPTED BY PORTER.

References

  • NCC Education level 5 – computing