CMREC
LAB NAME : CRYPTOGRAPHY AND NETWORK SECURITY LAB MANUAL
II B.TECH II SEM (23-24) R22 (CSC-C SECTION)
1.AIM: Write a C program that contains a string (char pointer) with a value ‘Hello World’. The program should
XOR each character in this string with 0 and displays the result.
HARDWAREANDSOFTWAREREQUIREMENT:
1. Intel based Desktop PC: - RAM of 512 MB
2. Turbo C
THEORY:
(a) String
The string is the one-dimensional array of characters terminated by a null ('\0'). Each and every character in the
array consumes one byte of memory, and the last character must always be 0. The termination character ('\0') is
used to identify where the string ends. In C language string declaration can be done in two ways
1. By char array
2. By string literal
Let's see the example of declaring string by char array in C language.
1. charch[17]={'o','n','l','i','n','e','s','m','a','r','t','t','r','a', 'i', 'n', 'e', 'r', '\0'};
As we know, array index starts from 0,so it will be represented as in the Figure given below.
0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8
o n l i n e s m a r t t R a i n e r \
0
While declaring string, size is not mandatory. So we can write the above code as given below:
charch[]={'o','n','l','i','n','e','s','m','a','r','t','t','r','a', 'i', 'n', 'e',
'r', '\0'};
We can also define the string by the string literal in C language. For example:
2. char str[]="onlinesmarttrainer";
In such case, '\0' will be appended at the end of the string by the compiler.
(b) XOR Operation
There are two inputs and one output in binary XOR (exclusive OR) operation. It Is similar to BINARY ADD
operation which t akes two inputs and produces one result i.e. one output. The inputs and result to a binary XOR
operation can only be 0 or 1.The binary XOR operation will always produce a 1 output if either of its inputs is
1and will produce a 0 output if both of its inputs are 0 or 1.
XORTruthTable
SUM=0 Input Output
CARRY=1
X Y OR XOR
0 0 0 0
0 1 1 1
1 0 1 1
1 1 1 0
SOURCE CODE:
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
charstr[]="HelloWorld"; char
str1[11];
inti,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n"); getch();
}
OUTPUT:
VIVA QUESTIONS:
1. What is XOR Operation?
2.AIM:Write a C program that contains a string (char pointer)with a value \HelloWorld’. The program should AND
or and XOR each character in this string with 127 and display the result.
HARDWAREANDSOFTWAREREQUIREMENT:
1. Intel based Desktop PC: - RAM of 512 MB
2. TurboC/BorlandC
THEORY:
(a) String
The string is the one-dimensional array of characters terminated by a null ('\0'). Each and every character in the array
consumes one byte of memory, and the last character must always be 0. The termination character ('\0') is used to
identify where the string ends. In C language string declaration can be done in two ways
1. By char array
2. By string literal
Let's see the example of declaring string by char array in C language.
charch[17]={'o','n','l','i','n','e','s','m','a','r','t','t','r','a', 'i', 'n', 'e', 'r', '\0'};
As we know, array index starts from 0,so it will be represented as in the figure given below.
0 1 2 3 4 5 6 7 8 9 1 11 12 13 14 15 16 1
0
O N l i N e s m a r t t r a i n e
While declaring string, size is not mandatory. So we can write the above code as given below:
charch[]={'o','n','l','i','n','e','s','m','a','r','t','t','r','a', 'i', 'n', 'e', 'r', '\
0'};
We can also define the string by the string literal in C language. For example:
charstr[]="onlinesmarttrainer";
In such case, '\0' will be appended at the end of the string by the compiler.
(b) AND Operation
There are two inputs and one output in binary AND operation. The inputs and result to a binary AND operation can
only be 0 or 1.The binary AND operation will always produce a 1 output if both inputs are 1 and will produce a 0
output if both inputs are 0.For two different inputs, the output will be 0.
ANDTruthTable
Input
Output
X Y
0 0 0
0 1 0
1 0 0
1 1 1
(c) XOR Operation
There are two inputs and one output In binary XOR (exclusive OR) operation. It is similar to ADD operation which
takes two inputs and produces one result i.e. one output. The inputs and result to a binary XOR operation can only be
0 or 1.The binary XOR operation will always produce a 1 output if either of its inputs is 1and will produce a 0 output
if both of its inputs are 0 or 1.
XORTruthTable
Input
Output
X Y
0 0 0
0 1 1
1 0 1
1 1 0
SOURCE CODE:
#include<stdio.h>
//#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
//clrscr();
char str[]="HelloWorld";
char str1[11];
char str2[11]; int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]&127; printf("%c",str1[i]);
}
printf("\n"); for(i=0;i<len;i++)
{
str2[i]=str[i]^127; printf("%c",str2[i]);
}
printf("\n");
//getch();
return (0);
}
OUTPUT: