fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. std::string string_to_hex(const std::string& input)
  6. {
  7. static const char* const lut = "0123456789ABCDEF";
  8. size_t len = input.size();
  9.  
  10. std::string output;
  11. output.reserve(2 * len);
  12. for (size_t i = 0; i < len; ++i)
  13. {
  14. const unsigned char c = input[i];
  15. output.push_back(lut[c >> 4]);
  16. output.push_back(lut[c & 15]);
  17. }
  18. return output;
  19. }
  20.  
  21. std::string encrypt(std::string msg, std::string key)
  22. {
  23. // Make sure the key is at least as long as the message
  24. std::string tmp(key);
  25. while (key.size() < msg.size())
  26. key += tmp;
  27.  
  28. // And now for the encryption part
  29. for (std::string::size_type i = 0; i < msg.size(); ++i)
  30. msg[i] ^= key[i];
  31. return msg;
  32. }
  33. std::string decrypt(std::string msg, std::string key)
  34. {
  35. return encrypt(msg, key); // lol
  36. }
  37.  
  38. int main()
  39. {
  40. char s1[] = "\x25\x0A\x02\x07\x0A\x59\x3A\x00\x1C\x07\x01\x58";
  41. char s2[] = "\x25\x0A\x02\x07\x0A\x57\x4D\x3B\x06\x02\x16\x59\x04\x1C\x4E\x0A\x45\x0D\x08\x1C\x1A\x4B\x0A\x1F\x4D\x0A\x00\x08\x17\x00\x1D\x1B\x07\x05\x02\x59\x1E\x1B\x1C\x02\x0B\x1E\x1E\x4F\x07\x05\x45\x3A\x46\x44\x40";
  42. std::cout << string_to_hex(encrypt("Hello World!", "monkey")) << std::endl;
  43. std::cout << decrypt(std::string(std::begin(s1), std::end(s1)-1), "monkey") << std::endl;
  44. std::cout << string_to_hex(encrypt("Hello. This is a test of encrypting strings in C++.", "monkey")) << std::endl;
  45. std::cout << decrypt(std::string(std::begin(s2), std::end(s2)-1), "monkey") << std::endl;
  46.  
  47. }
  48.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
250A02070A593A001C070158
Hello World!
250A02070A574D3B06021659041C4E0A450D081C1A4B0A1F4D0A000817001D1B070502591E1B1C020B1E1E4F0705453A464440
Hello. This is a test of encrypting strings in C++.