NIS MicroProject ISHA
NIS MicroProject ISHA
Micro Project
on
“Implementation of RSA Encryption”
Submitted in partial fulfillment of the requirement for the award of
Diploma of Engineering
in
Artificial Intelligence & Machine Learning
By
Nishita Rao
Pratik Gurav
This is to certify that the micro project entitled “Implementation of RSA Encryption
” has been submitted under the guidance of Mr.Pratik Gurav in partial fulfillment of
the requirement for the award of a Diploma of Engineering in Artificial Intelligence
& Machine Learning from the Maharashtra State Board of Technical Education.
PART – A PLAN
1 Brief Introduction 1
3 Action Plan 3
4 Resources Required 4
PART –B OUTCOMES
1 Brief Description 5
2 Aim of Micro-Project 14
PART-A
1.0 Brief Introduction
RSA (Rivest-Shamir-Adleman) is a widely used asymmetric encryption algorithm that ensures secure
data transmission by using a public key for encryption and a private key for decryption. It is based on
the mathematical difficulty of factoring large prime numbers, making it highly secure for digital
communications.
The RSA algorithm follows three main steps:
1. Key Generation – Two large prime numbers (ppp and qqq) are chosen, and their product (n=p×qn
= p \times qn=p×q) is used as a modulus. A public exponent (eee) is selected, and a private
exponent (ddd) is computed to satisfy modular arithmetic properties.
2. Encryption – The sender converts the message into a numerical format and applies the formula
C=Memod nC = M^e \mod nC=Memodn, where CCC is the ciphertext.
3. Decryption – The receiver, using the private key (ddd), recovers the original message using
M=Cdmod nM = C^d \mod nM=Cdmodn.
RSA is commonly used in secure communications, digital signatures, and authentication protocols.
Although it is computationally expensive, its security relies on the challenge of prime factorization.
Modern implementations optimize RSA by using hybrid encryption, where RSA encrypts a symmetric
key for faster data transmission.
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 1
RSA ENCRYPTION NIS 22620, Sem VI
The aim of this project is to implement a simplified RSA encryption system for secure data
transmission. This project demonstrates the principles of asymmetric cryptography, where a public
key encrypts data and a private key decrypts it, ensuring confidentiality and authentication in digital
communication.
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 2
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 3
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 4
RSA ENCRYPTION NIS 22620, Sem VI
PART-B OUTCOME
1. Asymmetric Encryption – Uses a public key for encryption and a private key for decryption,
ensuring secure communication.
2. Key Generation – Randomly generates two prime numbers, computes public and private keys
dynamically.
4. Encryption & Decryption – Converts text into numeric form, applies modular exponentiation,
and retrieves the original message securely
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 5
RSA ENCRYPTION NIS 22620, Sem VI
• IMPLEMENTATION IN PYTHON:-
import random
# Function to compute Greatest Common Divisor (GCD)
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Function to compute Modular Inverse (d) using Extended Euclidean Algorithm
def mod_inverse(e, phi):
for d in range(2, phi):
if (e * d) % phi == 1:
return d
return None
# Function to check if a number is prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# Function to generate a random prime number
def generate_prime():
while True:
num = random.randint(50, 200) # Choose a random number in a given range
if is_prime(num):
return num
# Function to generate RSA Public and Private Keys
def generate_keys():
p, q = generate_prime(), generate_prime()
while p == q: # Ensure p and q are distinct
q = generate_prime()
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 6
RSA ENCRYPTION NIS 22620, Sem VI
n=p*q
phi = (p - 1) * (q - 1)
# Choose e such that 1 < e < phi(n) and GCD(e, phi) = 1
e = random.randint(2, phi - 1)
while gcd(e, phi) != 1:
e = random.randint(2, phi - 1)
# Compute d (private key) using modular inverse
d = mod_inverse(e, phi)
# Main Program
public_key, private_key = generate_keys()
message = "HELLO"
ciphertext = encrypt(message, public_key)
decrypted_message = decrypt(ciphertext, private_key)
print(f"Original Message: {message}")
print(f"Encrypted Message: {ciphertext}")
print(f"Decrypted Message: {decrypted_message}")
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 7
RSA ENCRYPTION NIS 22620, Sem VI
• IMPLEMENTATION IN C:-
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function to compute GCD (Greatest Common Divisor)
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to find modular inverse using Extended Euclidean Algorithm
int modInverse(int e, int phi) {
for (int d = 2; d < phi; d++) {
if ((e * d) % phi == 1) {
return d;
}
}
return -1;
}
}
// RSA Key Generation
void generateKeys(int *e, int *d, int *n) {
int p = generatePrime();
int q = generatePrime();
while (p == q) { // Ensure p and q are different
q = generatePrime();
}
*n = p * q;
int phi = (p - 1) * (q - 1);
*e = 2;
while (*e < phi && gcd(*e, phi) != 1) {
(*e)++;
}
*d = modInverse(*e, phi);
}
// RSA Encryption
int encrypt(int msg, int e, int n) {
int cipher = 1;
for (int i = 0; i < e; i++) {
cipher = (cipher * msg) % n;
}
return cipher;
}
// RSA Decryption
int decrypt(int cipher, int d, int n) {
int msg = 1;
for (int i = 0; i < d; i++) {
msg = (msg * cipher) % n;
}
return msg;
}
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 9
RSA ENCRYPTION NIS 22620, Sem VI
int main() {
int e, d, n;
generateKeys(&e, &d, &n);
int msg;
printf("Enter a number to encrypt (numeric message): ");
scanf("%d", &msg);
return 0;
}
IMPLEMENTATION IN JAVA:
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 10
RSA ENCRYPTION NIS 22620, Sem VI
e = BigInteger.probablePrime(bitLength / 2, random);
while (phi.gcd(e).intValue() > 1) {
e = BigInteger.probablePrime(bitLength / 2, random);
}
d = e.modInverse(phi);
}
// Encrypt method
public BigInteger encrypt(BigInteger message) {
return message.modPow(e, n);
}
// Decrypt method
public BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(d, n);
}
// Get Modulus
public BigInteger getModulus() {
return n;
}
scanner.close();
}
1. Strong Security
• Based on the difficulty of prime factorization, making it nearly impossible to break with brute
force attacks.
2. Public-Key Cryptography
• Uses asymmetric encryption, where the public key encrypts data and the private key decrypts it,
ensuring secure communication.
3. Digital Signatures
• Supports authentication and integrity verification, making it useful for digital signatures and
certificates.
4. Secure Data Transmission
• Used in secure email, online transactions, VPNs, and SSL/TLS to protect sensitive information
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 12
RSA ENCRYPTION NIS 22620, Sem VI
2.0AIM of Micro-Project
The aim of this project is to implement and demonstrate RSA encryption and decryption using
asymmetric cryptography. This includes:
• Generating secure public and private keys for encryption and decryption.
• Encrypting sensitive data using the public key to ensure confidentiality.
• Decrypting the encrypted data using the private key for secure communication.
• Ensuring data integrity and authentication using RSA principles.
• Providing a practical understanding of cryptographic security and its applications in secure
messaging, digital signatures, and secure transactions.
.
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 13
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 14
RSA ENCRYPTION NIS 22620, Sem VI
1. Select Two Prime Numbers – Choose two large prime numbers p and q.
4. Choose Public Key (e) – Select e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1 (e and φ(n) are
coprime).
5. Compute Private Key (d) – Find d such that (e × d) % φ(n) = 1 (modular inverse of e).
Step 2: Encryption
1. Convert the message (M) into numeric form (if it’s text, convert characters to ASCII
values).
o C = M^e mod n
Step 3: Decryption
o M = C^d mod n
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 15
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 16
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 17
RSA ENCRYPTION NIS 22620, Sem VI
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 18
RSA ENCRYPTION NIS 22620, Sem VI
Conclusion :
VIVA COLLEGE OF DIPLOMA ENGG & TECH, Artificial Intelligence & Machine Learning . 19