RSA is a public-key encryption and signature system and makes use of large prime numbers. The asymmetry is based on the fact that computing a product is fast, but factoring takes much longer.
Operation
The RSA algorithm involves the steps key generation, encryption and decryption. It can also be used for cryptographic signatures.
Key generation
The first step to setup an RSA system, is to generate the public and the private key. This is done in the following way:
- Pick two distinct prime numbers and with bit-length .
- Compute , this will be part of both the public and the private key.
- Compute , where is Euler’s totient function.
- Choose an interger such that and (this means that and must be coprime). This will be part of the public key.
- Compute as the modular inverse of , so . For example by using Euler’s theorem. This will be part of the private key.
- This results in:
- Public key:
- Private key
Encryption
To encrypt a message , where , perform the following computation:
Where is the public key and is the resulting ciphertext.
Decryption
To decrypt ciphertext with the private key , perform the following computation:
Signatures
To sign a message , compute the hash of and sign it with the private key :
To verify the signature for the message , use the public key to check that:
RSA is exceptional in the fact that signing is the inverse of the encryption.
Remarks
Don’t use this schoolbook version RSA in practice, it has too much mathematical structure which gives it certain vulnerabilities. An example is the fact that this version of RSA is homomorphic, so two ciphertexts can be concatenated into a valid single ciphertext.
This can be used as an attack vector, so in practice some padding is used like Optimal Asymmetric Encryption Padding (OAEP).
Optimizations
For efficiency many popular crypto libraries use the following optimization for decryption and signing based on the Chinese remainder theorem. The following values are precomputed and stored as part of the private key:
- The primes from the key generation and .
- The value
- The value
- The inverse of one of the primes mod the other prime:
Instead of computing , the ciphertext can then by decryption by computing