The Wayback Machine - https://web.archive.org/web/20240910034708/https://www.geeksforgeeks.org/weak-rsa-decryption-chinese-remainder-theorem/
Open In App

Weak RSA decryption with Chinese-remainder theorem

Last Updated : 24 Jan, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 integers).

Basics :

  • RSA is a public key encryption system used for secure transmission of messages.
  • RSA involves four steps typically :
    (1) Key generation
    (2) Key distribution
    (3) Encryption
    (4) Decryption
  • Message Encryption is done with a “Public Key”.
  • Message Decryption is done with a “Private Key” – parameters (p, q, d) generated along with Public Key.
  • The private key is known only to the user, and the public key can be made known to anyone who wishes to send an encrypted message to the person with the corresponding private key.
  • A public key which is depicted by two parameters n (modulus) and e (exponent). The modulus is a product of two very large prime numbers (p and q as shown below). Decryption of this message would require the user to factorize n into two prime factors(the main reason, RSA is secure), and then find the modular inverse of e, wherein the difficult task lies.
  • A text message is first converted to the respective decimal value, which is the parameter ‘m’ which we are finding below. We now encrypt this message by doing c = pow(m, e) mod (p * q), where c is the encrypted text.

In this code, we exploit weak modulus and exponent values to try and crack the encryption by generating the private key by finding the values of p, q and d. In these examples, we will try to find d given p and q.

NOTE :
Here, in this example we are using small values of p and q but in actual we use very large values of p and q to make our RSA system secure.

Examples :

Input : 
c = 1614
e = 65537
p = 53
q = 31

Output :
1372

Explanation :
We calculate c = pow(m, e)mod(p * q). 
Insert m = 1372. 
On calculating, we get c = 1614.

Input : 
c = 3893595
e = 101
p = 3191
q = 3203

Output :
6574839

Explanation :
As shown above, if we calculate pow(m, e)mod(p * q)
with m = 6574839, we get c = 3893595

Normally, we can find the value of m by following these steps:

(1) Calculate the modular inverse of e.
We can make use of the following equation, d = e^(-1)(mod lambda(n)),
where lambda is the Carmichael Totient function.
Read: Carmichael function

(2) Calculate m = pow(c, d)mod(p * q)

(3) We can perform this calculation faster by using the Chinese Remainder Theorem,
as defined below in the function
Further reading on Chinese Remainder Theorem can be done at
RSA (cryptosystem)

Below is the Python implementation of this approach :




# Function to find the gcd of two 
# integers using Euclidean algorithm
def gcd(p, q):
      
    if q == 0:
        return p
          
    return gcd(q, p % q)
  
# Function to find the
# lcm of two integers 
def lcm(p, q):
    return p * q / gcd(p, q)
  
# Function implementing extended
# euclidean algorithm
def egcd(e, phi):
      
    if e == 0:
        return (phi, 0, 1)
    else:
        g, y, x = egcd(phi % e, e)
        return (g, x - (phi // e) * y, y)
  
# Function to compute the modular inverse
def modinv(e, phi):
      
    g, x, y = egcd(e, phi)
    return x % phi
  
  
# Implementation of the Chinese Remainder Theorem
def chineseremaindertheorem(dq, dp, p, q, c):
      
    # Message part 1
    m1 = pow(c, dp, p)
      
    # Message part 2
    m2 = pow(c, dq, q)
      
    qinv = modinv(q, p)
    h = (qinv * (m1 - m2)) % p
    m = m2 + h * q
    return m
  
# Driver Code
p = 9817
q = 9907
e = 65537
c = 36076319
d = modinv(e, lcm(p - 1, q - 1))
  
"""
pow(a, b, c) calculates a raised to power b 
modulus c, at a much faster rate than pow(a, b) % c
Furthermore, we use Chinese Remainder Theorem as it
splits the equation such that we have to calculate two
values whose equations have smaller moduli and exponent  
value, thereby reducing computing time.
"""
  
dq = pow(d, 1, q - 1)
dp = pow(d, 1, p - 1)
print chineseremaindertheorem(dq, dp, p, q, c)


Output :

41892906


Similar Reads

Rail Fence Cipher - Encryption and Decryption
Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail Fence algorithm. The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded. Examples:  EncryptionInput : "GeeksforGeeks "Key = 3Output : GsGsekfrek eoeDecryptionInput : GsGsekf
15 min read
Difference between Encryption and Decryption
Encryption is the process of converting a normal message (plain text) into a meaningless message (ciphertext). Decryption is the process of converting a meaningless message (ciphertext) into its original form (plaintext). The major distinction between secret writing and associated secret writing is the conversion of a message into an unintelligible
4 min read
RSA Algorithm using Multiple Precision Arithmetic Library
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 o
4 min read
RSA Algorithm in Cryptography
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that 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 se
15+ 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
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
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
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
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
Granovetter's Strength of Weak Ties in Social Networks
Prerequisite - Introduction to Social Networks In Social Networks there are two kinds of relationships or ties : Strong Tie : Strong relationship exists between close members with frequent interactions or meetings. Example - family members and close friends cause strong ties. Weak Tie : Weak relationship is caused by distant social relationships an
3 min read
What is Strong and Weak Collision Resistance in Cryptography?
Cryptographic hash functions are key tools in such fields as they permit the secure conversion of data into fixed-size digits. In the era of the internet when data security is very important, cryptography has a great significance in protecting non-public information. The two most vital characteristics of cryptographic hash functions include strong
5 min read
Computer Network Tutorial
A computer network is a collection of computers or devices connected to share resources. Any device which can share or receive the data is called a Node. Through which the information or data propagate is known as channels, It can be guided or unguided. In this Computer network tutorial, you’ll learn basic to advanced concepts like the Basics of co
10 min read
Difference Between OSI Model and TCP/IP Model
Data communication is a process or act in which we can send or receive data. Understanding the fundamental structures of networking is crucial for anyone working with computer systems and communication. For data communication two models are available, the OSI (Open Systems Interconnection) Model, and the TCP/IP (Transmission Control Protocol/Intern
5 min read
Advanced Encryption Standard (AES)
Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001. AES is widely used today as it is much stronger than DES and triple DES despite being harder to implement. In this article, we will cover the AES, the Working of Cipher an
8 min read
Top 50 Plus Networking Interview Questions and Answers for 2024
Networking is defined as connected devices that may exchange data or information and share resources. A computer network connects computers to exchange data via a communication media. Computer networking is the most often asked question at leading organizations such Cisco, Accenture, Uber, Airbnb, Google, Nvidia, Amazon, and many others. To get int
15+ min read
Types of Computer Networks
A computer network is a cluster of computers over a shared communication path that works to share resources from one computer to another, provided by or located on the network nodes. In this article, we will discuss computer networks and their types. What is a Computer Network?A computer network is a system that connects many independent computers
11 min read
Difference Between IPv4 and IPv6
The address through which any computer communicates with our computer is simply called an Internet Protocol Address or IP address. For example, if we want to load a web page or download something, we require the address to deliver that particular file or webpage. That address is called an IP Address. There are two versions of IP: IPv4 and IPv6. IPv
7 min read
Differences between TCP and UDP
Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) both are protocols of the Transport Layer Protocols. TCP is a connection-oriented protocol whereas UDP is a part of the Internet Protocol suite, referred to as the UDP/IP suite. Unlike TCP, it is an unreliable and connectionless protocol. In this article, we will discuss the diffe
9 min read
Types of Transmission Media
Transmission media refer to the physical pathways through which data is transmitted from one device to another within a network. These pathways can be wired or wireless. The choice of medium depends on factors like distance, speed, and interference. In this article, we will discuss the transmission media. What is Transmission Media? A transmission
8 min read
Difference between Unicast, Broadcast and Multicast in Computer Network
The cast term here signifies some data(stream of packets) is being transmitted to the recipient(s) from the client(s) side over the communication channel that helps them to communicate. Let's see some of the "cast" concepts that are prevailing in the computer networks field.  What is Unicast?This type of information transfer is useful when there is
5 min read
TCP 3-Way Handshake Process
The TCP 3-Way Handshake is a fundamental process that establishes a reliable connection between two devices over a TCP/IP network. It involves three steps: SYN (Synchronize), SYN-ACK (Synchronize-Acknowledge), and ACK (Acknowledge). During the handshake, the client and server exchange initial sequence numbers and confirm the connection establishmen
7 min read
TCP/IP Model
The TCP/IP model is a fundamental framework for computer networking. It stands for Transmission Control Protocol/Internet Protocol, which are the core protocols of the Internet. This model defines how data is transmitted over networks, ensuring reliable communication between devices. It consists of four layers: the Link Layer, the Internet Layer, t
13 min read
What is OSI Model? - Layers of OSI Model
OSI stands for Open Systems Interconnection , where open stands to say non-proprietary. It is a 7-layer architecture with each layer having specific functionality to perform. All these 7 layers work collaboratively to transmit the data from one person to another across the globe. The OSI reference model was developed by ISO - 'International Organiz
15+ min read
Basics of Computer Networking
Computer networking is a cornerstone of modern technology, enabling the interconnected systems that power the Internet, business communications, and everyday digital interactions. Understanding the fundamentals of computer networking is essential for anyone involved in technology, from enthusiasts to professionals. This article will explore the bas
12 min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. It defines how these components are connected and interact with each other. Understanding various types of network topologies helps in designing efficient and robust networks. Common types include bus, star, ring, mesh, and tree top
12 min read
Error Detection in Computer Networks
Error is a condition when the receiver's information does not match the sender's. Digital signals suffer from noise during transmission that can introduce errors in the binary bits traveling from sender to receiver. That means a 0 bit may change to 1 or a 1 bit may change to 0.  Data (Implemented either at the Data link layer or Transport Layer of
7 min read
Network Devices (Hub, Repeater, Bridge, Switch, Router, Gateways and Brouter)
Network Devices: Network devices, also known as networking hardware, are physical devices that allow hardware on a computer network to communicate and interact with one another. For example Repeater, Hub, Bridge, Switch, Routers, Gateway, Brouter, and NIC, etc. 1. Repeater - A repeater operates at the physical layer. Its job is to amplifies (i.e.,
6 min read