Exercises: RSA Encrypt / Decrypt
In this exercise you shall encrypt and decrypt messages using the RSA public-key cryptosystem.

Encrypt Message with RSA-OAEP

You are given a text message and a RSA public key (in PEM format). Write a program to encrypt the message, using the RSA-OAEP encryption scheme (RSA + PKCS#1 OAEP padding).
Input:
    First line: the input message
    Next few lines: the RSA public key (in the PKCS#8 PEM ASN.1 format)
    The public key length can be 512 bits, 1024 bits, 2048 bits, 3072 bits or 4096 bits.
Output:
    The encrypted message, printed as hex string.
Write your code in programming language of choice.
Sample input:
1
Secret message
2
-----BEGIN PUBLIC KEY-----
3
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMYhCcGpfoebriBbFaUMMwH3B5t7udir
4
ODJehnQTPlWLf9SVfQdx0v9ATJ2Rs5kQjdJ/wZYunMBVq6/FhgPZexsCAwEAAQ==
5
-----END PUBLIC KEY-----
Copied!
The above input uses a 512-bit RSA public key and a small plain text message, that can fit inside the key length (after the OAEP padding).
Sample output (for the above input):
1
218dd78c5e14b4d58efd10575b521db46c0caa5c699134abf18bbeeac170cfe446e25d0d82257082539e4ccd3e0aa8bffc1b07d2bde9e635a7b9b7fc6cf4c266
Copied!
Note: the above output should be different at each execution due to the randomness injected by the OAEP padding algorithm.

Decrypt a Message with RSA-OAEP

You are given a RSA-OAEP-encrypted ciphertext (as hex string) and a RSA private key (in PEM format). Write a program to decrypt the message, using the RSA-OAEP encryption scheme (RSA + PKCS#1 OAEP padding).
Input:
    First line: the ciphertext (the encrypted message), given as hex string
    Next few lines: the RSA private key (in the PKCS#8 PEM ASN.1 format)
Output:
    Print the decrypted message as plain text
    Print Decryption failed! in case of problem
Write your code in programming language of choice.
Sample input:
1
218dd78c5e14b4d58efd10575b521db46c0caa5c699134abf18bbeeac170cfe446e25d0d82257082539e4ccd3e0aa8bffc1b07d2bde9e635a7b9b7fc6cf4c266
2
-----BEGIN RSA PRIVATE KEY-----
3
MIIBOgIBAAJBAMYhCcGpfoebriBbFaUMMwH3B5t7udirODJehnQTPlWLf9SVfQdx
4
0v9ATJ2Rs5kQjdJ/wZYunMBVq6/FhgPZexsCAwEAAQJAbNSzBkTzMswqHq3Juupz
5
jk3CSP7ye/i5Grnfgx0a7WOGpVrEDQNo0iihEf5pRAfaazEdfJX2Tj+auuv06392
6
kQIhAOeJahRwOt8cYroLZzHHf7LWQglRaTbtKShqmbLdBZMzAiEA2xADyA3xGXcl
7
txN0DOfSycwFyqkdlfsuyAwKibPteHkCIQDJ1P6UzHR1UwA434HYYejOU3mDN+V4
8
zOoI4kwTIBohAwIgLrqv09EFiUUdSnxf2RDqqhlXcu+4W/IE/K904AL9uSECICeT
9
tkAnJHB7k6fvox6ErJV53w06bUF1jGw8yHuaCcHX
10
-----END RSA PRIVATE KEY-----
Copied!
The above input uses a 512-bit RSA private key and an encrypted ciphertext of the same length.
Sample output (for the above input):
1
Secret message
Copied!
Another sample input (wrong 512-bit private key):
1
218dd78c5e14b4d58efd10575b521db46c0caa5c699134abf18bbeeac170cfe446e25d0d82257082539e4ccd3e0aa8bffc1b07d2bde9e635a7b9b7fc6cf4c266
2
-----BEGIN RSA PRIVATE KEY-----
3
MIIBOQIBAAJBAJd0kbrC4AxpcqBgWVPpb8IoI/kdQkF1twrfQtoMkHgB71vpY6Sg
4
68CUA7Ejq/dbAHlvFdXqwEK9vXH3kFpc8pcCAwEAAQJAaFrlXm2Pun2dgWthoTOi
5
0YCe6LKESF43dMJIab1mfYiltrSpGaoTXLvHR+NaAgqcr9KAH24Mi05ttUBcWRsI
6
QQIhAOLTSyeDZnq5rqdwBlU8p6USpeImRhWRNcCHA/QLxcaPAiEAqu+O1p1YB3Mp
7
GKgB9PvZE3TZqmlgtEFmSMYinF3g13kCIF9FjpCXMYkkysZLWG2e32+HaKOXneJb
8
Lq+iRjfQZg7jAiBcm6D1YRV6I8gWFZ/JzFBVHC95BdJgljYGI2JI+QuBcQIgLJjH
9
IPctSCUtukz+7fdeOdw/0FINcUGvnQyuEK34UxE=
10
-----END RSA PRIVATE KEY-----
Copied!
The corresponding output should be:
1
Decryption failed!
Copied!
Note that the RSA-OAEP padding algorithm has built-in checksum, which allows to detect incorrect decryption attempts, but it is not an authenticated encryption scheme.

* Implement Hybrid Encryption / Decryption with RSA-KEM

Write a program to encrypt a large message (bigger than the RSA key length, e.g. a PDF document) using the RSA-KEM hybrid encryption scheme with AES symmetric encryption (use block mode of choice, e.g. GCM or CTR).
Hint:
    Note that in some languages it is hard to find and RSA-KEM implementation, so you can skip this exercise or use another hybrid encryption scheme (e.g. RSA + AES + HMAC).
Input:
    The message for encryption
    RSA public key (in PEM format)
Output:
    The encrypted ciphertext (hex string)
    The random IV salt for the AES cipher (hex string)
    The authentication tag / MAC for the encrypted message (hex string)
    The encapsulated secret key for the AES algorithm (hex string)
Write a program to decrypt given encrypted message, produced by the previous exercise, using the RSA-KEM hybrid encryption scheme with AES symmetric encryption (use block mode of choice, e.g. GCM or CTR).
Input:
    The encrypted ciphertext (hex string)
    The random IV salt for the AES cipher (hex string)
    The authentication tag / MAC for the encrypted message (hex string)
    The encapsulated secret key for the AES algorithm (hex string)
Output:
    The decrypted original plaintext message
    Print Decryption failed! if the message decryption is not successful (e.g. wrong password)
Last modified 6d ago