Crypto File
Crypto File
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 alphabet as a circle and
"wrap around". W becomes Z, X becomes A, Y becomes B, and Z becomes C. To change a message
back, each letter is replaced by the one three before it.
EXAMPLE:
ALGORITHM:
STEP-3: If the key is positive then encrypt the text by adding the key with each
#include <stdio.h>
#include <string.h>
#include<conio.h>
#include <ctype.h>
void main()
int key,i,length;
int result;
clrscr();
scanf("%s", plain);
scanf("%d", &key);
cipher[i]=plain[i] + key;
printf("%c", cipher[i]);
for(i=0;i<length;i++)
{
plain[i]=cipher[i]-key;
if(isupper(cipher[i])&&(plain[i]<'A')) plain[i]=plain[i]+26;
if(islower(cipher[i])&&(plain[i]<'a')) plain[i]=plain[i]+26;
printf("%c",plain[i]);
getch();
OUTPUT:
RESULT:
DESCRIPTION: To encrypt, a table of alphabets can be used, termed a or Vigenère table. It consists
of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left
compared to the prev 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. Each row starts with a key letter. The remainder of the row holds the letters A to
Z. Although there are 26 key rows shown, you will only use as many keys as there are unique letters
in the key string, here just 5 keys, {L, E, M, O, N}. For successive letters of the message, we are going
to take successive letters of the key string, and encipher each message letter using its corresponding
key row. Choose the next letter of the key, go along that row to find the column heading that
matches the message character; the letter at the[key-row, msg-col] is the enciphered letter.
EXAMPLE:
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-7: The junction character where these two meet forms the cipher character.
STEP-8: Repeat the above steps to generate the entire cipher text.
#include <stdio.h>
#include<conio.h>
#include <ctype.h>
#include <string.h>
void encipher();
void decipher();
void main()
int choice;
clrscr(); while(1)
printf("\t3. Exit");
scanf("%d",&choice);
if(choice == 3) exit(0);
}}
void encipher()
char input[50],
key[10];
scanf("%s",input);
scanf("%s",key);
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()
scanf("%s",input);
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);
OUTPUT:
RESULT:
Thus the Vigenere Cipher substitution technique had been implemented successfully.
INDEX
List of Experiments
A. Caesar Cipher
B. Vigenere Cipher
C. Rail-Fence Cipher
D. Multiplicative Cipher
A. RSA algorithm
B. DES algorithm
other tool)
IMPLEMENTATION OF RAIL FENCE – ROW & COLUMN
TRANSFORMATION TECHNIQUE
AIM:
DESCRIPTION:
In the rail fence cipher, the plain text 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.
EXAMPLE:
ALGORITHM:
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
STEP-5: Read the characters row wise or column wise in the former order to get the
cipher text.
PROGRAM: (Rail Fence)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int i,j,k,l;
char a[20],c[20],d[20];
clrscr();
gets(a); l=strlen(a);
/*Ciphering*/ for(i=0,j=0;i<l;i++)
if(i%2==0)
c[j++]=a[i];
for(i=0;i<l;i++)
{ \
if(i%2==1)
c[j++]=a[i];
printf("\n%s",c);
for(i=0,j=0;i<k;i++)
{ d[j]=c[i]; j=j+2; }
for(i=k,j=1;i<l;i++)
OUTPUT:
RESULT:
AIM:
DESCRIPTION:
DES is a symmetric encryption system that uses 64-bit blocks, 8 bits of which are used for parity
checks. The key therefore has a "useful" length of 56 bits, which means that only 56 bits are actually
used in the algorithm. The algorithm involves carrying out combinations, substitutions and
permutations between the text to be encrypted and the key, while making sure the operations can
be performed in both directions. The key is ciphered on 64 bits and made of 16 blocks of 4 bits,
generally denoted k1 to k16. Given that "only" 56 bits are actually used for encrypting, there can be
256 different keys.
Breakdown of the blocks into two parts: left and right, named L and R Permutation and substitution
steps repeated 16 times Re-joining of the left and right parts then inverse initial permutation
EXAMPLE:
ALGORITHM:
STEP-2: Split it into two 32-bit blocks and store it in two different arrays.
STEP-3: Perform XOR operation between these two arrays.
STEP-4: The output obtained is stored as the second 32-bit sequence and the original
STEP-5: Thus the encrypted 64-bit cipher text is obtained in this way. Repeat the same
PROGRAM:
DES.java
import javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;
class DES
String skeyString;
String inputMessage,encryptedData,decryptedMessage;
public DES()
try
{ generateSymmetricKey();
System.out.println(e);
void generateSymmetricKey()
skey=getRawKey(knumb);
sr.setSeed(seed);
kgen.init(56, sr);
raw = skey.getEncoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws
Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
return encrypted;
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec
skeySpec = new SecretKeySpec(raw, "DES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
return decrypted;
}}
OUTPUT:
RESULT:
Thus the data encryption standard algorithm had been implemented successfully using C language
IMPLEMENTATION OF RSA
AIM:
DESCRIPTION:
(me)d = m (mod n)
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:
PROGRAM: (RSA)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
char msg[100];
void ce();
void encrypt();
void decrypt();
void main()
{ clrscr();
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{ printf("\nWRONG INPUT\n");
getch();
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
printf("\nWRONG INPUT\n");
getch();
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i]; n=p*q;
t=(p-1)*(q-1); ce();
for(i=0;i<j-1;i++) printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
int i; j=sqrt(pr);
for(i=2;i<=j;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;
}}}
while(1)
k=k+t;
if(k%x==0)
return(k/x);
}}
void encrypt()
{ pt=m[i];
pt=pt-96; k=1;
for(j=0;j<key;j++)
{ k=k*pt; k=k%n; }
temp[i]=k;
ct=k+96;
en[i]=ct; i++;
en[i]=-1;
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
void decrypt()
i=0; while(en[i]!=-1)
{ ct=temp[i];
k=1; for(j=0;j<key;j++)
{ k=k*ct; k=k%n;
pt=k+96;
for(i=0;m[i]!=-1;i++) printf("%c",m[i]);
}
OUTPUT:
RESULT: Thus the C program to implement RSA encryption technique had been implemented
successfully
IMPLEMENTATION OF DIGITAL SIGNATURE STANDARD
AIM:
ALGORITHM:
STEP-2: X had document signed by him but he says he did not sign that document
digitally.
STEP-4: He chooses a random co-primes alpha and beta and the x’s original signature
x.
STEP-5: With these values, he applies it to the elliptic curve cryptographic equation to
obtain y.
STEP-6: Comparing this ‘y’ with actual y’s document, Alice concludes that y is a
forgery.
PROGRAM: (Digital Signature Standard)
import java.util.*;
import java.math.BigInteger;
while (!n.isProbablePrime(99))
{ while (!((n.mod(start)).equals(zero)))
{ start = start.add(one);
n = n.divide(start);
return n;
h = h.mod(p);
public static void main (String[] args) throws java.lang.Exception { Random randObj = new
Random();
BigInteger p = getNextPrime("10600");
BigInteger g = getGen(p,q,randObj);
x = x.mod(q);
BigInteger y = g.modPow(x,p);
k = k.mod(q);
BigInteger r = (g.modPow(k,p)).mod(q);
BigInteger s = kInv.multiply(hashVal.add(x.multiply(r)));
BigInteger w = s.modInverse(q);
BigInteger u1 = (hashVal.multiply(w)).mod(q);
BigInteger u2 = (r.multiply(w)).mod(q);
BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
v = (v.mod(p)).mod(q);
} } }
OUTPUT:
RESULT: Thus the simple Code Optimization techniques had been implemented successfully.