0% found this document useful (0 votes)
19 views9 pages

Crypto DA

Uploaded by

pritish.khera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Crypto DA

Uploaded by

pritish.khera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Pritish Khera

RegNo: 21BCE0664

Assignment 4

Cryptography and Network Security Lab


RSA

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. RSA derives its security from the difficulty of factoring
large integers that are the product of two large prime numbers.
Multiplying these two numbers is easy, but determining the
original prime numbers from the total -- or factoring -- is
considered infeasible due to the time it would take using even
today's supercomputers. The public and private key generation
algorithm is the most complex part of RSA cryptography. Two
large prime numbers, p and q, are generated using the
RabinMiller primality test algorithm. A modulus, n, is calculated by
multiplying p and q. This number is used by both the public and
private keys and provides the link between them. Its length,
usually expressed in bits, is called the key length.
Code
#include <iostream>
#include <cmath>

using namespace std;

// Function to check if a number is prime


bool isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}

// Function to find gcd of two numbers


int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}

// Function to calculate modular exponentiation


long long power(long long x, unsigned int y, int p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}

// Function to find modular inverse


long long modInverse(long long a, int m) {
int m0 = m;
long long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}

// Function to generate keys


void generateKeys(int p, int q, int &n, int &e, int &d) {
if (!isPrime(p) || !isPrime(q)) {
cout << "Both numbers must be prime." << endl;
return;
}
if (p == q) {
cout << "Numbers must be distinct." << endl;
return;
}
n = p * q;
int phi = (p - 1) * (q - 1);
cout << "The phi value for "<< n << " is " << phi << endl;
for (e = 2; e < phi; e++) {
if (gcd(e, phi) == 1)
break;
}
cout << "The public key generated is {"<< e << ", " << n << "}" << endl;
d = modInverse(e, phi);
cout << "The private key generated is {"<< d << ", " << n << "}" << endl;
}

// Function to encrypt plaintext


long long encrypt(int plaintext, int e, int n) {
return power(plaintext, e, n);
}

// Function to decrypt ciphertext


long long decrypt(long long ciphertext, int d, int n) {
return power(ciphertext, d, n);
}

int main() {
int p, q, n, e, d;
cout << "Enter two prime numbers (p and q): ";
cin >> p >> q;
cout << "Enter plaintext to be encrypted: ";
int plaintext;
cin >> plaintext;

// Generate keys
generateKeys(p, q, n, e, d);

// Encrypt plaintext
long long encrypted = encrypt(plaintext, e, n);
cout << "Encrypted text: " << encrypted << endl;

// Decrypt ciphertext
long long decrypted = decrypt(encrypted, d, n);
cout << "Decrypted text: " << decrypted << endl;

return 0;
}

Output
MD5
MD5 is a cryptographic hash function algorithm that takes the
message as input of any length and changes it into a fixed-length
message of 16 bytes. MD5 algorithm stands for the message-
digest algorithm. MD5 was developed as an improvement of MD4,
with advanced security purposes. The output of MD5 (Digest size)
is always 128 bits. MD5 was developed in 1991 by Ronald Rivest.
Use Of MD5 Algorithm: It is used for file authentication. In a web
application, it is used for security purposes. e.g. Secure password
of users etc. Using this algorithm, We can store our password in
128 bits format
Code
import java.security.MessageDigest;
import java.util.*;
public class sha
{
public static void main(String[] args)
{
try
{
Scanner obj = new Scanner(System.in);
System.out.print("Enter the message to be encrypted -> ");
String originalMessage = obj.nextLine();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(originalMessage.getBytes());
byte[] mdBytes = md.digest();
StringBuilder sb = new StringBuilder();
for (byte mdByte : mdBytes)
{
sb.append(Integer.toString((mdByte & 0xff) + 0x100, 16).substring(1));
}
String md5Hash = sb.toString();
System.out.println("\nOriginal Message -> " + originalMessage);
System.out.println("MD5 Hash Value -> " + md5Hash);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Output
SHA
SHA-512, or Secure Hash Algorithm 512, is a hashing algorithm
used to convert text of any length into a fixed-size string. Each
output produces a SHA-512 length of 512 bits (64 bytes). This
algorithm is commonly used for email addresses hashing,
password hashing, and digital record verification. SHA-512 is also
used in blockchain technology, with the most notable example
being the BitShares network. Key Features of the SHA-512 Hash
Function • Robustness and Resistance to various cryptographic
attacks • Logical and bitwise operations • Iterative Structure •
Versatility • Resistance to Birthday Attacks
Code
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA {

// Main Function
public static String encryptThisString(String input) {
try {
// getInstance() method is called with algorithm SHA-512
MessageDigest md = MessageDigest.getInstance("SHA-512");

// digest() method is called


// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = md.digest(input.getBytes());

// Convert byte array into signum representation


BigInteger no = new BigInteger(1, messageDigest);

// Convert message digest into hex value


String hashtext = no.toString(16);

// Add preceding 0s to make it 128 bit


while (hashtext.length() < 128) {
hashtext = "0" + hashtext;
}

// return the HashText


return hashtext;
}
// For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

// Driver code
public static void main(String args[]) {
System.out.println("HashCode Generated by SHA-512 for: ");

String s1 = "My name is";


System.out.println("\n" + s1 + " : " + encryptThisString(s1));

String s2 = "Pritish Khera";


System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}

Output

You might also like