The Wayback Machine - https://web.archive.org/web/20241008165539/https://www.geeksforgeeks.org/rsa-algorithm-using-multiple-precision-arithmetic-library/
Open In App

RSA Algorithm using Multiple Precision Arithmetic Library

Last Updated : 03 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Public Key Cryptography is also known as Asymmetric Cryptography is the type of cryptography which involves the usage of two keys namely Public Key and Private Key. The Public Key of the receiver is used to encrypt the plaintext by the sender while the Private Key of the receiver is used to decrypt the encrypted message and hence can be decrypted only by the intended receiver.
RSA (Rivest Shamir Adleman) is a public key cryptographic algorithm in which key generation is based on the product of two large prime numbers p and q resulting into N i.e. N = p × q. The crux of the security of this algorithm is that the attacker needs to first find out p and q by factorization of N which takes place in exponential time. Research shows that this can take more than 70 years if N is a 100 digit number. Due to this complexity, the attacker is unable to find the decryption key d because d depends on p, q and encryption key e. So even if N and e are known to the attacker, d cannot be computed.


Current Scenario of RSA :
As of April 2017, RSA-2048 may not be factorizable for many years to come. Moreover, the recent Ransomware virus also uses RSA-2048 to encrypt files on the infected system. These files cannot be decrypted without the availability of the decryption key because such a large key cannot be factorized. Hence, RSA-1024 and RSA-2048 are now being popularly used for secured communication. 


\\ \textbf{\hspace{4cm} Key generation:} \\ Select \hspace{0.2cm} p, q \hspace{5cm} p,q \hspace{0.2cm}both \hspace{0.2cm} prime\\ calculate \hspace{0.2cm} n = p*q \\ calculate \hspace{0.2cm}\phi(n) = (p-1)*(q-1) \\ select \hspace{0.2cm}integer \hspace{0.2cm}e \hspace{4cm} gcd(\phi(n),e)=1; 1<e<\phi(n)\\ calculate \hspace{0.2cm} d\\ Public Key \hspace{5cm} KU = {e,n}\\ PrivateKey \hspace{4.7cm} KR = {d,n}
 

Rsa Example


The above images show the 3 different phases of the RSA algorithm. Considering that a prime number generator generates 1024-bit primes p and q, the resultant N will be a 2048-bit number. Since all modulo operations performed during encryption and decryption are with respect to N which is a 2048-bit number, the software implementation of this can be time-consuming. Moreover, the unsigned long long int data type of C restricts computations to 64-bit numbers. In order to support the generation of large-sized keys as a requirement of RSA algorithm and fast computation of encryption and decryption involving large size modulus, a library called the GMP (GNU Multiple Precision Arithmetic Library) can be used. The use of this library will allow the entire RSA algorithm to run on a simple 64-bit operating system thereby avoiding the use of super-computers or high configuration hardware devices.
 

What is GMP?
GMP is an open source library that allows arithmetic computations to be performed on signed integers, rational numbers and decimal numbers without any practical limitations on its precision apart from the configurations of the machine it is run on. This library is used during those arithmetic calculations that involve very large or high precision numbers, most of which are used in cryptographic algorithms. The benefit of using this library is that it provides support for arbitrary precision numbers, the size of which is not known prior to execution of the program. The basic interface provided for the use of this library is for the C language. But wrappers exist for other languages including Ada, C++, C#, Julia, OCaml, Perl, PHP, Python, R, Ruby and the Wolfram Language.
As an example, a C program that performs the multiplication of two multiple precision numbers is mentioned below. 
Note that the gmp.h file needs to be included as a header file. The compilation of such a program on a Unix system can be done using the command 
 

gcc program_name.c -lgmp


 

C

#include <stdio.h>
#include <gmp.h>
 
int main(void) {
   mpz_t x, y, result;
 
   mpz_init_set_str(x, "7612058254738945", 10);
   mpz_init_set_str(y, "9263591128439081", 10);
   mpz_init(result);
 
   mpz_mul(result, x, y);
   gmp_printf("    %Zd\n"
              "*\n"
              "    %Zd\n"
              "--------------------\n"
              "%Zd\n", x, y, result);
 
   /* free used memory */
   mpz_clear(x);
   mpz_clear(y);
   mpz_clear(result);
   
   return 0;
}

                    

Coding the RSA Algorithm :
A C program depicting the working of RSA algorithm with small prime numbers is available here. In order to understand the working of the real RSA algorithm with the use of large prime numbers, a C code using GMP library is available here. This program implements RSA-1024 by generating random prime numbers p and q of 512 bits each followed by encryption and decryption. Here, the variable MODULUS_SIZE is assigned the value 1024. This value can be changed to 2048 to generate RSA keys of 2048 bits. 
 

Applications :
RSA algorithm has been widely used in secure network communications and secure transactions for many e-commerce applications. Other applications include voice communication over low bit rate channels, in secure key exchange for even high-speed IPSec, credit card details when communicated to online merchants etc.
 



Similar Reads

How to solve RSA Algorithm Problems?
RSA algorithm is an asymmetric cryptography algorithm which means, there should be two keys involve while communicating, i.e., public key and private key. There are simple steps to solve problems on the RSA Algorithm. Example-1: Step-1: Choose two prime number [Tex]p [/Tex]and [Tex]q [/Tex]Lets take [Tex]p = 3 [/Tex]and [Tex]q = 11 [/Tex]Step-2: Co
8 min read
Difference between RSA algorithm and DSA
In cryptography, the two commonly used algorithms in modern cryptography for secure data transmission and to ensure the signatures of digital signatures, are the Rivest-Shamir-Adleman (RSA) algorithm and Digital Signature Algorithm (DSA). We'll learn about RSA and DSA, how they work when are they used, and their differences. What is RSA?RSA stands
8 min read
RSA Algorithm in Cryptography
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric means that it works on two different keys i.e. Public Key and Private Key. As the name describes the Public Key is given to everyone and the Private key is kept private. An example of asymmetric cryptography: A client (for example browser) sends its public key to the server and reque
15+ min read
Weak RSA decryption with Chinese-remainder theorem
Prerequisite : RSA Algorithm Why RSA decryption is slow ? RSA decryption is slower than encryption because while doing decryption, private key parameter " d " is necessarily large. Moreover the parameters - " p and q " are two very large Prime Numbers. Given integers c, e, p and q, find m such that c = pow(m, e) mod (p * q) (RSA decryption for weak
4 min read
RSA Blind Signatures
Blind signatures are a form of digital signature in which the content of the message is hidden (blinded) before it is signed by any of the authorities involved in the process. This technique provides invisibility to the person who made the message. It is mainly used when the author and the signer are different parties. Blind signatures can be imple
8 min read
Chosen Ciphertext Attacks on RSA
In the domain of cryptography, a chosen ciphertext attack (CCA) is a significant threat, particularly to public-key cryptosystems. This article aims to resolve the complexities of chosen-ciphertext attacks, exploring their implications and impact on various cryptographic schemes. Terminology on Cipher Text AttacksChosen-Ciphertext Attack (CCA): A c
3 min read
RSA and Digital Signatures
RSA and digital signatures are crucial elements in modern cybersecurity. RSA, a widely used encryption algorithm, ensures secure data transmission by encrypting and decrypting information. Digital signatures, on the other hand, authenticate the identity of the sender and guarantee the integrity of the message. Together, RSA and digital signatures p
8 min read
Security of RSA
Rivest-Shamir-Adleman (RSA) encryption is a foundational element of modern cryptography, crucial for securing data transmission over the internet. Developed by Rivest, Shamir, and Adleman, RSA relies on the mathematical difficulty of factoring large prime numbers, making it a robust public-key cryptosystem. It's widely used in applications like HTT
4 min read
Difference Between AES and RSA Encryption
In cryptography, this is the process of changing or transforming plain text or information into a cipher text or a text that the receiver, who is the person to whom the information is intended, can only decode. There are several algorithms in data encryption such as AES and RSA. AES represents a symmetric key algorithm whose processes depend on the
6 min read
Difference Between Diffie-Hellman and RSA
Cyber Security is the branch of computers dealing with networks and technology to protect the computer system from unauthorized third-party users which aim at accessing and stealing user information and contribute to causing damage to the system. Cyber security intentions at implementing best practices to save computers/ laptops/ mobiles from any k
6 min read
Precision Time Protocol (PTP)
Prerequisite – Protocols in Application Layer Precision Time Protocol (PTP) is a protocol that promotes the synchronization of clocks throughout a computer network. This protocol is used to synchronize clocks of different types of devices. PTP was led by John Edison in 1588 for the standardization and was published in the year 2002. This is require
2 min read
Connecting to an SFTP Server using Java JSch Library
SFTP (Secure File Transfer Protocol) is a secure way to transfer files between a client and a server. It is similar to FTP (File Transfer Protocol) but is more secure as it uses SSH (Secure Shell) to encrypt the data being transferred. In this article, we will learn how to connect to an SFTP server using the Java programming language. To connect to
3 min read
Tokenization Using Spacy library
Before moving to the explanation of tokenization, let's first discuss what is Spacy. Spacy is a library that comes under NLP (Natural Language Processing). It is an object-oriented Library that is used to deal with pre-processing of text, and sentences, and to extract information from the text using modules and functions. Tokenization is the proces
3 min read
Large number arithmetic using doubly linked list
Given two very large numbers in form of strings. Your task is to apply different arithmetic operations on these strings. Prerequisite : Doubly Linked List. Examples: Input : m : 123456789123456789123456789123456789123456789123456789 n : 456789123456789123456789123456789123456789123456789 Output : Product : 563937184884934839205932493526930147847927
13 min read
TCL script to perform appropriate arithmetic operations using switch statement
In this article, we will cover the overview of TCL script to perform appropriate arithmetic operations using the switch statement and will implement with the help of an example. Let's discuss it one by one. Pre-requisite -If you want to know more about TCL, then kindly go through this article, https://www.geeksforgeeks.org/basics-of-ns2-and-otcltcl
3 min read
Information Technology Infrastructure Library (ITIL)
Information Technology Infrastructure Library (ITIL) is provided with a framework of best practices for delivering IT services. The ITIL is an appropriate method to management of IT service and also management can help businesses manage risk, strengthen customer relations, establish cost-effective practices, and build a stable IT environment that p
3 min read
How to Use Picasso Image Loader Library in Android?
Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the
6 min read
Software Framework vs Library
Many of us will be unaware of this difference which is really important to understand during development. The possible answer to this question, if asked, will be "Framework is a collection of various libraries". However, this definition is not entirely true. "Who calls whom" i.e. the caller/callee relationship defines the difference between the two
4 min read
C Library math.h Functions
The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. Let us discuss some important C math functions one by one. C Math Functions1. double ceil (double x) The C library function double ceil (double x) returns the smallest inte
6 min read
How to Use Glide Image Loader Library in Android Apps?
Glide, like Picasso, can load and display images from many sources, while also taking care of caching and keeping a low memory impact when doing image manipulations. Official Google apps (like the app for Google I/O 2015) are using Glide. In this article, we're going to explore the functionalities of Glide and why it's superior in certain aspects.
4 min read
<strings> library in C++ STL
Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return iterator to end (public member function )Advance :
3 min read
Containers in C++ STL (Standard Template Library)
A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements. The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference ob
2 min read
<numeric> library in C++ STL
Common mathematical functions std::fabs: This function returns the absolute value. std::sqrt: This function returns the square root std::sin: This function returns the sine measured in radians. Special mathematical functions std::beta: This function evaluates the (complete) Beta integral with given parameters std::hermite: This function Returns the
2 min read
<regex> library in C++ STL
Main classes These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters. basic_regex: Regular expression object (class template) sub_match: Identifies the sequence of characters matched by a sub-expression (class template) match_results: Identifies one regular expression ma
2 min read
Data Compression With Arithmetic Coding
Content removed on request. Please see Computer Network for more computer network articles.
1 min read
Arithmetic instructions in 8086 microprocessor
Arithmetic Instructions are the instructions which perform basic arithmetic operations such as addition, subtraction and a few more. Unlike in 8085 microprocessor, in 8086 microprocessor the destination operand need not be the accumulator. Following is the table showing the list of arithmetic instructions: OPCODE OPERAND EXPLANATION EXAMPLE ADD D,
2 min read
Longest arithmetic progression that can be formed with the given common difference d
Given an unsorted array a[] of size n and an integer d which is the common difference, the task is to find the length of the longest AP that can be formed for all j, greater than some i(<n), if a[j] = a[i] + (j-i) * d i.e. a[j] is in the AP of a[i] from index i to j. Examples: Input: n = 6, d = 2 arr[] = {1, 2, 5, 7, 9, 85} Output: 4 The longest
7 min read
Java Arithmetic Operators with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Arithmetic Operator
6 min read
Character Arithmetic in C
As already known character range is between -128 to 127 or 0 to 255. This point has to be kept in mind while doing character arithmetic. What is Character Arithmetic?Character arithmetic is used to implement arithmetic operations like addition, subtraction, multiplication, and division on characters in C language. In character arithmetic character
2 min read
Java | CDMA (Code Division Multiple Access)
CDMA is a channelization protocol for Multiple Access, where information can be sent simultaneously through several transmitters over a single communication channel. It is achieved in below steps: A signal is generated which extends over a wide bandwidth. The code which performs this action is called spreading code. Later on, a specific signal can
6 min read
Practice Tags :