Sunteți pe pagina 1din 6

Caesar Cipher

Introduction
The caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named after Julius Caesar, who apparently used it to communicate with his generals. More complex encryption schemes such as the Vigenre cipher employ the Caesar cipher as one element of the encryption process. The widely known ROT13 'encryption' is simply a caesar cipher with an offset of 13. The caesar cipher offers essentially no communication security, and it will be shown that it can be easily broken even by hand.

Example
To pass an encrypted message from one person to another, it is first necessary that both parties have the 'key' for the cipher, so that the sender may encrypt it and the receiver may decrypt it. For the caesar cipher, the key is the number of characters to shift the cipher alphabet. Here is a quick example of the encryption and decryption steps involved with the caesar cipher. The text we will encrypt is 'defend the east wall of the castle', with a shift (key) of 1. plaintext: defend the east wall of the castle

ciphertext: efgfoe uif fbtu xbmm pg uif dbtumf

It is easy to see how each character in the plaintext is shifted up the alphabet. Decryption is just as easy, by using an offset of -1. plain: abcdefghijklmnopqrstuvwxyz

cipher: bcdefghijklmnopqrstuvwxyza

Obviously, if a different key is used, the cipher alphabet will be shifted a different amount.

Mathematical Description
First we translate all of our characters to numbers, 'a'=0, 'b'=1, 'c'=2, ... , 'z'=25. We can now represent the caesar cipher encryption function, e(x), where x is the character we are encrypting, as:

Where k is the key (the shift) applied to each letter. After applying this function the result is a number which must then be translated back into a letter. The decryption function is :

1. To Encrypt a message, first of all we have to find out the ASCII value of the character to be encrypted, then subtract the ASCII value of first letter (a for lowercase value and A for uppercase value). Then add the Key value to the result and then take modulus of the result with 26. And finally add the ASCII value of first letter (a for lowercase value and A for uppercase value).

for(temp = 0; temp < strlen(input); temp++) { if(input[temp] >= 'A' && input[temp] <= 'Z' ) output[temp] = (char)(((((int)input[temp] - (int)'A') + key) % 26) + (int)'A'); else output[temp] = (char)(((((int)input[temp] - (int)'a') + key) % 26) + (int)'a'); } To decrypt the message, first of all we have to find out the ASCII value of the character to be decrypted,
then subtract the ASCII value of first letter (a for lowercase value and A for uppercase value). Then add the Key value and 26 to the result and then take modulus of the result with 26. And finally, add the ASCII value of first letter (a for lowercase value and A for uppercase value).

for(temp = 0; temp < strlen(input); temp++) { if(input[temp] >= 'A' && input[temp] <= 'Z' ) output[temp] = (char)(((((int)input[temp] - (int)'A') - key + 26) % 26) + (int)'A'); else output[temp] = (char)(((((int)input[temp] - (int)'a') - key + 26) % 26) + (int)'a'); }

#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int shift; char message[80]; printf("Enter message to be encrypted: "); scanf("%s", &message); printf("Enter shift amount (1-25): "); scanf("%d", &shift);

/*

for(int i=0;i<strlen(message);i++) { if (message[i]=!' ') if('A'>= message[i] <= 'Z') { message[i] = ((message[i]-'A') + shift) % 26 + 'A'; } else if('a'>= message[i] <='z') { message[i] =((message[i]-'a') + shift) % 26 + 'a'; } }

*/

for(int temp = 0; temp < strlen(message); temp++) { if(message[temp] >= 'A' && message[temp] <= 'Z' ) { message[temp] = (char)(((((int)message[temp] - (int)'A') + shift) % 26) + (int)'A');

} else { message[temp] = (char)(((((int)message[temp] - (int)'a') + shift) % 26) + (int)'a');

} }

printf("Encrypted message: %s", message);

return 0; }

Another way of doing the same program


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

int main(){ int shift; char message[81]; printf("Enter message to be encrypted: "); scanf("%s", &message); printf("Enter shift amount (1-25): "); fflush(stdin); scanf("%d", &shift); for(int i=0;i<strlen(message);i++) { // checking for upper case if((message[i]>='A')&&(message[i]<='Z')) message[i]=((message[i]-'A') + shift) % 26 + 'A'; else //checking for lower case if((message[i]>='a')&&(message[i]<='z')) message[i]=((message[i]-'a') + shift) % 26 + 'a'; } printf("\n\n%s",message);

getch(); return 0; }

Another way of doing the same program

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

int main(){ int shift; char message[81]; printf("Enter message to be encrypted: "); scanf("%s", &message); printf("Enter shift amount (1-25): "); fflush(stdin); scanf("%d", &shift); for(int i=0;i<strlen(message);i++) { // checking for upper case if((message[i]>='A')&&(message[i]<='Z')) message[i]=((message[i]) + shift) ; else //checking for lower case if((message[i]>='a')&&(message[i]<='z')) message[i]=((message[i]) + shift) ; } printf("\n\n%s",message);

getch(); return 0; }

S-ar putea să vă placă și