How to resolve the algorithm RSA code step by step in the C programming language
How to resolve the algorithm RSA code step by step in the C programming language
Table of Contents
Problem Statement
Given an RSA key (n,e,d), construct a program to encrypt and decrypt plaintext messages strings. Background RSA code is used to encode secret messages. It is named after Ron Rivest, Adi Shamir, and Leonard Adleman who published it at MIT in 1977. The advantage of this type of encryption is that you can distribute the number “
n
{\displaystyle n}
” and “
e
{\displaystyle e}
” (which makes up the Public Key used for encryption) to everyone. The Private Key used for decryption “
d
{\displaystyle d}
” is kept secret, so that only the recipient can read the encrypted plaintext. The process by which this is done is that a message, for example “Hello World” is encoded as numbers (This could be encoding as ASCII or as a subset of characters
a
01 , b
02 , . . . , z
26
{\displaystyle a=01,b=02,...,z=26}
). This yields a string of numbers, generally referred to as "numerical plaintext", “
P
{\displaystyle P}
”. For example, “Hello World” encoded with a=1,...,z=26 by hundreds would yield
08051212152315181204
{\displaystyle 08051212152315181204}
. The plaintext must also be split into blocks so that the numerical plaintext is smaller than
n
{\displaystyle n}
otherwise the decryption will fail. The ciphertext,
C
{\displaystyle C}
, is then computed by taking each block of
P
{\displaystyle P}
, and computing Similarly, to decode, one computes To generate a key, one finds 2 (ideally large) primes
p
{\displaystyle p}
and
q
{\displaystyle q}
. the value “
n
{\displaystyle n}
” is simply:
n
p × q
{\displaystyle n=p\times q}
. One must then choose an “
e
{\displaystyle e}
” such that
gcd ( e , ( p − 1 ) × ( q − 1 ) )
1
{\displaystyle \gcd(e,(p-1)\times (q-1))=1}
. That is to say,
e
{\displaystyle e}
and
( p − 1 ) × ( q − 1 )
{\displaystyle (p-1)\times (q-1)}
are relatively prime to each other. The decryption value
d
{\displaystyle d}
is then found by solving The security of the code is based on the secrecy of the Private Key (decryption exponent) “
d
{\displaystyle d}
” and the difficulty in factoring “
n
{\displaystyle n}
”. Research into RSA facilitated advances in factoring and a number of factoring challenges. Keys of 829 bits have been successfully factored, and NIST now recommends 2048 bit keys going forward (see Asymmetric algorithm key lengths). Summary of the task requirements:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm RSA code step by step in the C programming language
The presented C program demonstrates a simplified example of the RSA encryption and decryption process, using the GMP (GNU Multi-Precision) library for handling large integers.
Here's a detailed breakdown of the code:
-
Header Files: The program includes several standard C libraries:
<stdio.h>for input and output<stdlib.h>for standard library functions<string.h>for string manipulation<gmp.h>for the GMP library
-
Variable Declarations: The following variables are declared:
n,d,e: Large integers representing the public key componentspt: Large integer for the plaintext messagect: Large integer for the ciphertext
-
Initialization: The GMP functions
mpz_init()are used to initialize thept,ctvariables and the public key components (n,e,d) are set from string literals. -
Plaintext Input: The plaintext message "Rossetta Code" is imported into the
ptvariable usingmpz_import(). -
Encryption: The plaintext message (
pt) is encrypted using modular exponentiation with the public exponenteand modulusn. The result is stored in thectvariable. -
Printing Encrypted Text: The encrypted ciphertext (
ct) is printed usinggmp_printf(). -
Decryption: The ciphertext (
ct) is decrypted using modular exponentiation with the private exponentdand modulusn. The result is stored back in theptvariable. -
Printing Decrypted Text: The decrypted message (
pt) is printed usinggmp_printf(). -
Exporting Decrypted Text: The decrypted message (
pt) is exported as a string into thebufferarray usingmpz_export(). -
Printing Decrypted String: The
bufferis printed as a string, displaying the recovered plaintext. -
Cleanup: Finally, the GMP variables (
pt,ct,n,e,d) are cleared to release their allocated memory. -
Main Function: The
main()function serves as the entry point for the program. It calls the above-described functions to perform the RSA encryption and decryption.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
int main(void)
{
mpz_t n, d, e, pt, ct;
mpz_init(pt);
mpz_init(ct);
mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
mpz_init_set_str(e, "65537", 10);
mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);
const char *plaintext = "Rossetta Code";
mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);
if (mpz_cmp(pt, n) > 0)
abort();
mpz_powm(ct, pt, e, n);
gmp_printf("Encoded: %Zd\n", ct);
mpz_powm(pt, ct, d, n);
gmp_printf("Decoded: %Zd\n", pt);
char buffer[64];
mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
printf("As String: %s\n", buffer);
mpz_clears(pt, ct, n, e, d, NULL);
return 0;
}
You may also check:How to resolve the algorithm RCRPG step by step in the C programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the C programming language
You may also check:How to resolve the algorithm Verhoeff algorithm step by step in the C programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the C programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the C programming language