Cryptography & Network Security Lab B.Tech., Semester - VII Subject Code: RIT-751
Cryptography & Network Security Lab B.Tech., Semester - VII Subject Code: RIT-751
Group/Branch: IT 1(A2)
JSS MAHAVIDYAPEETHA
DEPARTMENT OF INFORMATION TECHNOLOGY
JSSACADEMY OF TECHNICAL EDUCATION
C-20/1, SECTOR-62, NOIDA
EXPERIMENT 1
OBJECTIVE:
Implement the encryption and decryption of 8-bit data using ‘Simplified DES Algorithm’
BRIEFDESCRIPTION:
DES means Data Encryption Standard. DES is one of the top cryptographic software security algorithm
used for providing security in many information systems. This c programming tutorial will help you to
generate secure password (encryption key).
ALGORITHM/FLOWCHART:
Source code:
#include<stdio.h>
int main()
{
int i, cnt=0, p8[8]={6,7,8,9,1,2,3,4};
int p10[10]={6,7,8,9,10,1,2,3,4,5};
char input[11], k1[10], k2[10], temp[11];
char LS1[5], LS2[5];
//k1, k2 are for storing interim keys
//p8 and p10 are for storing permutation key
//Applying p10...
for(i=0; i<10; i++)
{
cnt = p10[i];
temp[i] = input[cnt-1];
}
temp[i]='\0';
printf("\nYour p10 key is :");
for(i=0; i<10; i++)
{ printf("%d,",p10[i]); }
//Applying p8...
for(i=0; i<8; i++)
{
cnt = p8[i];
k1[i] = temp[cnt-1];
}
printf("\nYour key k1 is :");
puts(k1);
//This program can be extended to generate k2 as per DES algorithm.
}
Output of program
LAB EXPERIMENT 2
OBJECTIVE:
Implement ‘Linear Congruential Algorithm’ to generate 5 pseudo-random numbers in ‘C’.
BRIEF DESCRIPTION:
ALGORITHM:
Source code:
1. #include <stdio.h>
2.
3. /* always assuming int is at least 32 bits */
4. int rand();
5. int rseed = 0;
6.
7. inline void srand(int x) {
8. rseed = x;
9. }
Output:
$ gcc LCG.c
$ ./a.out
LAB EXPERIMENT 3
OBJECTIVE:
Implement Rabin-Miller Primality Testing Algorithm in ‘C’.
BRIEF DESCRIPTION:
The Miller–Rabin primality test or Rabin–Miller primality test is a primality test: an algorithm which
determines whether a given number is prime. Miller's version of the test is deterministic, but the
correctness relies on the unproven extended Riemann hypothesis. Michael O. Rabin modified it to obtain
an unconditional probabilistic algorithm.
Source code:
1. #include <stdio.h>
2. #include <string.h>
3. #include <stdlib.h>
4. * calculates (a * b) % c taking into account that a * b might overflow*/
5. long long mulmod(long long a, long long b, long long mod)
6. {
7. long long x = 0,y = a % mod;
8. while (b > 0)
9. {
10. if (b % 2 == 1)
11. {
12. x = (x + y) % mod;
13. }
14. y = (y * 2) % mod;
15. b /= 2;
16. }
17. return x % mod;
18. }
19. /*
20. * modular exponentiation
21. */
22. long long modulo(long long base, long long exponent, long long mod)
23. {
24. long long x = 1;
25. long long y = base;
26. while (exponent > 0)
27. {
28. if (exponent % 2 == 1)
29. x = (x * y) % mod;
30. y = (y * y) % mod;
31. exponent = exponent / 2;
32. }
33. return x % mod;
34. }
35.
36. /*
37. * Miller-Rabin Primality test, iteration signifies the accuracy
38. */
LAB EXPERIMENT 4
OBJECTIVE:
Implement the Euclid Algorithm to generate the GCD of an array of 10 integers in ‘C’.
BRIEF DESCRIPTION:
The Euclidean ALGORITHM, or Euclid's ALGORITHM, is an efficient method for computing the greatest
common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a
remainder. It is named after the ancient Greek mathematician Euclid, who first described it in Euclid's
Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a
calculation according to well-defined rules and is one of the oldest algorithms in common use. It can be
used to reduce fractions to their simplest form, and is a part of many other number-theoretic and
cryptographic calculations.
Source code:
#include <bits/stdc++.h>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
{
result = gcd(arr[i], result);
if(result == 1)
{
return 1;
}
}
return result;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 6, 8, 16 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findGCD(arr, n) << endl;
return 0;
}
Output:
LAB EXPERIMENT 5
OBJECTIVE
Implement RSA algorithm for encryption and decryption in ‘C’.
BRIEF DESCRIPTION:
The RSA cryptosystem is the most widely-used public key cryptography algorithm in the world. It can be
used to encrypt a message without the need to exchange a secret key separately. The RSA
algorithm can be used for both public key encryption and digital signatures. Its security is based on the
difficulty of factoring large integers. Party A can send an encrypted message to party B without any prior
exchange of secret keys. A just uses B's public key to encrypt the message and B decrypts it using the
private key, which only he knows. RSA can also be used to sign a message, so A can sign a message using
their private key and B can verify it using A's public key.
ALGORITHM:
The RSA algorithm involves four steps: key generation, key distribution, encryption and decryption.
Key generation
1. Generate two large random primes, p and q, of approximately equal size such that their product n =
pq is of the required bit length, e.g. 1024 bits.
2. Compute n = pq and (phi) φ = (p-1)(q-1).
3. Choose an integer e, 1 < e < phi, such that gcd(e, phi) = 1.
4. Compute the secret exponent d, 1 < d < phi, such that ed ≡ 1 (mod phi).
5. The public key is (n, e) and the private key (d, p, q). Keep all the values d, p, q and phi secret. [It is
preferred sometimes to write the private key as (n, d) because there is a need of the value n when
using d. Otherwise write the key pair as ((N, e), d).]
INPUT: Required modulus bit length, k. Select a value of e from {3, 5, 17, 257, 65537}
1. repeat
2. p ← genprime(k/2)
3. until (p mod e) ≠ 1
4. repeat
5. q ← genprime(k - k/2)
6. until (q mod e) ≠ 1
7. N ← pq
8. L ← (p-1)(q-1)
9. d ← modinv(e, L)
10. return(N, e, d)
Encryption
Decryption
Source code:
#include<stdio.h>
#include<math.h>
int temp;
while(1)
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
int main()
double p = 3;
double q = 7;
double n=p*q;
double count;
//public key
double e=2;
while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
//private key
double d;
double k = 2;
d = (1 + (k*totient))/e;
double c = pow(msg,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
printf("\np = %lf",p);
printf("\nq = %lf",q);
printf("\nn = pq = %lf",n);
printf("\ntotient = %lf",totient);
printf("\ne = %lf",e);
printf("\nd = %lf",d);
return 0;
OUTPUT:
p = 3.000000
q = 7.000000
n = pq = 21.000000
totient = 12.000000
e = 5.000000
d = 5.000000
LAB EXPERIMENT 6
OBJECTIVE:
Configure a mail agent to support Digital Certificates, send a mail and verify the correctness of this system
using the configured parameters.
BRIEF DESCRIPTION:
A digital certificate is a digital form of identification, like a passport. A digital certificate provides
information about the identity of an entity. A digital certificate is issued by a Certification Authority (CA).
Examples of trusted CA across the world are Verisign, Entrust, etc.
.
In Understanding Digital Signatures article, it was assumed that the receiver knows the Public Key of the
sender. In fact, the issue of distributing Public Key is massive, because the Public Key should be
distributed in a scalable way as well as be trusted as the true Public Key of the sender. These problems are
solved when a user obtains another user's Public Key from the digital certificate.
ALGORITHM:
1. Generate Key-pair: User-A generates a Public and Private key-pair or is assigned a key-pair by some
authority in their organization.
2. Request CA Certificate: User-A first requests the certificate of the CA Server.
3. CA Certificate Issued: The CA responds with its Certificate. This includes its Public Key and its
Digital Signature signed using its Private Key.
4. Gather Information: User-A gathers all information required by the CA Server to obtain its
certificate. This information could include User-A email address, fingerprints, etc. that the CA needs to
be certain that User-A claims to be who she is.
5. Send Certificate Request: User-A sends a certificate request to the CA consisting of her Public Key
and additional information. The certificate request is signed by CA's Public Key.
6. CA verifies User-A: The CA gets the certificate request, verifies User-A's identity and generates a
certificate for User-A, binding her identity and her Public Key. The signature of CA verifies the
authenticity of the Certificate.
7. CA issues the Certificate: The CA issues the certificate to User-A.
• Open Outlook
• Select Tools from menu
• Select Options from drop down menu
• In dialog box that appears select Security tab
• Enter a name for the security setting into the Security Settings Name box
• Ensure S/MIME is selected on the Secure Message Format box
• Check the Default Security Setting for this Secure Message Format
• In Certificates and ALGORITHMs section click the Choose button in the Signing
Certificate section
• Select the Secure Email Certificate from the Select Certificate dialog box
• Outlook should automatically choose the same Secure Email Certificate as your Signing
• Certificate for the Encryption Certificate. If not, click the Choose button in the Encryption
• Certificate and select Secure Email Certificate from the Select Certificate dialog box
• Ensure Send These Certificates with Signed Messages is selected
• Click OK to return to Options dialog box
• Click OK to return to Outlook.
• Setting up buttons for easy signing / encryption abilities from a New Message toolbar:
• Following these steps will display digital sign and encrypt buttons on the New Message toolbar:
• Click New Message button
• Select Tools from menu
• Select Customize from drop down menu
• Select the Commands tab
• Select the Standard from the Categories listings
• Scroll down the Commands list on the right to locate Encrypt Message Contents and
Attachments. Click on the entry.
• Using the mouse, drag the highlighted Encrypt Message Contents and Attachments listing
onto your Toolbar. It is recommended to place it next to the Send button.
• Repeat the steps 6 & 7 to also add the Digitally Sign Message listing.
Signing an Email:
Signing an email ensures the recipient knows the email has come from recipient and informs him
/ her if it has been tampered with since being signed.
Compose recipient email and attach files as usual
The incoming email to the recipient must have a copy of recipient Certificate in order to verify recipient
signed email is legitimate. Ensure email receiver to assign the recipient certificate to incoming mail
account.
Encrypting an Email:Encrypting an email ensures that only the recipient may view the email content
and any attachments.
Ensure the recipient has a Digital Certificate and recipient have assigned the Certificate to their entry in
recipient Outlook contacts area
LAB EXPERIMENT 7
OBJECTIVE:
Configure SSH (Secure Shell) and send/receive a file on this connection to verify the correctness of
this system using the configured parameters.
BRIEF DESCRIPTION:
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over
an unsecured network. The best-known example application is for remote login to computer systems by
users.SSH provides a secure channel over an unsecured network in a client-server architecture,
connecting an SSH client application with an SSH server. Common applications include remote
command-line login and remote command execution, but any network service can be secured with
SSH. The protocol specification distinguishes between two major versions, referred to as SSH-1 and
SSH-2.
SSH was designed as a replacement for Telnet and for unsecured remote shell protocols such as the
Berkeley rlogin, rsh, and rexec protocols. Those protocols send information, notably passwords, in
plaintext, rendering them susceptible to interception and disclosure using packet analysis.The
encryption used by SSH is intended to provide confidentiality and integrity of data over an unsecured
network, such as the Internet, although files leaked by Edward Snowden indicate that the National
Security Agency can sometimes decrypt SSH, allowing them to read the contents of SSH sessions.
FLOWCHART
LAB EXPERIMENT 8
OBJECTIVE:
Configure a firewall to block the following for 5 minutes and verify the correctness of this system using the
configured parameters:
(a) Two neighborhood IP addresses on your LAN
(b) All ICMP requests
BRIEF DESCRIPTION:
Setting up a firewall for the infrastructure is a great way to provide some basic security for the services.
Once it has been developed, the next step is to test the firewall rules. It is important to get a good idea of
whether firewall rules are doing.
To monitor the number of existing ad-hoc clients on a wireless LAN, to identify devices that have set their
own fixed addresses in the DHCP range or to take inventory of the devices currently connected to your
network, you can ping each IP address in the subnet. The list of devices that respond to the ping is a good
starting place for accomplishing any of these tasks.
The Internet Control Message Protocol (ICMP) is a supporting protocol in the Internet protocol suite. It
is used by network devices, including routers, to send error messages and operational information
indicating, for example, that a requested service is not available or that a host or router could not be
reached. ICMP differs from transport protocols such as TCP and UDP in that it is not typically used to
exchange data between systems, nor is it regularly employed by end-user network applications
ALGORITHM:
This means clients will now only be able to resolve the DNS records, and is allowed it through host DNS
server (these servers can forward requests on to external servers).