#include <iostream>
#include <string>
std::string encrypt(std::string msg, std::string key)
{
// Make sure the key is at least as long as the message
std::string tmp(key);
while (key.size() < msg.size())
key += tmp;
// And now for the encryption part
for (std::string::size_type i = 0; i < msg.size(); ++i)
msg[i] ^= key[i];
return msg;
}
std::string decrypt(std::string msg, std::string key)
{
return encrypt(msg, key); // lol
}
int main()
{
std::string message = encrypt("Hello world!", "monkey");
std::cout << "Encrypted: " << message;
std::cout << "\nDecrypted: " << decrypt(message, "monkey");
}
// key is now const&
std::string encrypt(std::string msg, std::string const& key)
{
// Side effects if the following is not written:
// In my case, division by 0.
// In the other case, stuck in an infinite loop.
if(!key.size())
return msg;
for (std::string::size_type i = 0; i < msg.size(); ++i)
msg[i] ^= key[i%key.size()];
return msg;
}
// Rewritten to use const& on both parameters
std::string decrypt(std::string const& msg, std::string const& key)
{
return encrypt(msg, key); // lol
}
// No external changes needed.