We’ll use the substitution cipher technique in this shot. In this technique, the text characters are replaced by other numbers, characters, or symbols. The substitution cipher technique is the earliest known cipher technique invented by Julius Caesar.
In this cipher technique, we replace each letter of the alphabet with a letter that is standing three places further:
x
times to the right.shift (x)
is the encryption key.left shift by x alphabets
.Note:
- Encryption:
(PT+Key)mod26
- Decryption:
(CT-Key)mod26
Let’s try to solve Caesar cipher with an example, assuming The given PT=CRYPTOLOGY and Key=3:
F
U
B
S
W
R
O
R
J
B
The ciphertext for
CRYPTOLOGY
isFUBSWRORJB
.
#include<stdio.h>#include<string.h>int main() {char pt[50],ct[50];int key,i;//printf("Enter the palintext:");scanf("%s",pt);// printf("Enter the key:");scanf("%d",&key);for(i=0;i<strlen(pt);i++){if(pt[i]>=65 && pt[i]<=90){ct[i]=(((pt[i]-65)+key)%26)+65;}else if(pt[i]>=97 && pt[i]<=122){ct[i]=(((pt[i]-97)+key)%26)+97;}elsect[i]=pt[i];}ct[i]='\0';printf("Cipher Text is:");puts(ct);}
Enter the input below
pt[50]
,ct[50];
key
, and i;
.Plain Text
and Key
.(P+K)mod 26
.\0
So, that we don’t encounter any garbage value.puts()
in the end.