47 std::string
encrypt (
const std::string &text,
const int &key) {
48 std::string encrypted_text =
"";
50 char encrypted_char = char(c ^ key);
51 encrypted_text += encrypted_char;
53 return encrypted_text;
61 std::string
decrypt (
const std::string &text,
const int &key) {
62 std::string decrypted_text =
"";
63 for (
auto &c : text) {
64 char decrypted_char = char(c ^ key);
65 decrypted_text += decrypted_char;
67 return decrypted_text;
77 std::string text1 =
"Whipalsh! : Do watch this movie...";
80 assert(text1 == decrypted1);
81 std::cout <<
"Original text : " << text1;
82 std::cout <<
" , Encrypted text (with key = 17) : " << encrypted1;
83 std::cout <<
" , Decrypted text : "<< decrypted1 << std::endl;
85 std::string text2 =
"->Valar M0rghulis<-";
88 assert(text2 == decrypted2);
89 std::cout <<
"Original text : " << text2;
90 std::cout <<
" , Encrypted text (with key = 29) : " << encrypted2;
91 std::cout <<
" , Decrypted text : "<< decrypted2 << std::endl;
Functions for XOR cipher algorithm.
Algorithms for encryption and decryption.
std::string decrypt(const std::string &text, const int &key)
std::string encrypt(const std::string &text, const int &key)