SHIFT CIPHER in C (ADDITIVE CIPHER )
Shift Cipher is a classical cipher in which plaintext is shifted to a position with respect to the key value. This is also called Additive Cipher.
Here we don't go that much about theory part, for that you can refer to any standard book or NPTEL Video lecture.
#include<stdio.h>
#include<string.h>
#define max 30
char CT[max];
int key;
void enterKey(void);
void senderText(void);
void receiverText(void);
void enterKey()
{
printf("Enter the key: ");
scanf("%d",&key);
//printf("key: %s",key);
}
void senderText()
{
printf("\n\n\t*****SENDER-ENCODING*****\n");
char text[max];
int t,i;
int aci_ct[max];
printf("\nEnter the text: ");
scanf("%s",text);
//printf("Entered text is: %s",text);
t=strlen(text);
//printf("\nText length: %d",t);
enterKey();
for(i=0;i<t;i++)
{
aci_ct[i]=(((text[i]-97)+key)%26)+97;
}
for(i=0;i<t;i++)
{
CT[i]=aci_ct[i];
}
printf("\n\tCipher Text: %s",CT);
}
void receiverText()
{
printf("\n\n\t*****RECEIVER-DECODING*****\n");
int t,i;
t=strlen(CT);
//printf("\nText length: %d",t);
printf("\n\tPlain Text: ");
for(i=0;i<t;i++)
{
printf("%c",(((CT[i]-97)-key+26)%26)+97);
}
}
main()
{
printf("\n\n\t\t\t.........SHIFT-CIPHER.........\n");
senderText();
receiverText();
printf("\n\n");
}
Output:
.........SHIFT-CIPHER.........
*****SENDER-ENCODING*****
Enter the text: welcometounicoderz
Enter the key: 23
Cipher Text: tbizljbqlrkfzlabow
*****RECEIVER-DECODING*****
Plain Text: welcometounicoderz
0 Comments