0% found this document useful (0 votes)
52 views

CSS Exp-2

The document contains details of an experiment on cryptography and system security. It includes the name, roll number, and class of the student Apurva Ankushrao conducting experiment 02. The experiment involves a C++ code that encrypts and decrypts a message by shifting each character by a user-input key. It takes a message and key as input, encrypts the message, then takes another message and same key as input and decrypts the message.

Uploaded by

Apurva Ankushrao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

CSS Exp-2

The document contains details of an experiment on cryptography and system security. It includes the name, roll number, and class of the student Apurva Ankushrao conducting experiment 02. The experiment involves a C++ code that encrypts and decrypts a message by shifting each character by a user-input key. It takes a message and key as input, encrypts the message, then takes another message and same key as input and decrypts the message.

Uploaded by

Apurva Ankushrao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: -Apurva Ankushrao

Roll no: -08

Class: -TE CMPN-1

Batch: -C1

Subject: -Cryptography And System Security

Experiment No. 02
Code:-
C++
#include<stdio.h>
#include<ctype.h>
int main()
{
char text[500], ch;
int key;
// taking user input
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
// visiting character by character
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// check for valid character
if (isalnum(ch)) {
// lower case characters
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// uppercase characters
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
// numbers
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// invalid character
else {
printf("Invalid Message");
}
// adding encoded answer
text[i] = ch;
}
printf("Encrypted message: %s", text);
printf("\n");
printf("Enter a message to decrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
//visiting each character
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// check for valid characters
if (isalnum(ch)) {
// lower case characters
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// uppercase characters
if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// numbers
if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
}
// invalid characters
else {
printf("Invalid Message");
}
// asding decoded character back
text[i] = ch;
}
printf("Decrypted message: %s", text);
return 0;
}

Output:-

You might also like