The Most Basic C Programming Only Please ! thankss One of the oldest known encry
ID: 664820 • Letter: T
Question
The Most Basic C Programming Only Please ! thankss
One of the oldest known encryption techniques is the Caesar cipher, attributed to Julius Caesar. It involves replacing each letter in a message with another letter that is a fixed number of positions later in the alphabet. (If the replacement would go past the letter Z, the cipher "wraps around" to the beginning of the alphabet. For example, if each letter is replaced by the letter two positions after it, then Y would be replaced by A, and Z would be replaced by B.) Write a program that encrypts a message using a Caesar cipher. The user will enter the message to be encrypted and the shift amount (the number of positions by which letters should be shifted):
Enter message to be encrypted: Go ahead, make my day.
Enter shift amount (1-25): 3
Encrypted message: Jr dkhdg, pdnh pb gdb.
Notice that the program can decrypt a message if the user enters 26 minus the original key:
Enter message to be encrypted: Jr dkhdg, pdnh pb gdb.
Enter shift amount (1-25): 23
Encrypted message: Go ahead, make my day.
// if at all possible maybe give description of whats hapening at some points so i can learn and see how it works better..
// if possible a sample output would be nice.. I'm going to beusing codeblocks as the compiler program..
// if possile only use defualt librarys and using some basic methods.. maybe an if state or if else, while..do.. ect..The basic, lol...
Explanation / Answer
Answer:
#include<stdio.h>
#include <string.h>
#include<conio.h>
void text_encrypt1(char cipher1[25], int shift1, int num1)
{
for (int a=0; a < num1; a++)
{
if (cipher1[a] >= 'A' && cipher1[a] <= 'Z')
{
cipher1[a] = (char)(((cipher1[a] + shift1 - 'A' + 26) % 26) + 'A');
}
else if (cipher1[a] >= 'a' && cipher1[a] <= 'z')
{
cipher1[a] = (char)(((cipher1[a] + shift1 - 'a' + 26) % 26) + 'a');
}
}
}
void text_decrypt1(char cipher1[25], int shift1, int num1)
{
for (int a=0; a < num1; a++)
{
if (cipher1[a] >= 'A' && cipher1[a] <= 'Z')
{
cipher1[a] = (char)(((cipher1[a] - shift1 - 'A' + 26) % 26) + 'A');
}
else if (cipher1[a] >= 'a' && cipher1[a] <= 'z')
{
cipher1[a] = (char)(((cipher1[a] - shift1 - 'a' + 26) % 26) + 'a');
}
}
}
void main ()
{
char text1[10];
static const char encrypt[] = "encrypt";
static const char decrypt[] = "decrypt";
int shift1;
char cipher1[25];
int result1;
int result2;
int num1;
clrscr();
printf( "Enter operation: encrypt or decrypt");
scanf("%s",text1);
printf( "Enter text to encrypt/decrypt");
scanf("%s", cipher1);
printf("Enter shift1 key")
scanf("%d", shift1);
num1 = strlen (cipher1);
result1 = strcmp (text1, encrypt);
result2 = strcmp (text1, decrypt);
if(result1 == 0)
{
text_decrypt1(cipher1, shift1, num1);
}
if(result2 == 0)
{
text_encrypt1(cipher1, shift1, num1);
}
printf("Result:%s");
printf("cipher%s",cipher);
getch();
}