---
title: "Applying Asymmetric Encryption | FiveStudy"
description: "RSA and ECC, and how to generate a key pair, extract the public key, encrypt, and decrypt a file with OpenSSL, including what each step produces."
url: https://fivestudy.com/guides/ap-cybersecurity-5-4-c-applying-asymmetric-encryption
---

[AP Cybersecurity](https://fivestudy.com/ap-cybersecurity) · Study guide

# Applying Asymmetric Encryption

[Figure: FiveStudy]

FiveStudy

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

# Applying Asymmetric Encryption

RSA and ECC, and how to generate a key pair, extract the public key, encrypt, and decrypt a file with OpenSSL, with what each command really produces.

1. Asymmetric algorithms and their uses 2. Tools 3. The four OpenSSL commands 4. What a real run shows 5. Worked examples 6. Common mistakes

## 1 Asymmetric algorithms and their uses

Common asymmetric algorithms include **RSA** and **elliptic curve cryptography (ECC)**. ECC reaches similar security with much shorter keys, which suits phones and small devices. Asymmetric algorithms are used in many applications, including **digital signatures** (proving who created or approved a file) and **digital certificates** (proving a website's public key really belongs to that website).

## 2 Tools

| Kind of tool | Example |
| --- | --- |
| Command line | OpenSSL |
| Specialized software | RSA Encryption Tool (open source) |
| Web-based tools | Many encryption websites; never paste a real private key into one |

## 3 The four OpenSSL commands

| Step | Command | What it does |
| --- | --- | --- |
| 1. Generate a key pair | `openssl genrsa -out rsa.pem 2048` | Creates a 2048-bit RSA key pair and saves it to `rsa.pem` |
| 2. Extract the public key | `openssl rsa -pubout -in rsa.pem -outform PEM -out public.pem` | Copies only the public key into `public.pem` |
| 3. Encrypt | `openssl pkeyutl -encrypt -pubin -inkey public.pem -in test -out test.enc` | Encrypts `test` with the public key (`-pubin` says the key file is a public key) |
| 4. Decrypt | `openssl pkeyutl -decrypt -inkey rsa.pem -in test.enc -out test` | Decrypts with the private key in `rsa.pem` |

**Figure 1.** The public key file encrypts; the private key file decrypts. Only `public.pem` ever leaves the receiver's computer.

The receiver runs steps 1 and 2 and sends `public.pem` to anyone who wants to send them something. A sender runs step 3 with the receiver's `public.pem`. Only the receiver can run step 4.

## 4 What a real run shows

Terminal session

```
$ openssl genrsa -out rsa.pem 2048
$ openssl rsa -pubout -in rsa.pem -outform PEM -out public.pem
$ head -2 public.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC…
$ cat test
Locker 214 code: 3-17-29
$ openssl pkeyutl -encrypt -pubin -inkey public.pem -in test -out test.enc
$ wc -c test.enc
     256 test.enc
$ openssl pkeyutl -decrypt -inkey rsa.pem -in test.enc -out test
$ cat test
Locker 214 code: 3-17-29
$ openssl pkeyutl -decrypt -pubin -inkey public.pem -in test.enc -out x
A private key is needed for this operation
```

- **The ciphertext is 256 bytes** even though the message was 25. A 2048-bit key always produces 2048 ÷ 8 = 256 bytes of ciphertext.
- **The public key cannot decrypt.** OpenSSL refuses: decryption needs the private key.
- **RSA only takes small inputs.** With a 2048-bit key, OpenSSL encrypted a 245-byte file but rejected one of 246 bytes with “data too large for key size.” That is why real systems use RSA to send a short AES key, then encrypt the actual file with AES.
- **The key files differ in size**: `public.pem` was 451 bytes and `rsa.pem` 1704, because the private key file also stores the numbers used to build the pair.

The key idea

Generate a pair with `genrsa`, extract and share the public key with `rsa -pubout`, encrypt with `pkeyutl -encrypt -pubin` and the public key, and decrypt with `pkeyutl -decrypt` and the private key.

## 5 Worked examples

Worked Example 1 Write the command

### Encrypting for a teacher

A teacher sent you `teacher_pub.pem`. Write the command to encrypt `essay.txt` for the teacher, saving `essay.enc`.

1. 1

   Use the teacher's public key, and mark it as public with `-pubin`.

2. 2

   Encrypt with `pkeyutl -encrypt`.

**Answer:** `openssl pkeyutl -encrypt -pubin -inkey teacher_pub.pem -in essay.txt -out essay.enc`

Worked Example 2 Find the error

### Which key file?

A student runs `openssl pkeyutl -decrypt -pubin -inkey public.pem -in test.enc -out test` and gets an error. Why?

1. 1

   Decryption must use the private key, the partner of the public key that encrypted the file.

2. 2

   The command points at `public.pem` and uses `-pubin`.

**Answer:** Use the private key: `openssl pkeyutl -decrypt -inkey rsa.pem -in test.enc -out test`.

Worked Example 3 Order the steps

### Setting up to receive

Put these in order for receiving an encrypted file: (a) the sender encrypts with `public.pem`; (b) `openssl rsa -pubout …`; (c) `openssl genrsa …`; (d) the receiver decrypts with `rsa.pem`; (e) the receiver sends `public.pem` to the sender.

1. 1

   First the receiver makes the pair: (c).

2. 2

   Then extracts the public key: (b), and shares it: (e).

3. 3

   The sender encrypts: (a). The receiver decrypts: (d).

**Answer:** c, b, e, a, d.

Worked Example 4 Explain the result

### The big file

A student tries to encrypt a 2 MB video with `pkeyutl -encrypt` and a 2048-bit public key, and gets “data too large for key size.” Explain, and suggest what real systems do.

1. 1

   One RSA operation with a 2048-bit key can only take a small input (here, up to 245 bytes).

2. 2

   A 2 MB file is far larger.

3. 3

   Real systems encrypt the file with AES, then encrypt only the short AES key with the receiver's RSA public key.

**Answer:** RSA can only encrypt small data; use it to protect an AES key and use AES for the file.

## 6 Common mistakes

Common Mistake 1 · Sharing rsa.pem

`rsa.pem` holds the private key. Share only `public.pem`.

Common Mistake 2 · Forgetting -pubin when encrypting

Without it, OpenSSL expects a private key file and fails to load `public.pem`.

Common Mistake 3 · Comparing RSA and AES key lengths

2048-bit RSA and 128-bit AES are different algorithms; their lengths do not compare.

Explain an asymmetric encryption step like this

“The receiver generates a 2048-bit RSA key pair with `openssl genrsa` and extracts the public key with `openssl rsa -pubout`. The sender encrypts the file with `pkeyutl -encrypt -pubin` and that public key, and only the receiver’s private key in `rsa.pem` can decrypt it.”

Say who runs each command and which key file each one uses.

FiveStudy AP® Cybersecurity · Applying Asymmetric Encryption
