NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


EXPERIMENT 1
TO IMPLEMENT HILL CIPHER
THEORY
The Hill cipher is a symmetric key encryption algorithm. It operates
on blocks of plaintext using a key matrix for encryption and its
inverse for decryption. Each block is represented as a column
vector, and the matrix multiplication is performed modulo a chosen
modulus (often 26 for the English alphabet). The security of the
Hill cipher relies on the choice and properties of the key matrix.
It provides better security than traditional substitution ciphers
but can be vulnerable if the key matrix is poorly chosen or if the
matrix size is small.
Encryption:
● Divide the plaintext into blocks, each represented as a column
vector.
● Choose a key matrix (invertible square matrix).
● Multiply the key matrix by each plaintext vector and take the
result modulo the size of the alphabet.
● The resulting vectors form the ciphertext.
Mathematically, if P is the plaintext vector, K is the key matrix,
and C is the ciphertext vector, the encryption process is
represented as C=K⋅P mod 26.
Decryption:
● Choose the inverse of the key matrix (modulus the size of the
alphabet).
● Multiply the inverse key matrix by each ciphertext vector and
take the result modulo the size of the alphabet.
● The resulting vectors form the decrypted plaintext.
Mathematically, if C is the ciphertext vector, K−1 is the inverse key
matrix, and P is the decrypted plaintext vector, the decryption
process is represented as P=K-1.C mod 26
PROGRAM
#include <stdio.h>
#include <math.h>
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] = encrypt[i][j] + a[i][k] * mes[k][j];
printf("nEncrypted string is: ");
for (i = 0; i < 3; i++)
printf("%c", (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] = decrypt[i][j] + b[i][k] * encrypt[k][j];
printf("nDecrypted string is: ");
for (i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));
printf("n");
}
void getKeyMessage()
{
int i, j;
char msg[3];
printf("Enter 3x3 matrix for key (It should be inversible):n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
}
printf("nEnter a 3 letter string: ");
scanf("%s", 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];
printf("nnInverse Matrix is:n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
printf("%d ", b[i][j]);
printf("n");
}
}
OUTPUT
CONCLUSION
Hence we learned about the hill cipher, how it works and what are
the conditions for the encryption and decryption of a hill cipher.
We wrote the program in c++ to demonstrate it and got the result as
shown in the output.
EXPERIMENT 2
TO IMPLEMENT MONOALPHABETIC CIPHER
THEORY
A monoalphabetic cipher is a type of substitution cipher where each
letter in the plaintext is consistently replaced by a corresponding
letter in the ciphertext. This substitution remains the same
throughout the entire message, creating a one-to-one mapping between
the letters of the plaintext and the letters of the cipher text.
Single character is replaced with single alternate character
throughout the whole data set. For example, if for a particular data
set A is considered as H, then at everyplace letter A is used, that
will be replaced with H.This is an easy method but to find the key
is very difficult so this is a good cipher.
Encryption:
Substitute each letter in plaintext with a corresponding letter in
the ciphertext based on a predetermined one-to-one mapping.
Decryption:
Reverse the process by substituting each letter in the ciphertext
with its corresponding letter in the plaintext using the same
mapping.
PROGRAM
#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[20], str2[20];
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 encryption..%s", str);
printf("n After encryption..%sn", str2);
}
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
Hence we learned about the Monoalphabetic cipher, how it works and
what are the conditions for the encryption and decryption. We wrote
the program in c to demonstrate it and got the result as shown in
the output.
EXPERIMENT 3
TO IMPLEMENT RAIL-FENCE CIPHER
THEORY
In the rail fence cipher, the plaintext is written downwards
diagonally on successive "rails" of an imaginary fence, then moving
up when the bottom rail is reached, down again when the top rail is
reached, and so on until the whole plaintext is written out. The
ciphertext is then read off in rows.The Rail Fence Cipher is a
transposition cipher that rearranges the characters of the plaintext
by writing it out in a pattern of diagonally aligned "rails" or
zigzags. The characters are then read off the rails to form the
ciphertext.
Encryption
● In the rail fence cipher, the plain-text is written downwards
and diagonally on successive rails of an imaginary fence.
● When we reach the bottom rail, we traverse upwards moving
diagonally, after reaching the top rail, the direction is
changed again. Thus the alphabets of the message are written
in a zig-zag manner.
● After each alphabet has been written, the individual rows are
combined to obtain the cipher-text.
Decryption
● Hence, the rail matrix can be constructed accordingly. Once
we’ve got the matrix we can figure-out the spots where texts
should be placed (using the same way of moving diagonally up
and down alternatively ).
● Then, we fill the cipher-text row wise. After filling it, we
traverse the matrix in a zig-zag manner to obtain the original
text.
PROGRAM
#include <stdio.h>
#include <string.h>
void encryptMsg(char msg[], int key)
{
int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;
char railMatrix[key][msgLen];
for (i = 0; i < key; ++i)
for (j = 0; j < msgLen; ++j)
railMatrix[i][j] = 'n';
for (i = 0; i < msgLen; ++i)
{
railMatrix[row][col++] = msg[i];
if (row == 0 || row == key - 1)
k = k * (-1);
row = row + k;
}
printf("nEncrypted Message: ");
for (i = 0; i < key; ++i)
for (j = 0; j < msgLen; ++j)
if (railMatrix[i][j] != 'n')
printf("%c", railMatrix[i][j]);
}
void decryptMsg(char enMsg[], int key)
{
int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;
char railMatrix[key][msgLen];
for (i = 0; i < key; ++i)
for (j = 0; j < msgLen; ++j)
railMatrix[i][j] = 'n';
for (i = 0; i < msgLen; ++i)
{
railMatrix[row][col++] = '*';
if (row == 0 || row == key - 1)
k = k * (-1);
row = row + k;
}
for (i = 0; i < key; ++i)
for (j = 0; j < msgLen; ++j)
if (railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("nDecrypted Message: ");
for (i = 0; i < msgLen; ++i)
{
printf("%c", railMatrix[row][col++]);
if (row == 0 || row == key - 1)
k = k * (-1);
row = row + k;
}
}
int main()
{
char msg[] = "Hello World";
char enMsg[] = "Horel ollWd";
int key = 3;
printf("Original Message: %s", msg);
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
OUTPUT:
CONCLUSION
Hence we learned about the Rail fence cipher, how it works and what
are the conditions for the encryption and decryption. We wrote the
program in c to demonstrate it and got the result as shown in the
output.
EXPERIMENT 4
TO IMPLEMENT VIGENERE CIPHER
THEORY
The Vigenere Cipher is a polyalphabetic substitution cipher that
improves upon the Caesar Cipher by using a keyword to determine the
shift value for each letter in plaintext. Unlike the Caesar Cipher,
where a single fixed key is used for the entire message, the
Vigenère Cipher employs a repeating keyword to create a series of
shift values.
Encryption:
● Choose a keyword and repeat it to match the length of the
plaintext.
● Shift each letter in the plaintext by the corresponding letter
in the repeated keyword.
● Wrap around the alphabet if necessary.
Decryption:
● Use the same keyword to determine the shift values for each
letter in the ciphertext.
● Shift each letter backward to reveal the original plaintext.
PROGRAM
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
main()
{
int i, j, k, numstr[100], numkey[100], numcipher[100];
char str[100], key[100];
printf("Enter a stringn");
gets(str);
// converting entered string to Capital letters
for (i = 0, j = 0; i < strlen(str); i++)
{
if (str[i] != ' ')
{
str[j] = toupper(str[i]);
j++;
}
}
str[j] = '';
printf("Entered string is : %s n", str);
for (i = 0; i < strlen(str); i++)
{
numstr[i] = str[i] - 'A';
}
printf("Enter a keyn");
gets(key);
// converting entered key to Capital letters
for (i = 0, j = 0; i < strlen(key); i++)
{
if (key[i] != ' ')
{
key[j] = toupper(key[i]);
j++;
}
}
key[j] = '';
// Assigning key to the string
for (i = 0; i < strlen(str);)
{
for (j = 0; (j < strlen(key)) && (i < strlen(str)); j++)
{
numkey[i] = key[j] - 'A';
i++;
}
}
for (i = 0; i < strlen(str); i++)
{
numcipher[i] = numstr[i] + numkey[i];
}
for (i = 0; i < strlen(str); i++)
{
if (numcipher[i] > 25)
{
numcipher[i] = numcipher[i] - 26;
}
}
printf("Vigenere Cipher text isn");
for (i = 0; i < strlen(str); i++)
{
printf("%c", (numcipher[i] + 'A'));
}
printf("n");
}
OUTPUT
CONCLUSION
Hence we learned about the vigenere cipher, how it works and what
are the conditions for the encryption and decryption. We wrote the
program in c to demonstrate it and got the result as shown in the
output.
EXPERIMENT 5
TO IMPLEMENT CAESAR CIPHER
THEORY
The Caesar Cipher, a fundamental substitution cipher, operates by
shifting each letter in the plaintext by a fixed amount, known as
the key, along the alphabet. This method of encryption is
characterised by its simplicity, where the displacement of each
letter remains constant throughout the entire message. The shift can
be both positive and negative, allowing for encryption and
decryption processes.
Encryption:
● Choose a fixed shift value (key).
● Shift each letter of the plaintext by the chosen key.
Decryption:
● Use the inverse of the shift to decrypt.
● In the case of a shift of 3, shift each letter in the
ciphertext three positions backward.
PROGRAM
#include <stdio.h>
#include <ctype.h>
int main()
{
char text[500], ch;
int key;
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", &key);
for (int i = 0; text[i] != ''; ++i)
{
ch = text[i];
if (isalnum(ch))
{
if (islower(ch)){
ch = (ch - 'a' + key) % 26 + 'a';
}
if (isupper(ch)){
ch = (ch - 'A' + key) % 26 + 'A';
}
if (isdigit(ch)){
ch = (ch - '0' + key) % 10 + '0';
}
}
else{
printf("Invalid Message");
}
text[i] = ch;
}
printf("Encrypted message: %s", text);
return 0;
}
OUTPUT
CONCLUSION
Hence we learned about the ceasar cipher, how it works and what are
the conditions for the encryption and decryption. We wrote the
program in c to demonstrate it and got the result as shown in the
output.
Tribhuvan University
Institute of Science and Technology
Prithvi Narayan Campus
Pokhara-1 ,Bagar
Lab Report
Of
Cryptography (CSC - 316)
Submitted by:
Sudip Poudel
Symbol no:27578
Submitted to:
Mr.Dev Timilsina
Department of Computer Science and Information
Technology.
INDEX
S.
no
Title Date of
submission
Remarks
1 TO IMPLEMENT HILL CIPHER 2080/10/04
2 TO IMPLEMENT MONOALPHABETIC
CIPHER
2080/10/04
3 TO IMPLEMENT RAIL-FENCE CIPHER 2080/10/04
4 TO IMPLEMENT VIGENERE CIPHER 2080/10/04
5 TO IMPLEMENT CAESAR CIPHER 2080/10/04
     
 
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.