C program to encrypt text using one of the simplest ciphers known as the “Caesar cipher.” In this encryption scheme, we shift all characters by a given offset. For example, if we use an offset of 4, every occurrence of ‘A’ will be replaced by ‘E’, every occurrence of ‘B’ will be replaced by ‘F’, and so forth. The encrypted text can be decrypted by using the reverse process if know the offset used for the encryption.
#include <stdio.h> #include <ctype.h> #define MAX 200 int main() { char instr[MAX], crypt[MAX], decrypt[MAX], aa; int shift = 6, k;/* this is the Caesar encryption key */ clrscr(); printf("\n Enter your string\n"); gets(instr); printf( "\noriginal string is [%s]",instr); k=0; while(instr[k] !='\0') { aa = toupper(instr[k]); if(aa != ' ') aa += shift; { if(aa > 'z') aa -=26; { crypt[k] = aa; k++; } } } crypt[k]='\0'; printf("\nEncrypted string is [%s]",crypt); k=0; while(crypt[k] !='\0') { aa = crypt[k]; if(aa != ' ') aa -=shift; { if(aa < 'A' && aa !=' ') aa +=26; { decrypt[k] = aa; k++; } } } decrypt[k]='\0'; printf("\n Decrypted string is [%s]",decrypt); return 0; }