Created
October 14, 2016 18:16
-
-
Save own2pwn/b8808f29836a00bafa327ea020cd011d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Евгений on 11.10.16. | |
#include <iostream> | |
#include <string> | |
std::string encCaesar(const std::string& message, int shift) { | |
if (shift > 26) | |
shift = shift % 26; | |
auto sLen = message.length(); | |
std::string encrypted = ""; | |
encrypted.reserve(sLen); | |
for (int i = 0; i < sLen; i++) { | |
auto cCode = int(message[i]); | |
if (cCode >= 97 && cCode <= 122) { | |
char encChar = (cCode+shift <=122) ? (char)(cCode+shift) : (char)(cCode - 26 + (shift % 26)); | |
encrypted += encChar; | |
} | |
} | |
return encrypted; | |
} | |
std::string decCaesar(const std::string& message, int shift) { | |
auto sLen = message.length(); | |
std::string decrypted; | |
decrypted.reserve(sLen); | |
if (shift != 0) { | |
for (int i = 0; i < sLen; i++) { | |
auto cCode = int(message[i]); | |
if (cCode >= 97 && cCode <= 122) { | |
char decChar; | |
auto newValue = cCode-shift; | |
if (newValue <= 122 && newValue >=97) | |
decChar = (char)newValue; | |
else if (newValue < 97) { | |
decChar = (char)(newValue + 26); | |
} | |
else { | |
decChar = (char)(cCode - 26 - (shift % 26)); | |
} | |
decrypted += decChar; | |
} | |
} | |
} | |
else { | |
for (int i = 1; i < 27 ;i++) { | |
std::cout << "decrypted message with shift " << i << "\n"; | |
shift = i; | |
decrypted = ""; | |
for (int i = 0; i < sLen; i++) { | |
auto cCode = int(message[i]); | |
if (cCode >= 97 && cCode <= 122) { | |
char decChar; | |
auto newValue = cCode-shift; | |
if (newValue <= 122 && newValue >=97) | |
decChar = (char)newValue; | |
else if (newValue < 97) { | |
decChar = (char)(newValue + 26); | |
} | |
else { | |
decChar = (char)(cCode - 26 - (shift % 26)); | |
} | |
decrypted += decChar; | |
} | |
} | |
std::cout << decrypted <<"\n\n"; | |
} | |
} | |
return decrypted; | |
} | |
int main(int argc, const char * argv[]) { | |
std::string plain = "qwertyuzxc"; | |
auto encrypted = encCaesar(plain, 3); | |
std::cout | |
<< "plain: " << plain | |
<< "\n\n" << "encrypted: (key == 3)\n" << encrypted | |
<< "\n\n" << "decrypted (key == 3):\n" << decCaesar(encrypted, 3) | |
<< "\n\n" << "brootforced:\n" << decCaesar(encrypted, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment