Cns Prac1
Cns Prac1
Practical-1
AIM: implement Caesar cipher with Encryption/Decryption.
Encryption :
#include <stdio.h>
#include <string.h>
void main() {
int i, key;
char message[100], ch;
printf("Enter the message to encrypt: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0';
printf("Enter the key: ");
scanf("%d", &key);
key = key % 26;
for(i = 0; message[i] != '\0'; ++i)
{ ch = message[i];
if(ch >= 'a' && ch <= 'z')
{ ch = ch + key;
if(ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z') {
ch = ch + key;
if(ch > 'Z') {
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}}
printf("Encrypted message: %s\n", message);}
SPCE(IT) 1
CNS(3161606) Enroll No:221240116068
Decrytion :
#include <stdio.h>
#include <string.h>
void main() {
int i, key;
char message[100], ch;
printf("Enter the message: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0';
printf("Enter the key: ");
scanf("%d", &key);
key = key % 26;
for(i = 0; message[i] != '\0'; ++i)
{ ch = message[i];
if(ch >= 'a' && ch <= 'z')
{ ch = ch + key;
if(ch > 'z') {
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z') {
ch = ch + key;
if(ch > 'Z') {
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s\n", message);
}
SPCE(IT) 2
CNS(3161606) Enroll No:221240116068
Output:
SPCE(IT) 3