# RSA algorithm in C

## Introduction:

The RSA algorithm is a highly quick encryption and decryption technique. It is utilized in numerous applications, including communication encryption and decryption. The algorithm is predicated on the notion that messages may be encrypted and decrypted if we are aware of both the public and private keys. In order to calculate n = pq, an RSA user generates the two huge prime numbers p and q. After that, the totient function is calculated (n) = (p - 1)(q - 1). They choose e as a public key by selecting it as a substantially prime integer to n and 1 e n.

The private key, d, is calculated as follows:

The process of encryption is as follows:

Overall the characters.

The process for decrypting all the characters is as follows:

### Example:

Create a C program that will ask the user for two prime integers and then use the RSA technique to encrypt and decrypt a message.

## RSA Cryptography

1. Request two prime numbers from the user and verify them.
2. Put the prime numbers in different variables.
3. Determine n = pq.
4. Calculate (n) = (p - 1)(q - 1)after the above step.
5. Select a random number e that is close to being prime to both n and 1 e n.
6. Determine d = e-1 mod n.
7. Print out the private and public keys.
8. Request a message from the user and then save it in a variable.
9. Use the public key to encrypt the message.
10. Using the private key, decrypt the message.
11. Print the message, both encrypted and decrypted.

Let's examine two samples of the RSA Algorithm in C.

• RSA Algorithm Using Simple Approach

## Approach 1

### RSA Algorithm Using Simple Approach

The RSA algorithm is used in this method. This kind of public key encryption makes use of two unique but connected keys, also known as asymmetric encryption and decryption technique. Additionally, knowing one key will not make it easier to understand how the other key protects itself.

The RSA algorithm makes use of the fact that multiplying two large prime integers together yields a simple result. However, if you know only one of the original primes, you cannot fairly assume the other or the two original numbers from that product.

Source Code

Here is the C program's source code for implementing the RSA algorithm. On a Linux system, the C program is successfully compiled and executed. Also presented below is the output of the program.

### Cases of Runtime Testing

In this instance, we enter the two prime numbers "5" and "17", and then encrypt and decrypt a message using the RSA technique.

## Approach 2:

In this method, we store some intermediate data in a temporary array that will be used later in the decrypt function.

Methods applied

int isPrime(int) -

This function determines whether or not the number is a prime.

int gcd(int, int) -

The largest common divisor of two numbers is returned by this function.

int totient(int, int) -

The totient of an integer is returned by this function.

int randome(int) -

This function gives back a random integer that is smaller than the input.

int private_key(int, int) -

The private key is returned by this function.

long pomod(long, long, long) -

This function gives the number's modular exponentiation.

char *encrypt(char *, long, long) -

The message is encrypted using this feature.

char *decrypt(char *, long, long) -

The message is decrypted using this function.

Example:

Source Code

Here is the C program's source code for implementing the RSA algorithm. On a Linux system, the C program is successfully compiled and executed. Also presented below is the output of the program.

Output:

### Cases of Runtime Testing

In this instance, we enter the two prime numbers "5" and "13", and then encrypt and decrypt a message using the RSA technique.

Program Description

• The user will be prompted to provide two prime numbers before this program uses the RSA technique to encrypt and decrypt a communication.
• The program will determine whether or not the values of p and q are prime after accepting their values.
• The program will determine the values of n, lambda_n, e, and d based on the aforementioned hypothesis if they are prime.
• After that, the message will be encrypted, and then decoded.

#### Note: Given that the character set implementation in C is so limited, many characters are lost during the encryption and decryption processes, making Approach 2 (Additional program) an imperfect implementation for large values of n (p * q). As a result, as a workaround, all the intermediate calculations for this program should be performed on a long long int type array.

Security:

The RSA algorithm is frequently utilized for secure data transfer, which is regarded as being exceptionally secure.

Public-key cryptography:

The RSA algorithm needs two separate keys for encryption and decryption because it is a public-key cryptography algorithm. Data is encrypted using the public key, and decrypted using the private key.

Key conversation:

A secret key can be exchanged between two parties securely using the RSA technique without actually sending the key over the network.

Electronic signatures:

A sender can sign a message using their private key and the recipient can verify the signature using the sender's public key when the RSA technique is used for digital signatures.

Poor processing rate:

When working with huge amounts of data, the RSA algorithm is slower than alternative encryption techniques.

Extra-large keys:

Large key sizes are necessary for the RSA algorithm to be secure, which means that greater processing power and storage space are needed.

Attacks through side-channel are susceptible:

The RSA technique is susceptible to side-channel attacks, which allow an attacker to obtain the private key by using data that has been leaked through side channels such power usage, electromagnetic radiation, and timing analysis.

Limited application in some cases:

Due to its poor processing speed, the RSA algorithm is unsuitable for some applications, such as those that demand continuous encryption and decryption of substantial volumes of data.

### Feedback   