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.
1Asymmetric 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).
2Tools
| 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 |
3The 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 |
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.
4What a real run shows
$ 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.pemwas 451 bytes andrsa.pem1704, because the private key file also stores the numbers used to build the pair.
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.
5Worked examples
Encrypting for a teacher
A teacher sent you teacher_pub.pem. Write the command to encrypt essay.txt for the teacher, saving essay.enc.
- 1Use the teacher's public key, and mark it as public with
-pubin. - 2Encrypt with
pkeyutl -encrypt.
openssl pkeyutl -encrypt -pubin -inkey teacher_pub.pem -in essay.txt -out essay.encWhich key file?
A student runs openssl pkeyutl -decrypt -pubin -inkey public.pem -in test.enc -out test and gets an error. Why?
- 1Decryption must use the private key, the partner of the public key that encrypted the file.
- 2The command points at
public.pemand uses-pubin.
openssl pkeyutl -decrypt -inkey rsa.pem -in test.enc -out test.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.
- 1First the receiver makes the pair: (c).
- 2Then extracts the public key: (b), and shares it: (e).
- 3The sender encrypts: (a). The receiver decrypts: (d).
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.
- 1One RSA operation with a 2048-bit key can only take a small input (here, up to 245 bytes).
- 2A 2 MB file is far larger.
- 3Real systems encrypt the file with AES, then encrypt only the short AES key with the receiver's RSA public key.
6Common mistakes
rsa.pem holds the private key. Share only public.pem.
Without it, OpenSSL expects a private key file and fails to load public.pem.
2048-bit RSA and 128-bit AES are different algorithms; their lengths do not compare.
“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.