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

Ins Practical-1 Ins

INS

Uploaded by

varmavikash990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ins Practical-1 Ins

INS

Uploaded by

varmavikash990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Faculty of Engineering & Technology

B.Tech CSE – 4rd Year 7th Sem [7A9]


203105311-
Information and Network Security Laboratory

PRACTICAL – 1

Aim: Implement Caesar cipher encryption-decryption.

The Caesar cipher 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 shifted a certain number of places down or up the alphabet.

Caesar Cipher Encryption


For encryption, each letter in the plaintext is replaced by a letter some fixed
number of positions down the alphabet.

Formula for encryption:

E(x) = (x + n) mod 26

where:

• E(x) is the encrypted letter,


• xxx is the position of the plaintext letter in the alphabet (0-indexed, i.e.,
A=0, B=1, ..., Z=25),
• n is the shift amount,
• mod 26 ensures that the result wraps around if it exceeds 25.

Caesar Cipher Decryption


For decryption, each letter in the ciphertext is replaced by a letter some fixed
number of positions up the alphabet.

Formula for decryption:

D(x) = (x − n + 26) mod 26

where:

Page no. 1 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
• D(x) is the decrypted letter,
• x is the position of the ciphertext letter in the alphabet (0-indexed),
• n is the shift amount,
• +26ensures that the result is positive before applying the modulo
operation.

CODE:
#include <iostream>
#include <string>

using namespace std;


string caesarEncrypt(const string& plaintext, int shift) {
string encryptedText = "";
for (char c : plaintext) {
if (isalpha(c)) {
char offset = islower(c) ? 'a' : 'A';
char encryptedChar = (c - offset + shift) % 26 + offset;
encryptedText += encryptedChar;
} else {
encryptedText += c; // Non-alphabetic characters are not changed
}
}
return encryptedText;
}

string caesarDecrypt(const string& ciphertext, int shift) {


string decryptedText = "";
for (char c : ciphertext) {

Page no. 2 enrollment no.-2203051057106


Faculty of Engineering & Technology
B.Tech CSE – 4rd Year 7th Sem [7A9]
203105311-
Information and Network Security Laboratory
if (isalpha(c)) {
char offset = islower(c) ? 'a' : 'A';
char decryptedChar = (c - offset - shift + 26) % 26 + offset;
decryptedText += decryptedChar;
} else {
decryptedText += c; // Non-alphabetic characters are not changed
}
}
return decryptedText;
}
int main() {
string plaintext = "Hello, World!";
int shift = 3;
string encrypted = caesarEncrypt(plaintext, shift);
string decrypted = caesarDecrypt(encrypted, shift);

cout << "Plaintext: " << plaintext << endl;


cout << "Encrypted: " << encrypted << endl;
cout << "Decrypted: " << decrypted << endl;
return 0;
}

Page no. 3 enrollment no.-2203051057106

You might also like