0% found this document useful (0 votes)
10 views4 pages

Lab7 F

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)
10 views4 pages

Lab7 F

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/ 4

EXPERIMENT-07

AIM : Write a C Program to implement Cipher text using Asymmetric Key


Ciphering and Deciphering and also implement RSA Algorithm for Modern
cipher method.
APPARATUS : Scilab
THEORY :
CODE :
// Function to compute greatest common divisor
(GCD)

function g=gcd(a, b)

while b <> 0 do

temp = b;

b = modulo(a, b);

a = temp;

end

g = a;

endfunction

// Function to compute modular inverse

function d=modInverse(e, phi_n)

for d = 1:phi_n

if modulo(e * d, phi_n) == 1 then

return

end

end

endfunction

// Function for modular exponentiation

function result=modExp(base, exp, mod)

result = 1;

base = modulo(base, mod);

while exp > 0 do

if modulo(exp, 2) == 1 then

result = modulo(result * base, mod);

end

exp = floor(exp / 2);

base = modulo(base * base, mod);

end

endfunction
// Function to generate RSA keys

function [e, d, n]=generateKeys(p, q)

n = p * q;

phi_n = (p - 1) * (q - 1);

// Choose e such that 1 < e < phi_n and gcd(e,


phi_n) = 1

e = 2;

while gcd(e, phi_n) <> 1 do

e = e + 1;

end

// Compute d as the modular inverse of e mod


phi_n

d = modInverse(e, phi_n);

endfunction

// Function to encrypt a message using public key


(e, n)

function C=encrypt(M, e, n)

C = modExp(M, e, n);

endfunction

// Function to decrypt a ciphertext using private


key (d, n)

function M=decrypt(C, d, n)

M = modExp(C, d, n);

endfunction

// Example usage of RSA encryption/decryption

// Choose two prime numbers

p = 61;

q = 53;

// Generate RSA keys

[e, d, n] = generateKeys(p, q);


// Display keys

disp("Public key (e, n): "), disp(e), disp(n);

disp("Private key (d, n): "), disp(d), disp(n);

// Encrypt a message (as a number)

M = 65; // Message to encrypt (ASCII code for 'A')

disp("Original message: "), disp(M);

C = encrypt(M, e, n);

disp("Encrypted ciphertext: "), disp(C);

// Decrypt the ciphertext

decryptedMessage = decrypt(C, d, n);

disp("Decrypted message: "),


disp(decryptedMessage);

OUTPUT :

CONCLUSIONS :
The C program successfully implements RSA encryption and decryption, demonstrating the
fundamental operations of key generation, encryption, and decryption. The encryption and
decryption processes correctly transform a message into an encrypted form and back to its original
value. This example highlights the practical application of RSA for secure message transmission.

You might also like