Saturday, February 28, 2015

CRYPTOGRAPHY LAB 2(MONOALPHABETIC CIPHER)


CODE

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
    int a[26];
    int i,j,value,flag=0,length;
    char akey[26],plaintext[999],ciphertext[999];
    char alp[26]={'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'};
    for(i=0;i<26;i++)
    {
        value=rand()%26;
        for(j=0;j<i;j++)
        {
            if(a[j]==value)
            {
                flag=1;
            }
        }
        if(flag==0)
            a[i]=value;
        else
        {
            i--;
            flag=0;
        }
    }
    for(i=0;i<26;i++)
    {
        akey[i]=a[i]+65;
        printf("%c \t",akey[i]);
    }
    printf("\n\nenter plain text in capital letter \n");//input plain text
    gets(plaintext);
    length=strlen(plaintext);

    for(i=0;i<length;i++)
    {
        for(j=0;j<26;j++)
        {
           if(plaintext[i]!=32) //FRO SPACE DECTIION
           {
               if(plaintext[i]==alp[j])
               ciphertext[i]=akey[j];
           }
           else
           ciphertext[i]=plaintext[i];

        }

    }
    ciphertext[length]='\0';
    puts(ciphertext);        //display cipher text


}


Cryptography Lab 1 (caesar cipher encryption)




#include<stdio.h>
#include<conio.h>
#include<string.h>

int caesar_encryption (int, char);                     //encryption function


int main()
{
char plaintext[999],ciphertext[900];
int key,textlength,i;

printf("enter plain text:\n");
gets(plaintext);

printf("enter value of key\n");                         //input the key
scanf("%d",&key);

textlength=strlen(plaintext);
for (i=0;i<textlength;i++)
{
if(plaintext[i]!=32)
{
ciphertext[i]=caesar_encryption(key, plaintext[i]);
   }
   else{
   ciphertext[i]=plaintext[i];
   }
}
 printf("cipher text: \n");
puts(ciphertext);
return 0;
}

int caesar_encryption (int key, char a)
{

  char b;
b=(((a-97)+key)%26)+97;                                   //core incryprion function
 return b;

}