Untitled
unknown
plain_text
2 years ago
1.8 kB
9
Indexable
#include<bits/stdc++.h>
using namespace std;
string generateKey(string s, string key){
int x = s.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == s.size())
break;
key.push_back(key[i]);
}
return key;
}
string encryption(string s, string key) {
string encrypted;
int keyIndex = 0;
for (int i = 0; i < s.size(); i++) {
char alpha;
if (isalpha(s[i])) {
if (islower(s[i]))
alpha = 'a';
else
alpha = 'A';
char shifted = (s[i] - alpha + key[keyIndex] - alpha) % 26 + alpha;
encrypted += shifted;
keyIndex = (keyIndex + 1) % key.size(); // Move to the next character in the key
} else {
encrypted += s[i];
}
}
return encrypted;
}
string decryption(string s,string key) {
string decrypted;
int keyIndex = 0;
for (int i = 0; i < s.size(); i++) {
char alpha;
if (isalpha(s[i])) {
if (islower(s[i]))
alpha = 'a';
else
alpha = 'A';
char shifted = (s[i] - alpha - (key[keyIndex] - alpha) + 26) % 26 + alpha;
decrypted += shifted;
keyIndex = (keyIndex + 1) % key.size(); // Move to the next character in the key
} else {
decrypted += s[i];
}
}
return decrypted;
}
int main() {
string key = generateKey("hello world", "Lemon");
string encrypted = encryption("Hello world", key);
cout <<"Encrypted: "<< encrypted << "\n";
string decrypted = decryption(encrypted, key);
cout<<"Decrypted: "<<decrypted;
}Editor is loading...
Leave a Comment