Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- string wordUP(string &k) {
- string kk;
- for (int i = 0; i < k.size(); ++i) {
- if (k[i] == ' ') {
- kk += ' ';
- continue;
- }
- if (k[i] >= 'A' && k[i] <= 'Z')
- kk += k[i];
- else if (k[i] >= 'a' && k[i] <= 'z')
- kk += k[i] + 'A' - 'a';
- }
- return kk;
- }
- string shifr(string t, string k) {
- string output;
- for (int i = 0, j = 0; i < t.length(); ++i) {
- char chr = t[i];
- if (chr == ' ') {
- output += ' ';
- continue;
- }
- output += (chr + k[j] - 2 * 'A') % 26 + 'A';
- j = (j + 1) % k.length();
- }
- return output;
- }
- string decshift(string t, string k) { // аналогично шифрованию, только обратное
- string output;
- for (int i = 0, j = 0; i < t.length(); ++i) {
- char chr = t[i];
- if (chr == ' ') {
- output += ' ';
- continue;
- }
- output += (chr - k[j] + 26) % 26 + 'A';
- j = (j + 1) % k.length();
- }
- return output;
- }
- int main() {
- string key, str;
- cout << "pls, enter str to encrypt:\n";
- getline(cin, str);
- cout << "pls, enter key:\n";
- getline(cin, key);
- str = wordUP(str);
- string new_key = wordUP(key);
- string encrypted = shifr(str, new_key);
- string decrypt = decshift(encrypted, new_key);
- cout << "Original Message: " << str << endl;
- cout << "Encrypted Message: " << encrypted << endl;
- cout << "Decrypted Message: " << decrypt << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement