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

Lab Task of Cyber Security (1)

The document contains several C++ scripts for various tasks including string manipulation (reversing a string and checking for palindromes), file input/output (reading from and writing to files), basic network communication (simple TCP client and server), and encryption algorithms (Caesar Cipher and Hill Cipher). Each section provides code snippets with explanations for implementing these functionalities. The document serves as a practical guide for beginners looking to learn C++ programming through hands-on examples.

Uploaded by

usama.ahmad259
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)
4 views

Lab Task of Cyber Security (1)

The document contains several C++ scripts for various tasks including string manipulation (reversing a string and checking for palindromes), file input/output (reading from and writing to files), basic network communication (simple TCP client and server), and encryption algorithms (Caesar Cipher and Hill Cipher). Each section provides code snippets with explanations for implementing these functionalities. The document serves as a practical guide for beginners looking to learn C++ programming through hands-on examples.

Uploaded by

usama.ahmad259
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/ 8

T No 1:

Here are some simple C++ scripts for various tasks:

String Manipulation

1. Reverse a String

#include <iostream>
#include <string>

std::string reverseString(const std::string& str) {


std::string reversedStr = str;
int left = 0;
int right = str.length() - 1;

while (left < right) {


char temp = reversedStr[left];
reversedStr[left] = reversedStr[right];
reversedStr[right] = temp;
left++;
right--;
}

return reversedStr;
}

int main() {
std::string inputStr;
std::cout << "Enter a string: ";
std::cin >> inputStr;

std::string reversedStr = reverseString(inputStr);


std::cout << "Reversed string: " << reversedStr << std::endl;

return 0;
}

2. Check if a String is Palindrome

#include <iostream>
#include <string>
bool isPalindrome(const std::string& str) {
int left = 0;
int right = str.length() - 1;

while (left < right) {


if (str[left] != str[right]) {
return false;
}
left++;
right--;
}

return true;
}

int main() {
std::string inputStr;
std::cout << "Enter a string: ";
std::cin >> inputStr;

bool isPal = isPalindrome(inputStr);


std::cout << "Is palindrome: " << (isPal ? "Yes" : "No") << std::endl;

return 0;
}

File Input/Output

1. Read from a File

#include <iostream>
#include <fstream>
#include <string>

void readFromFile(const std::string& filename) {


std::ifstream file(filename);

if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}

std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}

file.close();
}

int main() {
std::string filename;
std::cout << "Enter filename: ";
std::cin >> filename;

readFromFile(filename);

return 0;
}

2. Write to a File

#include <iostream>
#include <fstream>
#include <string>

void writeToFile(const std::string& filename, const std::string& content) {


std::ofstream file(filename);

if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}

file << content << std::endl;


file.close();
}

int main() {
std::string filename;
std::cout << "Enter filename: ";
std::cin >> filename;

std::string content;
std::cout << "Enter content: ";
std::cin.ignore();
std::getline(std::cin, content);
writeToFile(filename, content);

return 0;
}

Basic Network Communication (using sockets)

1. Simple TCP Client

#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

void tcpClient(const std::string& host, int port, const std::string& message) {


int clientSocket = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in serverAddress;


serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
inet_pton(AF_INET, host.c_str(), &serverAddress.sin_addr);

connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));

send(clientSocket, message.c_str(), message.length(), 0);

char response[1024];
recv(clientSocket, response, 1024, 0);
std::cout << "Response: " << response << std::endl;

close(clientSocket);
}

int main() {
std::string host;
int port;
std::string message;

std::cout << "Enter host: ";


std::cin >> host;
std::cout << "Enter port: ";
std::cin >> port;
std::cout << "Enter message: ";
std::cin.ignore();
std::getline(std::cin, message);

tcpClient(host, port, message);

return 0;
}

2. Simple TCP Server

#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

void tcpServer(const std::string&

T No 2:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

void caesarCipher(string &text, int shift) {


char ch;
for (int i = 0; i < text.length(); ++i) {
ch = text[i];

// Encrypt for uppercase letters


if (isupper(ch)) {
ch = (ch + shift - 'A') % 26 + 'A';
}
// Encrypt for lowercase letters
else if (islower(ch)) {
ch = (ch + shift - 'a') % 26 + 'a';
}

text[i] = ch;
}
}

int main() {
string text;
int shift;

cout << "Enter a string: ";


getline(cin, text);

cout << "Enter shift value: ";


cin >> shift;

caesarCipher(text, shift);

cout << "Encrypted text: " << text << endl;

return 0;
}

T No 3:

Here's an implementation of the Hill Cipher encryption and decryption algorithm in C++:

#include <iostream>
#include <vector>
#include <string>

// Function to multiply two matrices


std::vector<std::vector<int>> multiplyMatrices(const std::vector<std::vector<int>>&
matrix1, const std::vector<std::vector<int>>& matrix2) {
int rows1 = matrix1.size();
int cols1 = matrix1[0].size();
int rows2 = matrix2.size();
int cols2 = matrix2[0].size();

if (cols1 != rows2) {
std::cerr << "Matrix multiplication not possible." << std::endl;
exit(1);
}

std::vector<std::vector<int>> result(rows1, std::vector<int>(cols2, 0));

for (int i = 0; i < rows1; ++i) {


for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
result[i][j] %= 26; // Mod 26 for alphabets
}
}
}

return result;
}

// Function to find the inverse of a matrix


std::vector<std::vector<int>> findInverse(const std::vector<std::vector<int>>& matrix) {
int det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
det = (det % 26 + 26) % 26; // Mod 26

int invDet = 0;
for (int i = 0; i < 26; ++i) {
if ((det * i) % 26 == 1) {
invDet = i;
break;
}
}

if (!invDet) {
std::cerr << "Matrix is not invertible." << std::endl;
exit(1);
}

std::vector<std::vector<int>> inverse(2, std::vector<int>(2));


inverse[0][0] = (matrix[1][1] * invDet) % 26;
inverse[0][1] = (-matrix[0][1] * invDet) % 26;
inverse[1][0] = (-matrix[1][0] * invDet) % 26;
inverse[1][1] = (matrix[0][0] * invDet) % 26;

return inverse;
}

// Function to convert a character to its corresponding alphabetical index


int charToIndex(char c) {
return c - 'a';
}

// Function to convert an alphabetical index to its corresponding character


char indexToChar(int index) {
return index + 'a';
}
// Function to encrypt using Hill Cipher
std::string encrypt(const std::string& plaintext, const std::vector<std::vector<int>>& key) {
std::string ciphertext = "";
int textIndex = 0;

while (textIndex < plaintext.size()) {


std::vector<std::vector<int>> plaintextMatrix(2, std::vector<int>(1));
plaintextMatrix[0][0] = charToIndex(plaintext[textIndex++]);
plaintextMatrix[1][0] = charToIndex(plaintext[textIndex++]);

std::vector<std::vector<int>> ciphertextMatrix = multiplyMatrices(key,


plaintextMatrix);

ciphertext += indexToChar(ciphertextMatrix[0][0]);
ciphertext += indexToChar(ciphertextMatrix[1][0]);
}

return ciphertext;
}

// Function to decrypt using Hill Cipher


std::string decrypt(const std::string& ciphertext, const std::vector<std::vector<int>>& key) {
std::vector<std::vector<int>> inverseKey = findInverse(key);
return encrypt(ciphertext, inverseKey);
}

int main() {
std::string plaintext = "hello";
std::vector<std::vector<int>> key = {{3, 3}, {2, 5}};

std::string ciphertext = encrypt(plaintext, key);


std::cout << "Ciphertext: " << ciphertext << std::endl;

std::string decryptedText = decrypt(ciphertext, key);


std::cout << "Decrypted Text: " << decryptedText << std::endl;

return 0;
}

You might also like