You are on page 1of 77

8.

STEPS TO INSTALL AND CONFIGURE NETWORK COMPONENTS


LIKE HUB,SWITCH, and MODEM.

Step 1: Take a router of model 1841.

Step 2: Take two hub of model Hub-PT.

Step 3: Take two switch of model 2950-24.

Step 4: Take pc0, pc1 for switch0, take pc2, pc3 for switch1.

Step 5: Connect pc0, pc1 to Switch0 with Straight through cable, connect pc2,
pc3 to Switch1 with straight through cable, and connect Switch0 to Hub0 with
cross over cable, connect Switch1 to Hub1 with cross over cable. Connect
Hub0, Hub1 to Router0 with straight through cable.

Step 6: Router configuration:

Go to Router0 → CLI
Router>enable
Router#configure terminal
Router(config)#interface fastEthernet 0/0
Router(config-if)#ip address 10.10.1.1 255.0.0.0
Router(config-if)#no shutdown
Router(config-if)#exit
Router(config)#interface fastEthernet 0/0
Router(config-if)#ip address 192.168.1.1 255.255.255.0
Router(config-if)#no shutdown

Step 7: All pc Configuration:


Go to PC0 → Desktop → IP Configuration → Static
IP Address 10.10.1.5
Subnet Mask 255.0.0.0
Default Gateway 10.10.1.1

Go to PC1 → Desktop → IP Configuration → Static


IP Address 10.10.1.6
Subnet Mask 255.0.0.0
Default Gateway 10.10.1.1

Go to PC2 → Desktop → IP Configuration → Static


IP Address 192.168.1.5
Subnet Mask 255.255.255.0
Default Gateway 192.168.1.1

Go to PC3 → Desktop → IP Configuration → Static


IP Address 192.168.1.6
Subnet Mask 255.255.255.0
Default Gateway 192.168.1.1

Implementation & Result: Sending message is successful.


Go to any pc→ Desktop→ Command Prompt

b. How do you connect to Dial-Up networking?


1. Call an internet service provider (Bell, Rogers, Wightman Telecom) to obtain
your internet service. While you are on the phone setting up your account the customer
care person should then give you a user name, phone number, and a password. This is
so you can log on securely.

1.

2. Make sure your PC is plugged in. Connect a telephone cord from the back of your
PC to the Telephone jack outlet located on the wall of the room you are in. Turn your PC
power on.
3.Go to the control panel. When your PC is finished starting up, you should be on your
main desktop page. You should also see various icons. Look for the icon that is named
"my computer". Enter my computer. When you enter you should see a small square panel
to left of your screen that says "other places". In that panel are 4 items you can click on.
Click on the one that says "control panel".
4. Go to network connections. In the control panel you will see various icons. The
control panel lets you change the settings of your computer such as; adding new
software to your computer, changing your mouse icon, adding a new user to the
computer, or in this case creating/editing your internet account. While in the
control panel, look for the icon that says "network connections". Double click the
icon to enter it.

5. Create a new connection. On this screen you will see basically nothing. Look
to the upper left hand side of your screen, you should see a small square panel
that says "network tasks". In this panel there is a small icon with the words
"create a new connection". Enter this icon.
6.A small window will pop up, just click the next button once.

7.There are 4 bulletins to choose from. Since this is a dial-up connection that is being
set up. Click on the one that says "connect to the internet". Then click the next button.
8.There are now 3 bulletins you can click on, click the one that says "set up my
connection manually". Click the next button.

9.. Another set of 3 bulletins will appear, click on "connect using a dial up
modem". Then click the next button.
10 You will now be asked to type in your internet’s ISP name. You may
name this whatever you want to name it. (Ex, Your name, last name,
nickname) Type in your ISP name then click the next button.

11.Now you will be asked to type in a phone number (The number you type in will
be the number that you got from customer care while you were setting up your
account in step 1) Type in the phone number and click the next button.
12.Now you will see 3 boxes, one asks for your user name. The other two are for your
password. Type in your user name. (You got your user name while you were on the
phone with your internet service provider in step 1) Do the same process for the
password. Once you have filled in the 3 boxes click the next button.

13
PRAC 9. Write the steps in providing network security and to set
firewall security in windows.
A. SETUP SYSTEM AND SECURITY SETTINGS.1. Set up
system and security settings
1. From the Start menu, click Control Panel, then click System and
Security
2. Under Windows Firewall, select either Check firewall status to determine
whether the firewall is turned on or off, or Allow a program through
Windows Firewall to allow a blocked program through the firewall

3. B.B
BBBBBBBBB

BBogram features
B. SELECT PROGRAM FEATURES.

1. Click Turn Windows Firewall on or off from the left side menu
2. Configure the settings for your home/work (private) or public network
3. Click OK to save your changes

3. Choose firewall settings for different network location types


C. CHOOSE FIREWALL SETTINGS FOR DIFFERENT NETWORK
LOCATION TYPES.

1. Turn on Windows Firewall for each network location you use - Home or
work (private) or Public
o Click What are network locations? for more information on
network types
o Domain network locations are controlled by your network
administrator and can't be selected or changed
2. Select Turn on Windows Firewall under the applicable network location
type (in image below, both locations are selected)
3. Select Notify me when Windows Firewall blocks a new program for
each network type, if the box is not already checked
4. Click OK to save your changes.
PRAC 10. WRITE THE STEPS FOR INSTALLATION OF System
Software, Application Software and Antivirus.

a. Installing application software from the Web

Applications like Microsoft Office and Adobe Photoshop can now be purchased
and downloaded right to your computer. You can also install free software this
way.

The installation file will be saved to your computer in .exe format.


Pronounced dot e-x-e, this is the standard extension for installation files on
Windows computers. You can follow the steps below to install an application
from an .exe file.

1. Locate and download an .exe file.


2. Locate and double-click the .exe file. (It will usually be in
your Downloads folder.)

3. A dialog box will appear. Follow the instructions to install


the software.
4. The software will be installed. You can now open the
application from the Start menu (Windows 7) or the Start
Screen (Windows 8).

b. Installing system software

Installing Windows 10

1
Create Windows Installation media. If you order Windows 10 for a new PC build, they
may send you an installation media. If you don't have a Windows installation media, you
can make one. You'll need a computer with internet access and an 8 GB USB flash drive
(16 GB recommended). You also need to know if the computer you want to install
Windows 10 on has 32-bit or 64-bit architecture. You will also need to purchase
Windows to get a product key. This can be done before or after the installation. Use the
following steps to create a Windows Installation Media:
 Insert a USB drive with at least 8 GB of space into any computer with internet
access. This will wipe any existing data on the USB drive, so be sure to back
up any data you want to keep.
 Go to https://www.microsoft.com/en-us/software-download/windows10 in a
web browser.
 Click Download Tool Now.
 Open the "MediaCreationTool.exe" file in your web browser or Downloads
folder.
 Click Accept.
 Select "Create Installation Media (USB drive, DVD, ISO file) for another PC"
and click Next.
 Select your language, Windows edition, and PC architecture and click Next.
 Select "USB flash drive" and click Next.
 Select your USB drive and click Next.

2
Back up your files (optional). If you already have a Windows installation on your
computer, a new Windows installation may overwrite your files. Back up any data you
want to keep before beginning a new Windows installation. You can back up your files to
OneDrive, Google Drive, Dropbox, or an external hard drive or USB flash drive.
3
Insert the Windows Installation Media in the computer you want to install Windows
on. You can insert the Windows Installation media into any free USB port.

4
Boot the computer. If the PC is already powered on, restart it using the normal restart
procedures of the operating system that is installed on the PC. If it is not yet powered on,
press the power button to boot it up.

5
Enter the Boot menu. Most newer computers have a boot menu you can enter while
booting up. The way you enter it is different, depending on your motherboard. The most
common way to enter the Boot menu is to press F10, F11, F12, F2, or Esc as the PC
boots up. This Boot menu will display a list of drives you can boot from.[1]
 If you are using an older PC that does not have a Boot menu, you will need
to Enter the BIOS while booting up. The buttons you press to enter the BIOS is
different from one PC manufacturer to another. It will usually say what buttons
you need to press while your computer boots up. You typically need to
press F1 F2, F3, Esc or Delete to enter the BIOS.[2] You may need to be quick
about it. You can also reboot your PC into the BIOS from inside the Windows
Settings menu.
6
Select the USB drive. This is the drive that has the Windows Installation Media. This
will boot the PC from the Windows Installation Media. Once the computer boots from the
USB drive, press any key to start the Windows installation process.
 If you are inside the BIOS settings. Locate the option that says "Boot", "Boot
order", "Priorities" or something similar. Change the boot order so that your PC
boots from the USB drive first. Then select the option to save and exit. This
will reboot your PC from the USB drive.
7
Select your language, time and currency, and keyboard input and click Next . Use
the drop-down menus to first select your language. Then select your country or region,
and your keyboard input method. Then click Next.

8
Click Install Now . It's the button in the center of the screen.

9
Enter your Windows product key and click Next . If you've already purchased
Windows, you can find your license key in your confirmation email. If you have not
purchased Windows, click I don't have a product key. You'll need to purchase
Windows and activate it later on.
10
Select which edition of Windows you want to install and click Next . If you've already
purchased Windows, check which version of Windows you purchased and make sure you
click the right version. If you have not yet purchased Windows, check which version you
want to install. Different versions of Windows have a different price.
 If you are not sure which version of Windows to install, Windows Home
Edition is the most common version of Windows.

11
Click the checkbox next to "I accept the license terms" and click Next . You can read
the license terms in the window in the center. Click next when you are ready to continue.
12
Click Custom: Install Windows only or Upgrade: Install Windows and keep files,
settings, and applications . If your computer already has a Windows installation on it,
click Upgrade: Install Windows and keep files, settings, and applications. If your
computer does not have a Windows installation or you need to reinstall Windows,
click Custom: Install Windows only.
13
Select a drive or partition you want to install Windows on and click Delete . If you
have multiple hard drives or a hard drive with multiple partitions, select the partition you
want to install Windows on and click Delete. Be aware that this will erase all data on the
drive. Make sure you have backed up all data you want to keep before continuing. This
will leave the drive with unallocated space.

14
Select a drive with unallocated space and click Next . This will start installing
Windows on the drive you selected. The amount of time it takes will depend on the
hardware of your computer.
15
Remove the USB flash drive and restart your computer. Once Windows is finished
installing, your computer will restart automatically. Remove the USB flash drive
so that it does not try to boot from the Flash drive again. The first time Windows
boots up, you will need to go through the Setup process.
c. STEPS TO installation of Antivirus.

To install an antivirus program on your computer, follow the


steps below.

1. If you purchased the antivirus program from a retail store,


insert the CD or DVD into the computer's disc drive. The
installation process should start automatically, with a window
opening to help guide you through the install process.

2. If you downloaded the antivirus program on the Internet, find


the downloaded file on your computer. If the downloaded file
is a zip file, unzip the file to extract and access the installation
files. Look for a file named setup.exe, install.exe, or
something similar, then double-click that file. The installation
process should start, with a window opening to help guide you
through the install process.

PRAC 11. What do you mean by Spooling Printers? Write the steps
for spooling printer.
Printers have a limited amount of memory, often times much smaller than the
size of a document that you are wanting to print.
Printer spooling allows you to send large documents, or multiple documents, to a
printer and not have to wait for it to finish printing before continuing on to your next task.
Steps for Spooling Printers

The print spooler helps your Windows computer interact with the printer, and orders the
print jobs in your queue. If you see any error message about the print spooler, this tool
has been corrupted or is failing to interact correctly with other software.

Restoring Default Printer State

1.

Clear the Print Queue. This will often fix the problem on its own. It is also a requirement before
continuing to the steps below.[8]

 Open the Services window (Windows key + R, type services.msc, press enter).
 Select Print Spooler and click the Stop icon, if it is not stopped already.
 Navigate to C:\Windows\system32\spool\PRINTERS and open this file. You may need
to show hidden files and/or enter an administrator's password.
 Delete all contents inside the folder. Do not delete the PRINTERS folder itself. Note
that this will remove all current print jobs, so make sure no one on your network is using
the printer.
 Return to the Services window, select Print Spooler, and click Start.
2 Update printer drivers. Your printer driver may be corrupted, causing the
spooler problems when it tries to handle faulty data from the printer.
Try updating your drivers first. If this doesn't solve the problem, continue to the
next step.
3.Delete your printer. Your printer software may be corrupted. This quick process will remove it
so you can start again with a fresh installation:[9]

 Unplug your printer or disconnect from a wireless printer.


 Search for "Devices and Printers" in the search bar, then click to open it.
 Right-click the icon for the printer that's failing to print. Click "Delete" on the drop-
down menu.

4
Delete the printer driver. The driver must be uninstalled separately. Leave your Devices and
Printers window open, and make these changes:

 Left-click any other printer icon, then click Print Server Properties on the top menu
bar.
 In the Properties window, click the Drivers tab.
 Select the driver for the deleted printer, then click Remove.
 If you choose "Remove driver and driver package," the installation package will be
deleted as well. Only do this if you know where to find a new installation package for
that driver.
5
Reinstall your printer. Plug your printer back in and follow the onscreen instructions to reinstall
the printer. If you deleted the driver package, you will also need to download a replacement. Look
for this on the printer manufacturer's website.
6
Delete reappearing printers with Print Management. If your printer or driver keep
reappearing, or fail to uninstall, this tool can sometimes do the trick. It is only available for
Windows 7 Pro/Ultimate/Enterprise and Windows 8 Pro/Enterprise. Use it as follows:[10]

 Navigate to Start → Administrative Tools → Print Management, and log in with an


administrator password. If you can't find this, try Start → Control Panel → System &
Security → Administrative Tools → Print Management.
 In the left pane, click the arrow next to Print Servers to expand the list.
 Click the arrow next to your computer (marked Local).

7
Click Printers in the left pane. Find the printer you're having trouble with in the right pane,
right-click it, and select "Delete."

 Click Drivers in the left pane. Right-click each driver used by that printer, and select
"Delete" to uninstall it. (You will not be able to uninstall it if another printer is using it.)
 Alternatively, right-click the driver and select "Remove Driver Package." This will
uninstall the driver and delete the installation package. This is sometimes necessary, but
you will not be able to reinstall the driver until you download a new installation
package.
 Connect to the printer to reinstall it. Download a new driver if you removed the driver
package.

19. Write the steps to prevent the denial of service attacks.

A distributed denial of service (DDoS) attack operates like its name implies; it’s a
form of cyber attack that disrupts connectivity or network services to deny service for
users. Attacks generally feature tools, such as a bot, that overwhelm the network with
repeated signals until it can no longer process genuine requests from users.

1. Organize a DDoS Attack Response Plan. Don’t be caught blindsided by DDoS


attacks; have a response plan ready in case of a security breach so your organization can
respond as promptly as possible. Your plan should document how to maintain business
operations if a DDoS attack is successful, any technical competencies and expertise that
will be necessary, and a systems checklist to ensure that your assets have advanced threat
detection.

Additionally, establish an incident response team in case the DDoS is successful and
define responsibilities, such as notifying key stakeholders and ensuring communication
throughout the organization.

2. Secure your Infrastructure with DDoS Attack Prevention Solutions. Equip your
network, applications, and infrastructure with multi-level protection strategies. This may
include prevention management systems that combine firewalls, VPN, anti-spam, content
filtering and other security layers to monitor activities and identity traffic inconsistencies
that may be symptoms of DDoS attacks.

If you’re looking for protection by leveraging cloud-based solutions, many providers


allow for advanced protection resources for additional charges. Other options allow for
businesses to go “full cloud,” entrusting sensitive data with a reputable cloud provider
that offers heightened security protocols, both virtual and physical.

3. Perform a Network Vulnerability Assessment. Identify weakness in your networks


before a malicious user does. A vulnerability assessment involves identifying security
exposures so you can patch up your infrastructure to be better prepared for a DDoS
attack, or for any cybersecurity risks in general.

Assessments will secure your network by trying to find security vulnerabilities. This is
done by taking inventory of all devices on the network, as well as their purpose, system
information, and any vulnerabilities associated with them, and including what devices
need to be prepared for upgrades or future assessments. Doing so will help define your
organization’s level of risk so you can optimize any security investments.

4. Identify Warning Signs of a DDoS Attack. If you can identify the symptoms of a
DDoS attack as early as possible, you can take action and hopefully mitigate damage.
Spotty connectivity, slow performance, and intermittent web crashes are all signs that
your business may be coming under attack from a DDoS criminal. Educate your team on
signs of DDoS attacks so everyone can be alert for warning signs.

Not all DDoS attacks are extensive and high volume; low-volume attacks that launch for
short durations are just as common. These attacks can be particularly nefarious because
they are more likely to go under the radar as just a random incident rather than a potential
security breach. Low-volume DDoS attacks are likely distractions for damaging malware;
while your IT security staff is distracted by a low-volume attack, malicious software like
ransomware can infiltrate your network.

5. Adopt Cloud-Based Service Providers. There are several benefits to outsourcing


DDoS attack prevention to the cloud.

Cloud providers who offer high levels of cybersecurity, including firewalls and threat
monitoring software, can help protect your assets and network from DDoS criminals. The
cloud also has greater bandwidth than most private networks, so it is likely to fail if under
the pressure of increased DDoS attacks.

Prac 14. Write the steps to transfer files between Wireless


Communication using Bluetooth and FTP.

What Is Bluetooth File Transfer?

Bluetooth file transfer is a simple way to send files to another nearby Bluetooth device
without the need for a separate app. Bluetooth is compatible with smartphones, tablets,
laptops, and desktop computers. You can transfer files over Bluetooth using Android
OS, Fire OS, Windows OS, Mac OS, and Linux OS.
While macOS supports Bluetooth, file transfers with that system are managed by
AirDrop. Windows PCs are able to send files to a mobile device (and vice versa).

1. Open a file manager (on Windows, open File Explorer) and go to the folder that
contains the file you want to send.
2. Right-click the file.

Only one file at a time can be transferred over Bluetooth.

3. Select Send To and choose Bluetooth.

4. Select Next and follow the prompts to rename the file, choose the Bluetooth
device, and send the file.
5. After several seconds, a notification appears on the receiving device.
6. Tap Accept on the receiving device to download the file.
7. Select Finish when the file transfer is complete.

B. Transfer using FTP

To transfer files using FTP, you require to an FTP server, a user account, and an FTP
client or application. Generally, most FTP servers require a username and a password in
order to make a connection.

The common methods of making an FTP connection are;

1. Username and password Login: This is a restrictive method where only users
with an account can access the server. This is common when there is a need for
users to upload or download files to and from the server.
2. Username only: Some sites only require a username to access. However, this will
only allow downloads and not uploads
3. Anonymous FTP access: This does not require the user to enter a username or
password and is the easiest. The access is usually for sites where users can only
download files such as forms, software, and others.

Creating FTP User Accounts

1. Log in to the domain cPanel


2. Navigate to Files and click FTP accounts
3. Type in the username and password for your new account in the Log
In and Password boxes. If you have several domains in your cPanel account,
ensure that you select the correct one where you want the FTP account
4. Type in a directory if you want different one from the default
5. Specify the disk space if you want something different from the default 2000MB.
You can either type in the disk space, or select unlimited at the Quota section at
the bottom.
6. Click Create FTP Account

Once the process is complete, you will see a screen that shows the login names, path, and
quota. To see the details for each FTP account, click Configure FTP client. This will
show you the FTP username, server, and port.
Using an FTP Client to Transfer Files over FTP
Connections

Moving files is much easier when using an FTP client and you may consider one if you
require regular access to the FTP servers. Using these allows you to easily upload,
download or transfer files much easier than a command line or web browser. For the
purpose of this article, we will look at two FTP clients for Windows

Winscp FTP Client for Windows

1. Download and install the WinSCP client here


2. Open the application
3. Type your FTP server name in the format ftp.server_name.com
4. Type your Host name in the format user1@server_name.com
5. Select port 21
6. Click Login
7. Click Save if you do not want the server to keep on asking you to log in every
time.
You may choose Anonymous login if the server allows it.

A screen with two panes opens up after log successful logon. By default, the left-hand
pane is the local drive while the right is the root directory of the remote FTP location.

To upload files
1. Select the files on the left-hand pane. You can select one or more files or folders
using the control and shift buttons.
2. Click the Upload drop-down button to choose whether you just want to upload
normally, in the background or and whether to transfer the files and then delete
from the source location.

To download files from the server,

1. Select folder or files on the right-hand pane


2. Click download (use the drop-down arrow to select the option)

Alternatively, transfer and then right-click, it gives the same options as clicking on the
drop-down button.
Use FTP Rush to Transfer Files Between Two Servers

The FTP Rush works almost the same way as the WinSCP. However, it has the option of
transferring files between two remote servers.

1. Download the FTP Rushhere


2. Install the application on your Windows computer
3. Open the application.

By default FTP Rush displays a local directory on the left pane and the remote on the
right pane. However, you can switch these the way you want them as well as connect to
two remote servers. If you want two remote servers, click on the computer /server on the
icons bar under the name Local.
4. To connect to remote machine, click on the (2) Remote bar.

5. At the top of the page, enter the Host name, port, FTP Username and Password,
and the Path. Click the quick connect button just before the Host, or
press Enter key to connect. Alternatively, click on the drop-down button between
the connect button and Host to open Quick Connect dialog box. Enter the details
FTP details and click OK to connect.
Alternatively, click the connect/disconnect icon and enter the details.

To copy files between different computers or server, go to the correct pane select the files
you want to upload to download and then click the Transfer button (Green arrow).
Right-clicking on the highlighted files will also give you the Transfer and other options
such as Delete, Make directory, Advanced Transfer, etc.

 To upload files, select the files from your Local pane and click the transfer arrow.

 To download files from the server to local, select the files in the Remote pane and click
transfer arrow.
To transfer files between two remote servers,

1. Go to the Local drive pane and click the icon to switch to remote.

2. Enter the FTP username and password for the second website and click OK.
3. Once you establish a connection to each server, select and transfer the files you
want to copy to the other server.
PRAC 13 : Write a program to check the strength of the password.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
char password[100],alpha,digit,special,i;
printf("Enter your Passwordn");
gets(password);
for(i=0; password[i]!=‘0’; i++)
{
if(isalpha(password[i])!=0) alpha++;
else if(isdigit(password[i]!=0)
digit++;
else special++;
}
{
if(alpha>0 && digit>0 && special>0)
printf(“Great! Your password is strongn”);
else if(alpha>0 && digit>0)
printf(“Your password is moderaten We recommend you to add
some special charactersn”); else
printf(“your password is Weak! Please Enter againn”);
}

Output
Strength of password:-Strong
12. Write a program to identify the category of IP address for a given IP
address.

Each computer which is connected to the network has an identifier to recognise


connected computer address, known as IP (Internet Protocol) Address.

An IPv4 has 4 octets having decimal value between 0 to 255 and depending on
the first octet’s value, IP Addresses divided into 5 classes: (Read more: IP Address
and Its classes)

Class A 1 to 126
Class B 128 to 191
Class C 192 to 223
Class D 224 to 239
Class E 240 to 254

In this program, we will read an IP Address and find its class.

Program to find class of an IP Address in C


#include <stdio.h>

#include <string.h>

/*

Function : extractIpAddress

Arguments :

1) sourceString - String pointer that contains ip address

2) ipAddress - Target variable short type array pointer that will store ip
address octets

*/

void extractIpAddress(unsigned char *sourceString,short *ipAddress)

unsigned short len=0;

unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];

len=strlen(sourceString);

for(i=0;i<len;i++)
{

if(sourceString[i]!='.'){

buf[cnt++] =sourceString[i];

if(sourceString[i]=='.' || i==len-1){

buf[cnt]='\0';

cnt=0;

oct[cnt1++]=atoi(buf);

ipAddress[0]=oct[0];

ipAddress[1]=oct[1];

ipAddress[2]=oct[2];

ipAddress[3]=oct[3];

int main()

unsigned char ip[20]={0};

short ipAddress[4];

printf("Enter IP Address (xxx.xxx.xxx.xxx format): ");

scanf("%s",ip);

extractIpAddress(ip,&ipAddress[0]);
printf("\nIp Address: %03d. %03d. %03d.
%03d\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);

if(ipAddress[0]>=0 && ipAddress[0]<=127)

printf("Class A Ip Address.\n");

if(ipAddress[0]>127 && ipAddress[0]<191)

printf("Class B Ip Address.\n");

if(ipAddress[0]>191 && ipAddress[0]<224)

printf("Class C Ip Address.\n");

if(ipAddress[0]>224 && ipAddress[0]<=239)

printf("Class D Ip Address.\n");

if(ipAddress[0]>239)

printf("Class E Ip Address.\n");

return 0;
}

Output

Enter IP Address (xxx.xxx.xxx.xxx format): 145.160.017.001

Ip Address: 145. 160. 017. 001


Class B Ip Address.

16.Write a program to search the given pattern using Optimized


Algorithm.

C program for A modified Naive Pattern Searching

algorithm that is optimized for the cases when all

characters of pattern are different */


#include <stdio.h>

#include <string.h>

void search(char pat[], char txt[])

int M = strlen(pat);

int N = strlen(txt);

int i = 0;

while (i <= N - M) {

int j;

/* For current index i, check for pattern match */

for (j = 0; j < M; j++)

if (txt[i + j] != pat[j])

break;

if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]

printf("Pattern found at index %d \n", i);

i = i + M;

else if (j == 0)

i = i + 1;

else
i = i + j; // slide the pattern by j

/* Driver program to test above function */

int main()

char txt[] = "ABCEABCDABCEABCD";

char pat[] = "ABCD";

search(pat, txt);

return 0;

}
Output:
Pattern found at index 4
Pattern found at index 12

Prac 35. Write an algorithm and program to implement Data


Encryption Standard(DES) for encryption and Decryption.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

int IP[] =
{
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};

int E[] =
{
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};

int P[] =
{
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};

int FP[] =
{
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};

int S1[4][16] =
{
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
};

int S2[4][16] =
{
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
};

int S3[4][16] =
{
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
};

int S4[4][16] =
{
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
};

int S5[4][16] =
{
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
};

int S6[4][16] =
{
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
};

int S7[4][16]=
{
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
};

int S8[4][16]=
{
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};

int PC1[] =
{
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
int PC2[] =
{
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};

int SHIFTS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };

FILE* out;
int LEFT[17][32], RIGHT[17][32];
int IPtext[64];
int EXPtext[48];
int XORtext[48];
int X[8][6];
int X2[32];
int R[32];
int key56bit[56];
int key48bit[17][48];
int CIPHER[64];
int ENCRYPTED[64];

void expansion_function(int pos, int text)


{
for (int i = 0; i < 48; i++)
{
if (E[i] == pos + 1) {
EXPtext[i] = text;
}
}
}

int initialPermutation(int pos, int text)


{
int i;
for (i = 0; i < 64; i++)
{
if (IP[i] == pos + 1) {
break;
}
}
IPtext[i] = text;
}

int F1(int i)
{
int r, c, b[6];

for (int j = 0; j < 6; j++) {


b[j] = X[i][j];
}

r = b[0] * 2 + b[5];
c = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];

if (i == 0) {
return S1[r][c];
}
else if (i == 1) {
return S2[r][c];
}
else if (i == 2) {
return S3[r][c];
}
else if (i == 3) {
return S4[r][c];
}
else if (i == 4) {
return S5[r][c];
}
else if (i == 5) {
return S6[r][c];
}
else if (i == 6) {
return S7[r][c];
}
else if (i == 7) {
return S8[r][c];
}
}

int XOR(int a, int b) {


return (a ^ b);
}

int ToBits(int value)


{
int k, j, m;
static int i;

if (i % 32 == 0) {
i = 0;
}

for (j = 3; j >= 0; j--)


{
m = 1 << j;
k = value & m;
if (k == 0) {
X2[3 - j + i] = '0' – 48;
}
else {
X2[3 - j + i] = '1' – 48;
}
}

i = i + 4;
}

int SBox(int XORtext[])


{
int k = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 6; j++) {
X[i][j] = XORtext[k++];
}
}

int value;
for (int i = 0; i < 8; i++)
{
value = F1(i);
ToBits(value);
}
}

int PBox(int pos, int text)


{
int i;
for (i = 0; i < 32; i++)
{
if (P[i] == pos + 1) {
break;
}
}
R[i] = text;
}

void cipher(int Round, int mode)


{
for (int i = 0; i < 32; i++) {
expansion_function(i, RIGHT[Round – 1][i]);
}

for (int i = 0; i < 48; i++)


{
if (mode == 0) {
XORtext[i] = XOR(EXPtext[i], key48bit[Round][i]);
}
else {
XORtext[i] = XOR(EXPtext[i], key48bit[17 – Round][i]);
}
}

SBox(XORtext);

for (int i = 0; i < 32; i++) {


PBox(i, X2[i]);
}

for (int i = 0; i < 32; i++) {


RIGHT[Round][i] = XOR(LEFT[Round – 1][i], R[i]);
}
}

void finalPermutation(int pos, int text)


{
int i;
for (i = 0; i < 64; i++)
{
if (FP[i] == pos + 1) {
break;
}
}
ENCRYPTED[i] = text;
}

void convertToBinary(int n)
{
int k, m;
for (int i = 7; i >= 0; i--)
{
m = 1 << i;
k = n & m;

if (k == 0) {
fprintf(out, "0");
}
else {
fprintf(out, "1");
}
}
}

int convertCharToBit(long int n)


{
FILE* inp = fopen("input.txt", "rb");
out = fopen("bits.txt", "wb+");
char ch;
int i = n * 8;

while (i)
{
ch = fgetc(inp);
if (ch == -1) {
break;
}
i--;
convertToBinary(ch);
}
fclose(out);
fclose(inp);
}

void Encryption(long int plain[])


{
out = fopen("cipher.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}

for (int i = 0; i < 32; i++) {


LEFT[0][i] = IPtext[i];
}

for (int i = 32; i < 64; i++) {


RIGHT[0][i – 32] = IPtext[i];
}
for (int k = 1; k < 17; k++)
{
cipher(k, 0);

for (int i = 0; i < 32; i++)


LEFT[k][i] = RIGHT[k – 1][i];
}

for (int i = 0; i < 64; i++)


{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
}
else {
CIPHER[i] = LEFT[16][i – 32];
}
finalPermutation(i, CIPHER[i]);
}

for (int i = 0; i < 64; i++) {


fprintf(out, "%d", ENCRYPTED[i]);
}
fclose(out);
}

void Decryption(long int plain[])


{
out = fopen("decrypted.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}

for (int i = 0; i < 32; i++) {


LEFT[0][i] = IPtext[i];
}

for (int i = 32; i < 64; i++) {


RIGHT[0][i – 32] = IPtext[i];
}

for (int k = 1; k < 17; k++)


{
cipher(k, 1);

for (int i = 0; i < 32; i++) {


LEFT[k][i] = RIGHT[k – 1][i];
}
}

for (int i = 0; i < 64; i++)


{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
} else {
CIPHER[i] = LEFT[16][i – 32];
}
finalPermutation(i, CIPHER[i]);
}

for (int i = 0; i < 64; i++) {


fprintf(out, "%d", ENCRYPTED[i]);
}

fclose(out);
}

void convertToBits(int ch[])


{
int value = 0;
for (int i = 7; i >= 0; i--) {
value += (int)pow(2, i) * ch[7 – i];
}
fprintf(out, "%c", value);
}

int bittochar()
{
out = fopen("result.txt", "ab+");
for (int i = 0; i < 64; i = i + 8) {
convertToBits(&ENCRYPTED[i]);
}
fclose(out);
}

void key56to48(int round, int pos, int text)


{
int i;
for (i = 0; i < 56; i++)
{
if (PC2[i] == pos + 1) {
break;
}
}
key48bit[round][i] = text;
}

int key64to56(int pos, int text)


{
int i;
for (i = 0; i < 56; i++)
{
if (PC1[i] == pos + 1) {
break;
}
}
key56bit[i] = text;
}

void key64to48(unsigned int key[])


{
int k, backup[17][2];
int CD[17][56];
int C[17][28], D[17][28];

for (int i = 0; i < 64; i++) {


key64to56(i, key[i]);
}

for (int i = 0; i < 56; i++)


{
if (i < 28) {
C[0][i] = key56bit[i];
}
else {
D[0][i – 28] = key56bit[i];
}
}

for (int x = 1; x < 17; x++)


{
int shift = SHIFTS[x – 1];

for (int i = 0; i < shift; i++) {


backup[x - 1][i] = C[x – 1][i];
}

for (int i = 0; i < (28 – shift); i++) {


C[x][i] = C[x – 1][i + shift];
}

k = 0;
for (int i = 28 – shift; i < 28; i++) {
C[x][i] = backup[x – 1][k++];
}

for (int i = 0; i < shift; i++) {


backup[x - 1][i] = D[x – 1][i];
}

for (int i = 0; i < (28 – shift); i++) {


D[x][i] = D[x – 1][i + shift];
}

k = 0;
for (int i = 28 – shift; i < 28; i++) {
D[x][i] = backup[x – 1][k++];
}
}

for (int j = 0; j < 17; j++)


{
for (int i = 0; i < 28; i++) {
CD[j][i] = C[j][i];
}

for (int i = 28; i < 56; i++) {


CD[j][i] = D[j][i – 28];
}
}

for (int j = 1; j < 17; j++)


{
for (int i = 0; i < 56; i++) {
key56to48(j, i, CD[j][i]);
}
}
}

void decrypt(long int n)


{
FILE* in = fopen("cipher.txt", "rb");
long int plain[n * 64];
int i = -1;
char ch;

while (!feof(in))
{
ch = getc(in);
plain[++i] = ch – 48;
}

for (int i = 0; i < n; i++)


{
Decryption(plain + i * 64);
bittochar();
}

fclose(in);
}

void encrypt(long int n)


{
FILE* in = fopen("bits.txt", "rb");

long int plain[n * 64];


int i = -1;
char ch;

while (!feof(in))
{
ch = getc(in);
plain[++i] = ch – 48;
}

for (int i = 0; i < n; i++) {


Encryption(plain + 64 * i);
}

fclose(in);
}

void create16Keys()
{
FILE* pt = fopen("key.txt", "rb");
unsigned int key[64];
int i = 0, ch;

while (!feof(pt))
{
ch = getc(pt);
key[i++] = ch – 48;
}
key64to48(key);
fclose(pt);
}

long int findFileSize()


{
FILE* inp = fopen("input.txt", "rb");
long int size;

if (fseek(inp, 0L, SEEK_END)) {


perror("fseek() failed");
}
// size will contain number of chars in the input file.
else {
size = ftell(inp);
}
fclose(inp);

return size;
}

int main()
{
// destroy contents of these files (from previous runs, if any)
out = fopen("result.txt", "wb+");
fclose(out);

out = fopen("decrypted.txt", "wb+");


fclose(out);

out = fopen("cipher.txt", "wb+");


fclose(out);

create16Keys();

long int n = findFileSize() / 8;


convertCharToBit(n);

encrypt(n);
decrypt(n);

return 0;
}

OUTPUT FILE:

result.txt – IT WILL CONTAIN OUR DECRYPTED TEXT.

TEMP FILES:

bits.txt – IT WILL CONTAIN OUR PLAIN TEXT CONVERTED IN


BITS.
cipher.txt – IT WILL CONTAIN OUR ENCRYPTED TEXT IN BITS.
decrypted.txt – IT WILL CONTAIN OUR DECRYPTED TEXT IN BITS

Prac 29. Write a program to download a file from HTTP server.

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>

int main(void)
{
char domain[] = "www.sstatic.net",
path[]="stackexchange/img/logos/so/so-logo-med.png"; //example
int sock, bytes_received;
char send_data[1024],recv_data[9999], *p;
struct sockaddr_in server_addr;
struct hostent *he;
FILE *fp;

he = gethostbyname(domain);
if (he == NULL){
herror("gethostbyname");
exit(1);
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0))== -1){
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,sizeof(struct sockaddr))
== -1){
perror("Connect");
exit(1);
}

snprintf(send_data, sizeof(send_data), "GET /%s HTTP/1.1\r\nHost:


/%s\r\n\r\n", path, domain);
//printf("%s\n", send_data);
send(sock, send_data, strlen(send_data), 0);
printf("Data sended.\n");

fp=fopen("received_file","wb");

bytes_received = recv(sock, recv_data, 9999, 0);


recv_data[bytes_received] = '\0';
printf("Data receieved.\n");
printf("%s\n", recv_data);

p = strstr(recv_data, "\r\n\r\n"); //to find "\r\n\r\n" sequence and put the


pointer p after that
p=p+4;

fwrite(p,strlen(p),1,fp);

close(sock);
fclose(fp);

return 0;
}

Prac 33. Write an algorithm and program for encrypting a plain text
and decrypting a cipher text using Caeser Cipher.

What is Caesar Cipher?

It is one of the simplest encryption technique in which each character in plain text is replaced
by a character some fixed number of positions down to it.

For example, if key is 3 then we have to replace character by another character that is 3
position down to it. Like A will be replaced by D, C will be replaced by F and so on.
Encryption

#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
Output

Enter a message to encrypt: axzd


Enter key: 4
Encrypted message: ebdh

Decryption

#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);
return 0;
}
Output

Enter a message to decrypt: ebdh


Enter key: 4
Decrypted message: axzd

Prac 21: What is malware? Write the steps to remove the malware
from your PC.

Malware is intrusive software that is designed to damage and destroy


computers and computer systems. Malware is a contraction for
“malicious software.” Examples of common malware includes viruses,
worms, Trojan viruses, spyware, adware, and ransomware.

1. Step 1: Disconnect from the internet


Disconnecting from the internet will prevent more of your data from being
sent to a malware server or the malware from spreading further.

Step 2: Enter safe mode


Safe mode, often referred to as safe boot, is a way to start your computer so
that it performs checks and allows only the minimum required software and
programs to load. If malware is set to load automatically, this will prevent
the malware from doing so, making it easier to remove. To enter safe mode:

1. Start (or restart) your Mac, then immediately press and hold the Shift
key. The Apple logo will appear on your display.
2. Release the Shift key when you see the login window (if you are
asked to log in twice, learn more about what to do here).

Step 3: Check your activity monitor for malicious applications


If you know that you’ve installed a suspicious update or application, close
the application if it’s running. You can do so by using your activity monitor.
This shows the processes that are running on your computer, so you can
manage them and see how they affect your computer’s activity and
performance.
Step 4: Run a malware scanner
malware scanners can remove most standard infections.

Step 5: Verify your browser’s homepage


It’s common for malware to modify your web browser’s homepage to re-
infect your Mac. Check your homepage and connection settings using the
steps below for common browsers.
To verify your homepage on Chrome:

1. In the top right corner of your Chrome browser, click More →


Settings.
2. Select the dropdown menu in the “Search engine” section.
3. Verify your default homepage.

PRAC 38 : Write an algorithm and a program to implement Diffie


Hellman Key.
The Diffie-Hellman algorithm is used to establish a shared secret between
two parties that can be used for secret communication to exchange data
over a public network.
/* This program calculates the Key for two persons

using the Diffie-Hellman Key exchange algorithm */

#include<stdio.h>

#include<math.h>

// Power function to return value of a ^ b mod P

long long int power(long long int a, long long int b,

long long int P)

if (b == 1)

return a;

else

return (((long long int)pow(a, b)) % P);

//Driver program

int main()

long long int P, G, x, a, y, b, ka, kb;

// Both the persons will be agreed upon the

// public keys G and P

P = 23; // A prime number P is taken

printf("The value of P : %lld\n", P);

G = 9; // A primitive root for P, G is taken


printf("The value of G : %lld\n\n", G);

// Alice will choose the private key a

a = 4; // a is the chosen private key

printf("The private key a for Alice : %lld\n", a);

x = power(G, a, P); // gets the generated key

// Bob will choose the private key b

b = 3; // b is the chosen private key

printf("The private key b for Bob : %lld\n\n", b);

y = power(G, b, P); // gets the generated key

// Generating the secret key after the exchange

// of keys

ka = power(y, a, P); // Secret key for Alice

kb = power(x, b, P); // Secret key for Bob

printf("Secret key for the Alice is : %lld\n", ka);

printf("Secret Key for the Bob is : %lld\n", kb);

return 0;

}
Output:
The value of P : 23
The value of G : 9

The private key a for Alice : 4


The private key b for Bob : 3

Secret key for the Alice is : 9


Secret Key for the Bob is : 9

PRAC 39 : Write an RSA algorithm and program to implement Digital


Signature Scheme.

Digital Signature :
As the name sounds are the new alternative to sign a document digitally.
It ensures that the message is sent by the intended user without any
tampering by any third party (attacker). In simple words, digital signatures
are used to verify the authenticity of the message sent electronically.
RSA :
It is the most popular asymmetric cryptographic algorithm. It is primarily
used for encrypting message s but can also be used for performing digital
signature over a message.
An RSA algorithm is an important and powerful algorithm in
cryptography. It is widely used in Digital Signature and in an SSL. The
algorithm works in the following way

1. Select at random two LARGE prime number p and q.


2. Multiply p and q i.e. n=p∗q.
3. Select a small odd integer e which is relatively prime to phi(n),
where phi(n)=(p−1)∗(q–1).
4. Calculate the value of d (explained below). d is calculated as a
modular multiplicative inverse of e modulo n.
5. Publish the pair P=(e,n) as a public key.
6. Keep secret the pair S=(d,n) as a private key.
The message is encrypted using P=(e,n) using following formula.
P(M)=Me mod n
The message can be decrypted using P=(d,n) using following formula.
S(C)=Cd mod n
Secret key pair (d,n) should be kept secret.

To calculate the value of d, we use the various number theories from


mathematics. In step 3 of the algorithm, we select e which is relatively
prime to phi(n). This means the GCD of e and phi(n) is always 1. i.e.
GCD(e,ϕ(n))=1
Let x and y be two parameters such that they satisfy following
mathematical expression,
GCD(a,b)=d=ax+by
If we replace value of a and b with e and phi(n) respectively, we get
GCD(e,ϕ(n))=1=ex+ϕ(n)y
We already know the values of e and phi and now we can use
the Extended Euclid’s Algorithm to get the value of x and y. We can re-
arrange the above expression as below
ex+ϕ(n)y=1
Taking ( mod ϕ(n)) on both side
ex+ϕ(n)y≡1( mod ϕ(n))
Since ϕ(n)y( mod ϕ(n)) is 0, the final expression becomes
ex≡1( mod ϕ(n))
x is now called a Modular Multiplicative Inverse of e.
We can finally solve for d using a Modular Linear Equation Solver which
uses Extended Euclid’s Algorithm.

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

typedef struct {

int d;

int x;

int y;

} EE;

EE extended_euclid(int a, int b) {

EE ee1, ee2, ee3;

if (b == 0) {

ee1.d = a;

ee1.x = 1;
ee1.y = 0;

return ee1;

} else {

ee2 = extended_euclid(b, a % b);

ee3.d = ee2.d;

ee3.x = ee2.y;

ee3.y = ee2.x - floor(a / b) * ee2.y;

return ee3;

// Copied from

// https://stackoverflow.com/questions/11720656/modulo-operation-with-
negative-numbers

int modulo(int x, int N){

return (x % N + N) % N;

void decimal_to_binary(int op1, int aOp[]){

int result, i = 0;

do{
result = op1 % 2;

op1 /= 2;

aOp[i] = result;

i++;

}while(op1 > 0);

int modular_exponentiation(int a, int b, int n){

int *bb;

int count = 0, c = 0, d = 1, i;

// find out the size of binary b

count = (int) (log(b)/log(2)) + 1;

bb = (int *) malloc(sizeof(int*) * count);

decimal_to_binary(b, bb);

for (i = count - 1; i >= 0; i--) {

c = 2 * c;

d = (d*d) % n;

if (bb[i] == 1) {
c = c + 1;

d = (d*a) % n;

return d;

int get_d(int e, int phi){

EE ee;

ee = extended_euclid(e, phi);

return modulo(ee.x, phi);

int main(int argc, char* argv[]) {

int p, q, phi, n, e, d, m, c;

printf("Enter the value of p: ");

scanf("%d", &p);

printf("Enter the valeu of q: ");

scanf("%d", &q);

n = p*q;

phi = (p - 1) * (q - 1);
printf("Enter the value of e: ");

scanf("%d", &e);

d = get_d(e, phi);

printf("Public Key: (n = %d, e = %d)\n", n, e);

printf("Private Key: (n = %d, d = %d)\n", n, d);

printf("Enter message to encrypt: ");

scanf("%d", &m);

c = modular_exponentiation(m, e, n);

printf("Encrypted message is: %d\n", c);

m = modular_exponentiation(c, d, n);

printf("Message is decrypted to %d\n", m);

return 0;

You might also like