Professional Documents
Culture Documents
Exp - No: 1 Caeser Cipher Date: Aim
Exp - No: 1 Caeser Cipher Date: Aim
Exp - No: 1 Caeser Cipher Date: Aim
ALGORITHM
1.It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some
fixed number of positions down the alphabet. For example, with a left shift of 3, D would be
replaced by A, E would become B, and so on.
2. The method is named after Julius Caesar, who used it in his private correspondence.
3. The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain
alphabet rotated left or right by some number of positions.
4.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.
5. Encryption of a letter x by a shift n can be described
mathematically as, En(x) =(x + n) mod26
6. Decryption is performed similarly, Dn (x)=(x - n)
mod26
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void decrypt(char
arr[])
{
int i;
for(i = 0; i<strlen(arr); i++)
{
arr[i] = arr[i] + 10;
}
}
void encrypt(char arr[])
{
int i;
for(i = 0; i<strlen(arr); i++)
{
arr[i] = arr[i] - 10;
}
}
int main()
{
RESULT:
EXP.NO:7 PLAY FAIR CIPHER
Date:
AIM:
To implement a program to encrypt a plain text and decrypt a cipher text using play fair Cipher
substitution technique.
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
void playfair(char ch1, char ch2, char key[MX][MX])
{ int i, j, w, x, y, z; FILE * out; if ((out =
fopen("cipher.txt", "a+")) == NULL)
{ printf("File Currupted.");
} for (i = 0; i < MX; i+
+)
{ for (j = 0; j < MX; j++)
{ if (ch1 == key[i][j])
{ w = i;
x = j;
} else if (ch2 == key[i][j])
{ y = i; z = j;
}
}
}
//printf("%d%d %d
%d",w,x,y,z); if (w == y) { x =
(x + 1) % 5;
z = (z + 1) % 5;
printf("%c%c", key[w][x], key[y][z]);
fprintf(out, "%c%c", key[w][x], key[y][z]);
} else if (x == z)
{ w = (w + 1) %
5; y = (y + 1) %
5; printf("%c%c", key[w][x], key[y][z]);
fprintf(out, "%c%c", key[w][x], key[y]
[z]);
} else { printf("%c%c", key[w][z], key[y]
[x]); fprintf(out, "%c%c", key[w][z],
key[y][x]);
}
fclose(out);
}
l, m = 0, n;
OUTPUT:
RESULT:
ALGORITHM
1.The Hill cipher is a substitution cipher invented by Lester S. Hill in 1929.
2.Each letter is represented by a number modulo 26. To encrypt a message, each block of n letters is
multiplied by an invertible n × n matrix, again modulus 26.
3.To decrypt the message, each block is multiplied by the inverse of the matrix used for
encryption. The matrix used for encryption is the cipher key, and it should be chosen randomly
from the set of invertible n × n matrices (modulo 26).
4.The ciph er can, be adapted to an alphabet with any number of letters.
6. All arithmetic just needs to be done modulo the number of letters instead of modulo 26.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
printf("\nEnter the string : ");
scanf("%s",txt);
for(i=0;i<25;i++)
{ if(txt[i]>=97 && txt[i]<122) {
}
else
{ end=i;
break;
}
} for(i=0;i<end;i+
+)
{
txt[i]=txt[i]-'a';
} printf("\nEnter %dX%d key matrix row by
row.\n",end,end); for(i=0;i<end;i++)
{ for(j=0;j<end;j++)
{ printf("key[%d][%d] :
",i,j);
scanf("%d",&mtrx[i][j]);
}
}
printf("\n");
for(i=0;i<end;i++)
{ sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
} ans[i]
[0]=sum; }
Develop a program to implement Diffie Hellman Key Exchange Algorithm for encryption and
Decryption.
DESCRIPTION.:
1.Diffie-Hellman key exchange (D-H) is a specific method of securely exchanging cryptographic
keys over a public channel and was one of the first public-key protocols. T
2.he Diffie-Hellman key exchange method allows two parties that have no prior knowledge of each
other to jointly establish a shared secret key over an insecure channel.
3.This key can then be used to encrypt subsequent communications using a symmetric key cipher.
ALGORITHM
PROGRAM:
import java.util.*;
class Diffe
{ public static void main(String
args[])
{
Scanner sc=new Scanner(System.in); System.out.println("Enter
modulo(p)");
int p=sc.nextInt();
System.out.println("Enter primitive root of "+p); int
g=sc.nextInt();
System.out.println("Choose 1st secretno(Alice)"); int
a=sc.nextInt();
System.out.println("Choose 2nd secretno(BOB)");
int b=sc.nextInt(); int A = (int)Math.pow(g,a)%p;
int B = (int)Math.pow(g,b)%p; int S_A
=(int)Math.pow(B,a)%p;
int S_B =(int)Math.pow(A,b)%p if(S_A==S_B)
{ System.out.println("ALice and Bob can communicate with each other!!!");
System.out.println("They share a secret no = "+S_A);
}else
{
System.out.println("ALice and Bob cannot communicate with each other!!!");
}}}
OUTPUT
RESULT:
EXP.NO: MD5
4
Date:
AIM:
Develop a program to implement Message Digest Algorithm.
ALGORITHM DESCRIPTION:
1. The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-
bit (16-byte) hash value, typically expressed in text format as a 32-digit hexadecimal number.
2. MD5 has been utilized in a wide variety of cryptographic application and is also commonly used to
verify data integrity.
PROGARM:
import java.math.BigInteger; import
java.security.MessageDigest; import
java.security.NoSuchAlgorithmException;
public class MD5 {
public static String getMd5(String input)
{ try
{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext =
no.toString(16); while
(hashtext.length() < 32)
{ hashtext = "0" + hashtext;
}
return hashtext;
} catch (NoSuchAlgorithmException e)
{ throw new RuntimeException(e);
} } public static void main(String
args[]) throws
NoSuchAlgorithmException
{
String s = "The quick brown fox jumps overthe lazy dog";
System.out.println("Your HashCode Generated by MD5 is: " +
getMd5(s)); }
}
OUTPUT:
RESULT:
EXP.NO:5 SHA
Date:
AIM:
ALGORITHM DESCRIPTION:
64 bits are appended to the end of the padded message. These bits hold the binary format
of 64 bits indicating the length of the original message.
H0 = 0x67452301
H1 = 0xEFCDAB89 H2 = 0x98BADCFE H3 = 0x10325476 H4 = 0xC3D2E1F0
PROGRAM:
OUTPUT:
RESULT
EXP.NO:6 IMPLEMENTATION OF DIGITAL SIGNATURE STANDARD(DSS)
Date:
AIM:
ALGORITHM:
Sender Side :
1.The hash code.
2.The random number ‘k’ generated for that particular signature.
3.The private key of the sender i.e., PR(a).
4.A global public key(which is a set of parameters for the communicating principles) i.e., PU(g). These
input to the function will provide us with the output signature containing two components – ‘s’ and ‘r’.
Therefore, the original message concatenated with the signature is sent to the receiver.
Receiver Side :
1.The hash code generated by the receiver.
2.Signature components ‘s’ and ‘r’.
3.Public key of the sender.
4.Global public key.
PROGRAM:
import java.security.KeyPair;
importjava.security.KeyPairGenerato
r; import java.security.PrivateKey;
importjava.security.Signature;
import java.util.Scanner;
public class DSS {
public static void main(String args[]) throws
Exception{ Scanner sc = new Scanner(System.in);
RESULT:
EXP NO :8 POLYALPHABETIC CIPHER
DATE:
AIM: To implement a program for encrypting a plain text and decrypting a cipher text using
Poly alphabetic technique.
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <ctype.h>
#include <string.h>
void encipher();
void decipher();
void main()
{
int choice;
while(1)
{
printf("\n1. Encrypt Text");
printf("\t2. Decrypt Text");
printf("\t3. Exit");
printf("\n\nEnter Your Choice : ");
scanf("%d",&choice);
if(choice == 3)
exit(0);
else if(choice == 1)
encipher();
else if(choice == 2)
decipher();
else
printf("Please Enter Valid Option.");
}
}
void encipher()
{
unsigned int i,j;
char input[50],key[10];
printf("\n\nEnter Plain Text: ");
scanf("%s",input);
printf("\nEnter Key Value: ");
scanf("%s",key);
printf("\nResultant Cipher Text: ");
for(i=0,j=0;i<strlen(input);i++,j++)
{
if(j>=strlen(key))
{ j=0;
}
printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
}}
void decipher()
{
unsigned int i,j;
char input[50],key[10];
int value;
printf("\n\nEnter Cipher Text: ");
scanf("%s",input);
printf("\n\nEnter the key value: ");
scanf("%s",key);
for(i=0,j=0;i<strlen(input);i++,j++)
{
if(j>=strlen(key))
{ j=0; }
value = (toupper(input[i])-64)-(toupper(key[j])-64);
if( value < 0)
{ value = value * -1;
}
printf("%c",65 + (value % 26));
}}
OUTPUT:
RESULT: