ISS-Practicals KRUTIKA
ISS-Practicals KRUTIKA
PRACTICAL NO. 01
IMPLEMENTATION OF CAESAR CIPHER
AIM: To implement the simple substitution technique named Caesar cipher using C language.
DESCRIPTION:
To encrypt a message with a Caesar cipher, each letter in the message is changed using a simple rule:
shift by three. Each letter is replaced by the letter three letters ahead in the alphabet. A becomes D, B
becomes E, and so on. For the last letters, we can think of the alphabet as a circle and "wrap around".
W becomes Z, X becomes A, Y bec mes B, and Z becomes C. To change a message back, each letter
is replaced by the one three before it.
EXAMPLE:
ALGORITHM:
STEP-1: Read the plain text from the user.
STEP-2: Read the key value from the user.
STEP-3: If the key is positive then encrypt the text by adding the key with each
character in the plain text.
STEP-4: Else subtract the key from the plain text.
STEP-5: Display the cipher text obtained above.
int key;
// Taking user input.
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
// Visiting character by character.
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {
//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// Invalid character.
else {
printf("Invalid Message");
}
// Adding encoded answer.
text[i] = ch;
}
printf("Encrypted message: %s", text);
return 0;
}
OUTPUT:
PRACTICAL NO. 02
IMPLEMENTATION OF PLAYFAIR CIPHER
EXAMPLE:
Example1:Plaintext: INFORMATION TECHONOLOGY Key: IDEAL Ciphertext:?
Grouped text: IN FO RM AT IO NT EC HO NO LO GY
Ciphertext: EK GN MC YO AK OS DF GP MN AP AT
I/J D E A L
B C F G H
K M N O P
Q R S T U
V W X Y Z
ALGORITHM:
STEP-1: Read the plain text from the user.
STEP-2: Read the keyword from the user.
STEP-3: Arrange the keyword without duplicates in a 5*5 matrix in the row order and
fill the remaining cells with missed out letters in alphabetical order. Note that
‘i’ and ‘j’ takes the same cell.
STEP-4: Group the plain text in pairs and match the corresponding corner letters by
forming a rectangular grid.
STEP-5: Display the obtained cipher text.
}
void searching(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
int mod5(int a)
{
if (a < 0)
a += 5;
return (a % 5);
}
void decrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
searching(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
void decrypt_by_playfair_cipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key text
ks = strlen(key);
ks = remove_all_spaces(key, ks);
change_to_lowercase(key, ks);
// ciphertext
ps = strlen(str);
change_to_lowercase(str, ps);
ps = remove_all_spaces(str, ps);
generate_key(key, ks, keyT);
decrypt(str, keyT, ps);
}
int main()
{
char str[SIZE], key[SIZE];
OUTPUT:
RESULT: Thus the Playfair cipher substitution technique had been implemented successfully.
PRACTICAL NO.03
IMPLEMENTATION OF HILL CIPHER
AIM: IMPLEMENTATION OF HILL CIPHER To write a C program to implement the hill cipher
substitution techniques.
DESCRIPTION:
Each letter is represented by a number modulo 26. Often the simple scheme A = 0, B = 1... Z = 25, is
used, but this is not an essential feature of the cipher. To encrypt a message, each block of n letters is
multiplied by an invertible n × n matrix, against modulus 26. To decrypt the message, each block is
multiplied by the inverse of the m trix used for encryption. The matrix used for encryption is the
cipher key, and it sho ld be chosen randomly from the set of invertible n × n matrices (modulo 26).
EXAMPLE:
Plaintext: DOG
6 24 1 3 22
13 16 10 14 ( mod 26 ) = 11 = ‘ WLY ’
20 17 15 6 24
ALGORITHM:
STEP-1: Read the plain text and key from the user.
STEP-2: Split the plain text into groups of length three.
STEP-3: Arrange the keyword in a 3*3 matrix.
STEP-4: Multiply the two matrices to obtain the cipher text of length three.
STEP-5: Combine all these groups to get the complete cipher text.
int determinant = 0, t = 0;
;
printf("Enter plain text\n ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("\nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}
OUTPUT:
RESULT: Thus the hill cipher substitution technique had been implemented successfully in C.
PRACTICAL NO.04
IMPLEMENTATION OF VIGENERE CIPHER
EXAMPLE:
Plaintext: Key: GOOD MORNING Key: DAY
To generate a new key, the given key is repeated in a circular manner, as long as the length of the
plain text does not equal to the new key.
G O O D M O R N I N G
D A Y D A Y D A Y D A
Encryption
The first letter of the plaintext is combined with the first letter of the key. The column of plain text
"G" and row of key "D" intersects the alphabet of "J" in the vigenere table, so the first letter of
ciphertext is "J".
Similarly, the second letter of the plaintext is combined with the second letter of the key. The column
of plain text "O" and row of key "A" intersects the alphabet of "O" in the vigenere table, so the second
letter of ciphertext is "O".
Ciphertext = JOMGMMUNGQG
Decryption
Decryption is done by the row of keys in the vigenere table. First, select the row of the key letter, find
the ciphertext letter's position in that row, and then select the column label of the corresponding
ciphertext as the plaintext.
J O M G M M U N G Q G
D A Y D A Y D A Y D A
For example, in the row of the key is "D" and the ciphertext is "J" and this ciphertext letter appears in
the column "G", that means the first plaintext letter is "G".
Next, in the row of the key is "A" and the ciphertext is "O" and this ciphertext letter appears in the
column "O", that means the second plaintext letter is "O".
Plaintext = GOODMORNING
ALGORITHM:
STEP-2: Circulate the alphabets in each row to position left such that the first letter is
attached to last.
STEP-3: Repeat this process for all 26 rows and construct the final key matrix.
STEP-4: The keyword and the plain text is read from the user.
STEP-5: The characters in the keyword are repeated sequentially so as to match with
STEP-6: Pick the first letter of the plain text and that of the keyword as the row indices
STEP-7: The junction character where these two meet forms the cipher character.
STEP-8: Repeat the above steps to generate the entire cipher text.
OUTPUT:
RESULT: Thus the Vigenere Cipher substitution technique had been implemented successfully.
PRACTICAL NO.05
IMPLEMENTATION OF RAIL FENCE CIPHER
EXAMPLE:
Ciphertext: ACHTAKTET M
ALGORITHM:
STEP-1: Read the Plain text.
STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of the plain text.
STEP-4: Arrange the characters of the keyword in sorted order and the corresponding
columns of the plain text.
STEP-5: Read the characters row wise or column wise in the former order to get the
cipher text.
{
//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:
RESULT: Thus the rail fence algorithm had been executed successfully
PRACTICAL NO.06
IMPLEMENTATION OF RSA
AIM: To write a C program to implement the RSA encryption algorithm.
DESCRIPTION:
RSA is an algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric
cryptographic algorithm. Asymmetric means that there are two different keys. This is also called
public key cryptography, because one of them can be given to everyone. A basic principle behind RSA
is the observation that it is practical to find three very large positive integers e, d and n such that with
modular exponentiation for all integer m:
The public key is represented by the integers n and e; and, the private key, by the integer d. m
represents the message. RSA involves a public key and a private key. The public key can be known by
everyone and is used for encrypting messages. The intention is that messages encrypted with the
public key can only be decrypted in a reasonable amount of time using the private key.
EXAMPLE:
ALGORITHM:
STEP-1: Select two co-prime numbers as p and q.
STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.
PROGRAM: (RSA)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("ENTER FIRST PRIME NUMBER: ");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0 || p == 1)
{
printf("WRONG INPUT\n");
exit(1);
}
printf("ENTER ANOTHER PRIME NUMBER: ");
scanf("%ld", &q);
flag = prime(q);
if (flag == 0 || q == 1 || p == q)
{
printf("WRONG INPUT\n");
exit(1);
}
printf("ENTER MESSAGE: ");
scanf(" %[^\n]s", msg);
for (i = 0; i < strlen(msg); i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE:\n");
for (i = 0; i < j - 1; i++)
printf("%ld\t%ld\n", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
if (pr == 1)
return 0;
for (i = 2; i <= sqrt(pr); i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
void ce()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encrypt()
{
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS:\n");
for (i = 0; m[i] != -1; i++)
printf("%c", (char)m[i]);
}
OUTPUT:
RESULT: Thus the C program to implement RSA encryption technique had been implemented
successfully.
PRACTICAL NO.07
IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE
ALGORITHM
AIM: To implement the Diffie-Hellman Key Exchange algorithm using C language.
DESCRIPTION:
The Diffie-Hellman algorithm is being used to establish a shared secret that can be used for secret
communications while exchanging data over a public network using the elliptic curve to generate
points and get the secret key using the parameters.
For the sake of simplicity and practical implementation of the algorithm, we will consider
only 4 variables, one prime P and G (a primitive root of P) and two private values a and b.
P and G are both publicly available numbers. Users (say Alice and Bob) pick private values a
and b and they generate a key and exchange it publicly. The opposite person receives the key
and that generates a secret key, after which they have the same secret key to encrypt.
EXAMPLE:
RAM SHYAM
Public Keys available = P, G Public Keys available = P, G
Private Key Selected = a Private Key Selected = b
Key Generated as = Key Generated as=
ALGORITHM:
STEP-1: Both Ram and Shyam shares the same public keys P and G.
STEP-2: Ram selects a random public key a.
STEP-3: Ramcomputes his secret key A as P a mod p.
STEP-4: Then Ram sends A to Shaym.
STEP-5: Similarly Shaym also selects a public key b and computes his secret key as B and sends the
same back to Ram.
STEP-6: Now both of them compute their common secret key as the other one’s secret key power of
a
mod p.
else
return (((long long int)pow(a, b)) % P);
}
// Driver program
int main()
{
long long int P, G, x, a, y, b, ka, kb;
return 0;
}
OUTPUT:
RESULT: Thus the Diffie-Hellman key exchange algorithm had been successfully implement
using C.
PRACTICAL NO.08
IMPLEMENTATION OF SHA-1
Aim: To implement the SHA-I hashing technique using C program.
DESCRIPTION:
In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function. SHA-1
produces a 160-bit hash value known as a message digest. The way this algorithm
works is that for a message of size < 264 bits it computes a 160-bit condensed output called a message
digest. The SHA-1 algorithm is designed so that it is practically infeasible to find two input messages
that hash to the same output message. A hash function such as SHA-1 is used to calculate an
alphanumeric string that serves as the cryptographic representation of a file or a piece of data. This is
called a digest and can serve as a digital signature. It is supposed to be unique and non-reversible.
EXAMPLE:
ALGORITHM:
STEP-1: Read the 256-bit key values.
STEP-2: Divide into five equal-sized blocks named A, B, C, D and E.
STEP -3: The blocks B, C and D are passed to the function F.
STEP-4: The resultant value is permuted with block E.
STEP-5: The block A is shifted right by ‘s’ times and permuted with the result of step-4.
STEP-6: Then it is permuted with a weight value and then with some other key pair and taken as the
first block.STEP-7: Block A is taken as the second block and the block B is shifted by ‘s’ times and
taken as the third block.
STEP-8: The blocks C and D are taken as the block D and E for the final output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
int main() {
EVP_MD_CTX *mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
char hexString[(EVP_MAX_MD_SIZE * 2) + 1]; // +1 for the null terminator
OpenSSL_add_all_digests();
md = EVP_get_digestbyname("sha1");
if (!md) {
printf("SHA-1 not found, exiting.\n");
exit(1);
}
mdctx = EVP_MD_CTX_create();
input = "abc";
input = "abcdefghijklmnopqrstuvwxyz";
EVP_MD_CTX_destroy(mdctx);
EVP_cleanup();
return 0;
}
Output:-
RESULT: Thus the SHA-1 hashing technique had been implemented successfully.