2.6. Asymmetric-key Cryptography#

In asymmetric cryptography (public-key cryptography) there are two keys, which form a key-pair. One key is used to encrypt the plaintext to ciphertext and one key is used to decrypt the ciphertext to plaintext. Note that you cannot encrypt and decrypt using the same key. If you encrypt with one key you can only decrypt it with the other key.

../../_images/plaintext_ciphertext_asymmetric.png

Simple Example If you’re wondering how asymmetric cryptography works, it all comes down to maths, specifically using the power and modulo operator.

It turns out, you can pick values \(x\), \(y\) and \(z\) such that

\[c = p^x\mod z\]
\[p = c^y\mod z\]

where \(p\) is the plaintext and \(c\) is the ciphertext. This means that the public key consists of the values \(x\) and \(z\) and the private key consists of values \(y\) and \(z\). There are clever ways to pick \(x\), \(y\), and \(z\), which we won’t go into, but lets say we had

\[x=3, y=7, z=33\]

Recall that letters can be stored as numbers in the computer:

A

B

C

D

E

F

G

H

I

J

K

L

M

N

1

2

3

4

5

6

7

8

9

10

11

12

13

14

O

P

Q

R

S

T

U

V

W

X

Y

Z

!

?

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Suppose we had the plaintext

M E S S A G E

We can represent this using the numbers

13 5 19 19 1 7 5

We’ll now encrypt with our public key information (3, 33) the formula \(p^2 \mod 33\) and apply this to the numbers 13 5 19 19 1 7 5.

for letter in [13, 5, 19, 19, 1, 7, 5]:
    print(letter**3 % 33)
Output
19
26
28
28
1
13
26

We get the ciphertext

19 26 28 28 1 13 26

which correspond to

S Z ? ? A M Z

Now we’ll try to decrypt using our private key information (7, 33) formula \(c^7 \mod 33\) and apply this to the numbers 19 26 28 28 1 13 26.

for letter in [19, 26, 28, 28, 1, 13, 26]:
    print(letter**7 % 33)
Output
13
5
19
19
1
7
5

We get 13 5 19 19 1 7 5 which corresponds to MESSAGE.

Note that this was a simple demonstration of how asymmetric keys work. Real cryptographic algorithms are much more complicated than this.

2.6.1. Sending Encrypted Messages#

Suppose Stevie wants to send Alice an encrypted message. This can be challenging using symmetric ciphers where both Stevie and Alice need to secretly communicate the key before they can send encrypted messages. So how is this solved with asymmetric ciphers?

Well Alice will generate two keys, a private key and public key.

  • The public key will be used for encryption

  • The private key will be used for decryption

Alice will post the public key for everyone (including Stevie) to see. This means that the whole world can encrypt messages using the public key. For most people this won’t be useful because they’ll be able to encrypt a message, but they won’t be able to decrypt the message since they don’t have the second key, i.e. the private key. Only Alice will be able to decrypt messages because she is the only person with the private key. This means that Stevie can encrypt a secret message and send it to Alice knowing only Alice will be able to decrypt it!

Similarly, if Alice wants to send encrypted messages to Stevie, Stevie can create his own set of public/private keys and share the public key with Alice in plaintext. This means when Alice encrypts messages with Stevie’s public key, she know that only Stevie will be able to decrypt it.

2.6.2. Proof Of Identity#

Asymmetric cryptography can also be used to prove identity. For example, if Alice wants to publish something and guarantee that she is the author, she can encrypt it with her private key. Now everyone else in the world will be able to decrypt her work using the public key, but in doing so, they will know that Alice must have been the person to have originally encrypt it because she is the only person who knows her private key.

2.6.3. In Practice#

Unlike symmetric encryption, which relies on confusion and diffusion through substitution-permutation networks to scramble data, asymmetric encryption secures data by leveraging mathematical problems that are easy to compute in one direction but extremely difficult to reverse. These problems are known as one-way functions they are simple to perform but infeasible to invert without a secret key.

2.6.4. RSA Encryption#

RSA is named after its inventors Rivest, Shamir, and Adleman. The security of RSA is based on the difficulty of factoring large prime numbers. Currently, factoring a large number into two prime factors is not feasible.

Encryption

To encrypt a plaintext message \(p\), convert it to a number and compute:

\[c = p^x \mod z\]

Decryption

Using the private key, the recipient recovers the plaintext:

\[p = c^x \mod n\]

Key Generation

Keys for RSA are generated by:

  1. Randomly generating two large numbers

  2. Calculating \(n = p \times q\)

  3. Selecting the public key \(x\) according to a fixed rule

  4. Selecting the private key \(y\), which is the modular inverse of \(x\)

2.6.5. Elliptic Curve Cryptography#

ECC is based on the mathematics of elliptic curves. The key idea of ECC is that moving along these curves is extremely difficult to predict.

ECC is mostly used to establish a shared secret between two parties, which can be used as or to create a symmetric encryption key to transfer data.

Key Generation

  1. Choose a large random integer \(d\), which is the private key

  2. Compute the public key \(Q\) by multiplying \(d\) with a predefined starting point \(G\) on the curve i.e. \(Q=dG\)

Importantly the multiplication is not normal multiplication, instead it is a translation of the point along the curve according to specific rules.

Encryption

Suppose Alice and Bob want to establish a shared secret.

  1. Alice uses Bob’s public key \(Q_B\) and her private key \(d_A\) to compute \(S = d_AQ_B\)

  2. Bob uses Alice’s public key \(Q_A\) and his private key :math`d_B` to compute \(S = d_BQ_A\)

Both Bob and Alice now have a shared secret \(S\).

2.6.6. Speed#

Asymmetric encryption is typically slower than symmetric encryption because the operations involved such as modular exponentiation or elliptical curve multiplication are computationally expensive.

Therefore asymmetric encryption is generally not suitable for cases where high speed encryption is required such as encrypting whole files or transmitting data over a network.

2.6.7. Data Size and Key Size#

Both RSA and ECC can only encrypt as many bits are used for the key e.g. if the key is 128 bits then only 128 bits of data can be encrypted.

Therefore asymmetric encryption is generally not suitable for cases with large amounts of data such as encrypting whole files.

2.6.8. Combining Asymmetric and Symmetric Encryption#

Because of the issues with asymmetric encryption the usual protocol is to:

  • Use an asymmetric encryption algorithm to establish a symmetric key between two parties

  • Encrypt and decrypt the bulk of the data using the symmetric key