Showing posts with label IS. Show all posts
Showing posts with label IS. Show all posts

implements hill cipher algorithms

Hill cipher is polygraphics substitute cipher based on linear agenda.Each number represent by numbers as A=0,B=1,,,,,,Z=25.Then make below operations:

Each plain text represented by the 1*N matrix and key is N*N matrix and matrix multipication apply.The decryption,inverse the matrix.

For example ACT is plain text,convert into cipher text.A=0,C=3,T=19.
The matrix is(0 3 19) in column

We need also select one key to encrypt it..The key is 3*3 matrix forms.Then convert it encrypted text.
The program is given belows:

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

void enc(char p[]);
void dec(char c[]);
int k;
void main()
{
                        char p[20];
                        clrscr();
                        printf("enter the plaintext:");
                        scanf("%s",&p);
                        printf("enter the key:");
                        scanf("%d",&k);
                        enc(p);
            getch();
}
void enc(char p[])
{
                        int i;
                        char c[50];
                        for(i=0;i<strlen(p);i++)
                        {
                                    c[i]=p[i]+k;
                                    if((c[i]>90 && c[i]<94)||(c[i]>122 && c[i]<126))
                                    c[i]=p[i]+k-26;
                        }
                                    c[i]=NULL;
                                    printf("encrypted message: %s",c);
                                    dec(c);
}
void dec(char c[])
{
                        int i;
                        char p[100];
                        for(i=0;i<strlen(c);i++)
                        {
                                    p[i]=c[i]-k;
                                    if((c[i]>64 && c[i]<69)||(c[i]>96 && c[i]<100))
                                    p[i]=c[i]-k+26;
                        }
                                    p[i]=NULL;
                                    printf("\n decrypted message:%s",p);
}

Implements caesar cipher algorithms

The caesar are known as the shift cipher which is one of the simplest techniques of encryption.It is part of substitute ciphertext in which any word replace by increase or decrease any number to word.For examples if plain text word is A then we increase it 3 word as get D as encrypted word.

The pattern of 3 word encryption and decryption are given below:

Plain text:ABCDEFGHIJKLMNOPQRSTUVWXYZ
ENCRYPTED TEXT :DEFGHIJKLMNOPQRSTUVWXYZABC

Here we say that A is repaced by D,B is replaced by E.Here we need to repace Z by A,That is get by increase 3 but the value is greater than 26,need to decrease by 26.so,get C.


The algorithm program is:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void enc(char p[]);
void dec(char c[]);
int k;
void main()
{
                        char p[20];
                        clrscr();
                        printf("enter the plaintext:");
                        scanf("%s",&p);
                        printf("enter the key:");
                        scanf("%d",&k);
                        enc(p);
            getch();
}
void enc(char p[])
{
                        int i;
                        char c[50];
                        for(i=0;i<strlen(p);i++)
                        {
                                    c[i]=p[i]+k;
                                    if((c[i]>90 && c[i]<94)||(c[i]>122 && c[i]<126))
                                    c[i]=p[i]+k-26;
                        }
                                    c[i]=NULL;
                                    printf("encrypted message: %s",c);
                                    dec(c);
}
void dec(char c[])
{
                        int i;
                        char p[100];
                        for(i=0;i<strlen(c);i++)
                        {
                                    p[i]=c[i]-k;
                                    if((c[i]>64 && c[i]<69)||(c[i]>96 && c[i]<100))
                                    p[i]=c[i]-k+26;
                        }
                                    p[i]=NULL;
                                    printf("\n decrypted message:%s",p);
}