Professional Documents
Culture Documents
Cover Pageof Lab Assignments Crypto
Cover Pageof Lab Assignments Crypto
Submitted by
Submitted to
April 5, 2022
COLLEGE OF APPLIED BUSINESS
Chabahil, Gangahity, Kathmandu
CERTIFICATE OF AUTHENTICATION
……………….. ……………………
DESCRIPTION:
RSA (Rivest-sharmir-adleman) is an algorithm used by modern computers to encrypto and
decrypt messages. it is an asymmetric cryptographic algorithm, asymmetric means that there
are two different keys. that is private key and public key .
ALGORITHM:
RSA algorithm uses the following procedure to generate public and private
keys:
2. Multiply these numbers to find n = p x q, where n is called the modulus for encryption
and decryption.
3. Choose a number e less than n, such that n is relatively prime to (p - 1) x (q -1). It means
that e and (p - 1) x (q - 1) have no common factor except 1. Choose "e" such that1<e<φ
4. If n = p x q, then the public key is <e, n>. A plaintext message m is encrypted using
public key <e, n>. To find cipher text from the plain text following formula is used to
ciphertext
C=me mod n Here, m must be less than n. A larger message (>n) is treated as a
concatenation of messages, each of which is encrypted separately.
5. To determine the private key, we use the following formula to calculate the d such that:
De mod{(p-1)x(q-1)}=1
Or
De mod φ (n) = 1
6. The private key is <d, n>. A ciphertext message c is decrypted using private key <d, n>.
To calculate plain text m from the ciphertext c following formula is used to get plain text
m.
m = cd mod n
PROGRAM: (RSA ALGORIHM)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main() {
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", & x);
flag = prime(x);
if (flag == 0) {
printf("\nINVALID INPUT\n");
exit(0); }
printf("\nENTER SECOND PRIME NUMBER\n");
scanf("%d", & y);
flag = prime(y);
if (flag == 0 || x == y) {
printf("\nINVALID INPUT\n");
exit(0); }
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s", msg);
for (i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = x * y;
t = (x - 1) * (y - 1);
encryption_key();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i = 0; i < j - 1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr) {
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++) {
if (pr % i == 0)
return 0; }
return 1;
}
void encryption_key() {
int k;
k = 0;
for (i = 2; i < t; i++) {
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != x && i != y) {
e[k] = i;
flag = cd(e[k]);
if (flag > 0) {
d[k] = flag;
k++; }
if (k == 99)
break;
}
}
}
long int cd(long int a) {
long int k = 1;
while (1) {
k = k + t;
if (k % a == 0)
return (k / a); }}
void encrypt() {
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;}
en[i] = -1;
printf("\n\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);}
void decrypt() {
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1) {
ct = temp[i];
k = 1;
for (j = 0; j < key; j++) {
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++; }
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
OUTPUT:
LAB-9 IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE
ALGORITHM
AIM:
Implementing Diffie Hellman Key Exchange Algorithm Whitefield Diffie and Martin
Hellman devised an solution to the problem of key agreement or key exchange in 1976. This
solution is called as Diffie-Hellman key exchange / Agreement Algorithm.
DESCRIPTION:
Diffie hellman algorithm is a public key algorithm used to established a shared secret that can
be used for secret communication while exchanging data over a public network.
Aside from using the algorithm for generating public keys, there are some other
places where DH Algorithm can be used: Encryption: The Diffie Hellman key exchange
algorithm can be used to encrypt; one of the first schemes to do is ElGamal encryption.
ALGORITHM:
1. key =(YA)XBmod q -> this is the same as calculated by B
OUTPUT: