Lab Task of Cyber Security (1)
Lab Task of Cyber Security (1)
String Manipulation
1. Reverse a String
#include <iostream>
#include <string>
return reversedStr;
}
int main() {
std::string inputStr;
std::cout << "Enter a string: ";
std::cin >> inputStr;
return 0;
}
#include <iostream>
#include <string>
bool isPalindrome(const std::string& str) {
int left = 0;
int right = str.length() - 1;
return true;
}
int main() {
std::string inputStr;
std::cout << "Enter a string: ";
std::cin >> inputStr;
return 0;
}
File Input/Output
#include <iostream>
#include <fstream>
#include <string>
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>
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}
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;
}
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
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;
return 0;
}
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
T No 2:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
text[i] = ch;
}
}
int main() {
string text;
int shift;
caesarCipher(text, shift);
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>
if (cols1 != rows2) {
std::cerr << "Matrix multiplication not possible." << std::endl;
exit(1);
}
return result;
}
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);
}
return inverse;
}
ciphertext += indexToChar(ciphertextMatrix[0][0]);
ciphertext += indexToChar(ciphertextMatrix[1][0]);
}
return ciphertext;
}
int main() {
std::string plaintext = "hello";
std::vector<std::vector<int>> key = {{3, 3}, {2, 5}};
return 0;
}