Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Cryptography in C In cryptography, a Caesar cipher, also known as Caesar\'s ciph

ID: 3596885 • Letter: C

Question

Cryptography in C In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. Example: The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher using a left rotation of three places, equivalent to a right shift of 23 (the shift parameter is used as the key): Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher XYZABCDEFGHIJKLMNOPQRSTUVW When encrypting, a person looks up each letter of the message in the "plain" line and writes down the corresponding letter in the "cipher" line. Plaintext THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD Deciphering is done in reverse, with a right shift of 3. Z-25. Then to shift Notice that modulo is helpful, assuming that, A=0, B=1, C-3, characters one needs to do the following: Int letter = ‘A', key = 3, newletter; newLetter = (letter + key) % 26:// since 26 letters in the alphabet Note that this does not work in the opposite direction since subtracting 3 could lead to a negative number.

Explanation / Answer

If you have any problem with the code just let me know in the comments and I'll try to fix it as soon as possible..

#include<stdio.h>

int main() {

char *message,*emessage,*dmessage;

int i,j=0,k,key,temp;

clrscr();

printf(" sentence: ");

fflush(stdin);

gets(message);

printf(" key: ");

scanf("%d",&key);

key=key%26;

for (i=0;message[i]!=NULL;i++)

message[i]=tolower(message[i]);

for (i=0;message[i]!=NULL;i++) {

//printf("%c ",message[i]);

if(message[i]==' ')

emessage[j++]=message[i]; else {

if(message[i]>=48 && message[i]<=57) {

temp=message[i]+key;

if(temp>57)

emessage[j++]=48+(temp-58); else

emessage[j++]=temp;

} else {

if(message[i]>=97 && message[i]<=123) {

temp=message[i]+key;

if(temp>122)

emessage[j++]=97+(temp-123); else

emessage[j++]=temp;

} else

emessage[j++]=message[i];

}

// printf("%c ",emessage[j]);

}

}

emessage[j]='';

printf(" Encrypted message: ");

for (i=0;emessage[i]!=NULL;i++)

printf("%c",emessage[i]);

// printf(" end");

for (i=0,j=0;emessage[i]!=NULL;i++) {

if(emessage[i]==' ')

dmessage[j++]=emessage[i]; else {

if(emessage[i]>=48 && emessage[i]<=57) {

temp=emessage[i]-key;

if(temp<48)

dmessage[j++]=58-(48-temp); else

dmessage[j++]=temp;

} else {

if(emessage[i]>=97 && emessage[i]<=123) {

temp=emessage[i]-key;

if(temp<97)

dmessage[j++]=123-(97-temp); else

dmessage[j++]=temp;

} else

dmessage[j++]=emessage[i];

}

}

}

dmessage[j]='';

printf(" decrypted message: ");

for (i=0;dmessage[i]!=NULL;i++)

printf("%c",dmessage[i]);

getch();

return(0);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote