NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io



S.N Experiment Date of submission Remarks
1. TO WRITE A PROGRAM TO IMPLEMENT CAESAR CIPHER. 2080/06/25
2. TO WRITE A PROGRAM TO IMPLEMENT HILL CIPHER. 2080/06/25
3. TO WRITE A PROGRAM TO IMPLEMENT VIGENERE CIPHER. 2080/06/25
4. TO WRITE A PROGRAM TO IMPLEMENT MONOALPHABETIC CIPHER. 2080/06/25
5. TO WRITE A PROGRAM TO IMPLEMENT RAIL FENCE CIPHER. 2080/06/25









LAB 1

OBJECTIVE: TO WRITE A PROGRAM TO IMPLEMENT CAESAR CIPHER.

THEORY:

Caesar cipher is the simple shift monoalphabetic classical cipher where each letter is replaced by a letter 3 position (actual Caesar cipher) ahead using the circular alphabetic ordering i.e. letter after Z is A.
We number each English alphabet starting from 0(A) to 25 (Z). Each letter of the clear message is replaced by the letter whose number is obtained by adding the key (a number form 0 to 25) to the letter's number modulo 26. The encryption can also be represented using modular arithmetic by first transforming the letters into numbers according to the scheme, A=0, B=1, …., Z=25. Encryption of a letter c by a shift k can be described mathematically as,

c= Ek(m)= (m+k) mod26

Decryption is performed similarly,
m= Dk(c) = (c+26-k) mod26

DEMONSTRATION:
 Source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char plain_text[20], cipher_text[20];
int key, i, length;
int result;
printf("Enter the plain text:-t");
scanf("%s", & plain_text);
printf("Enter the key value:-t");
scanf("%d",&key);
printf("The plain text is %s",plain_text);
printf("nThe encrypted text is:-t");
length= strlen(plain_text);
for(i=0; i<length; i++)
{
cipher_text[i]= plain_text[i] + key;
if ((cipher_text[i])>'Z' && (cipher_text[i]>
'z'))
cipher_text[i]= cipher_text[i] -26;
printf("%c",cipher_text[i]);
}
printf("nThe decrypted text is:-t");
for (i=0;i<length;i++)
{
plain_text[i]= cipher_text[i]-key;
if((plain_text[i]<'A')&& (plain_text[i]<'a'))
plain_text[i]= plain_text[i] +26;
printf("%c",plain_text[i]);
}
return 0;
}

 Output:



CONCLUSION:
Thus, encryption and decryption using caesar cipher was successfully implemented in the lab.













LAB 2

OBJECTIVE: TO WRITE A PROGRAM TO IMPLEMENT A HILL CIPHER.

THEORY:
Another interesting multi letter cipher is the Hill cipher. This encryption algorithm takes m successive plaintext letters and substitutes for them m ciphertext letters. The substitution is determined by m linear equations in which each character is assigned a numerical value (a=0, b=1,…, z=25).
For example, consider the plaintext "paymoremoney" and use the encryption key
17 17 5
K = 21 18 21
2 2 19

The first three letters of the plaintext are represented by the vector
15 15 375 11
0 then K. 0 = 819 mod 26 = 13 =LNS
24 24 487 18

The ciphertext for the entire plaintext is LNSHDLEWMTRW. Hence in general the hill cipher can be expressed as:
C= E (K, P) = KP mod 26
P= D (K, P) = K-1C mod 26 = K-1KP = P

DEMONSTRATION:
 Source code:
#include<iostream>
#include<math.h>
using namespace std;
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption(); //encrypts the message
void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix
int main() {
getKeyMessage();
encryption();
decryption();
return 0;
}
void encryption() {
int i, j, k;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 1; j++)
{
for(k = 0; k < 3; k++)
{
// encrypt[i][j]=0;
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
}}}
cout<<"nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 97);
}
void decryption() {
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
{
for(j = 0; j < 1; j++)
{
for(k = 0; k < 3; k++)
{
// decrypt[i][j]=0;
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
}}}
cout<<"nDecrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 97);//char a value is 97 and z's is 122
cout<<"n";
}
void getKeyMessage() {
int i, j;
char msg[3];
cout<<"Enter 3x3 matrix for key (It should be invertible):n";
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];
}}
cout<<"nEnter a 3 letter string: ";
cin>>msg;
for(i = 0; i < 3; i++)
mes[i][0] = msg[i] - 97;
}
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
b[i][j] = b[i][j] / c[i][i];
}}
cout<<"nnInverse Matrix is:n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
{
cout<<b[i][j]<<" ";
}
cout<<"n";
}
}








 Output:


CONCLUSION:
Thus, encryption and decryption using hill cipher was successfully implemented in the lab.


















LAB 3

OBJECTIVE: TO WRITE A PROGRAM TO IMPLEMENT A VIGENERE CIPHER.

THEORY:
Vigenere cipher is a method of encrypting alphabetic text. It uses a simple form of polyalphabetic substitution. The encryption of the original text is done using Vigenere square table. The table consists of the alphabets written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar Ciphers. At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword. For encryption, a pair of key alphabet(seen in the columns) and plaintext alphabet (seen in the rows) are matched and the corresponding alphabet will be our encrypted alphabet.
For example, for the message THE BOY HAS THE BALL and the key VIG, encipher using Caesar cipher for each letter:
Key: VIGVIGVIGVIGVIGV
Plain Text: THEBOYHASTHEBALL
Cipher Text: OPKWWECIYOPKWIRG

DEMONSTRATION:
 Source code:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void vigenere(char *, char *);
void encipher();
void decipher();
void main()
{
int option;
while(1)
{
printf("n 1. Encipher!!");
printf("n 2. Decipher!!");
printf("n 3. Exit n");
printf("n Enter your option: ");
scanf("%d",&option);
fflush(stdin);
if(option==3)
exit(0);
else if(option==1)
encipher();
else if(option==2)
decipher();
else
printf("n Invalid selection!!Try again!! ");
}
}
void encipher()
{
unsigned int i,j; //unsigned integer can represent only non negative integers
char plain[127],key[15];
printf("n Enter the plaintext (maximum 128 characters): ");
gets(plain);
printf("n Enter the key (maximum 16 characters): ");
gets(key);
for(i=0,j=0; i<strlen(plain); i++,j++)
{
if(j>=strlen(key))
{
j=0;
}
printf("%c",65+(((toupper(plain[i])-
65)+(toupper(key[j])-65))%26));
//plaintext+key%26 gives encrypted characters
}
}
void decipher()
{
unsigned int i,j;
char plain[127],key[15];
int value;
printf("n Enter the ciphertext: ");
gets(plain);
printf("n Enter the key: ");
gets(key);
for(i=0,j=0; i<strlen(plain); i++,j++)
{
if(j>=strlen(key))
{
j=0;
}
value=(toupper(plain[i])-64)-(toupper(key[j])-
64);
if(value<0)
{
value=value+26;
}
printf("%c",65+(value%26));
}
}

 Output:


CONCLUSION:
Thus, encryption and decryption using vigenere cipher was successfully implemented in the lab.











LAB 4

OBJECTIVE: TO WRITE A PROGRAM TO IMPLEMENT A MONOALPHABETIC CIPHER.

THEORY:
A monoalphabetic cipher is any cipher in which the letters of the plain text are mapped to cipher text letters based on a single alphabetic key. Examples of monoalphabetic ciphers would include the Caesar-shift cipher, where each letter is shifted based on a numeric key, and the atbash cipher, where each letter is mapped to the letter symmetric to it about the center of the alphabet. The relationship between a character in the plaintext and the characters in the cipher text is one-to-one. This type of cipher is a form of symmetric encryption as the same key can be used to both encrypt and decrypt a message.
For example, if the keyword is ANDREW DICKSON WHITE, then the cipher alphabet is given by
plain: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
cipher: A N D R E W I C K S O H T B F G J L M P Q S T U V W X Y

DEMONSTRATION:
 Source code:
#include<stdio.h>
char monocipher_encr(char);
char alpha[27][3] = { { 'a', 'f' }, { 'b', 'a' }, { 'c', 'g' }, { 'd', 'u' }, {
'e', 'n' }, { 'f', 'i' }, { 'g', 'j' }, { 'h', 'k' }, { 'i', 'l' }, {
'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
'y', 'e' }, { 'z', 'h' } };
//char str[20];
int main() {
char str[30], str2[30];
int i;
printf("n Enter String..");
gets(str);
for (i = 0; str[i]; i++) {
str2[i] = monocipher_encr(str[i]);
}
str2[i] = '';
printf("n Before Decryption..%s", str);
printf("n After Decryption..%sn", str2);
return 0;
}
char monocipher_encr(char a) {
int i;
for (i = 0; i < 27; i++) {
if (a == alpha[i][0])
break;
}
return alpha[i][1];
}

 Output:


CONCLUSION:
Thus, encryption and decryption using monoalphabetic cipher was successfully implemented in the lab.

















LAB 5

OBJECTIVE: TO WRITE A PROGRAM TO IMPLEMENT A RAIL FENCE CIPHER.

THEORY:
The Rail Fence Cipher is a form of transposition cipher that derives its name from the way in which it is encoded. In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows.
For example, if we have 3 rails and a message of THIS IS THE PLAINTEXT, then the cipher text is TIEIXHSSHPANETITLT which is evaluated as follows:

T I E I X
H S S H P A N E T
I T L T

DEMONSTRATION:
 Source code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i,j,len,rails,count,code[100][1000];
char str[1000];
printf("Enter a Secret Messagen");
gets(str);
len=strlen(str);
printf("Enter number of railsn");
scanf("%d",&rails);
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
code[i][j]=0;
}
}
count=0;
j=0;
while(j<len)
{
if(count%2==0)
{
for(i=0;i<rails;i++)
{
//strcpy(code[i][j],str[j]);
code[i][j]=(int)str[j];
j++;
}
}
else
{
for(i=rails-2;i>0;i--)
{
code[i][j]=(int)str[j];
j++;
}
}
count++;
}
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
if(code[i][j]!=0)
printf("%c",code[i][j]);
}
}
printf("n");
}
 Output:

CONCLUSION:
Thus, encryption and decryption using rail fence cipher was successfully implemented in the lab.

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.