Cromon

Main.cpp

Oct 24th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. void encodeVignère(std::tstring& input, const std::tstring& key) {
  2.     std::function<bool (std::tstring::value_type)> predicate =
  3.         [](std::tstring::value_type value)
  4.         {
  5.             return !((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
  6.         };
  7.  
  8.     std::tstring::iterator invalidChar = std::find_if(input.begin(), input.end(), predicate);
  9.     if(invalidChar != input.end())
  10.         throw std::exception("Invalid input provided!");
  11.  
  12.     std::tstring::const_iterator invalidKey = std::find_if(key.begin(), key.end(), predicate);
  13.     if(invalidKey != key.end())
  14.         throw std::exception("Invalid key provided!");
  15.  
  16.     uint32 keyIndex = 0;
  17.     for(std::tstring::reference ref : input) {
  18.         std::tstring::const_reference keyChar = key[(keyIndex++) % key.length()];
  19.         uint32 keyOffset = 0;
  20.         if(keyChar >= 'A' && keyChar <= 'Z')
  21.             keyOffset = (uint32)(keyChar - 'A');
  22.         else
  23.             keyOffset = (uint32)(keyChar - 'z');
  24.  
  25.         bool isUpperCase = (ref >= 'A' && ref <= 'Z');
  26.         std::tstring::value_type maxChar = isUpperCase ? 'Z' : 'z';
  27.         std::tstring::value_type minChar = isUpperCase ? 'A' : 'a';
  28.  
  29.         uint32 newKey = (uint32)ref + keyOffset;
  30.         newKey -= minChar;
  31.         newKey %= 26;
  32.         newKey += minChar;
  33.  
  34.         ref = (std::tstring::value_type)newKey;
  35.     }
  36. }
  37.  
  38. void decodeVignère(std::tstring& input, const std::tstring& key) {
  39.     std::function<bool (std::tstring::value_type)> predicate =
  40.         [](std::tstring::value_type value)
  41.         {
  42.             return !((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
  43.         };
  44.  
  45.     std::tstring::iterator invalidChar = std::find_if(input.begin(), input.end(), predicate);
  46.     if(invalidChar != input.end())
  47.         throw std::exception("Invalid input provided!");
  48.  
  49.     std::tstring::const_iterator invalidKey = std::find_if(key.begin(), key.end(), predicate);
  50.     if(invalidKey != key.end())
  51.         throw std::exception("Invalid key provided!");
  52.  
  53.     uint32 keyIndex = 0;
  54.     for(std::tstring::reference ref : input) {
  55.         std::tstring::const_reference keyChar = key[(keyIndex++) % key.length()];
  56.         uint32 keyOffset = 0;
  57.         if(keyChar >= 'A' && keyChar <= 'Z')
  58.             keyOffset = (uint32)(keyChar - 'A');
  59.         else
  60.             keyOffset = (uint32)(keyChar - 'z');
  61.  
  62.         bool isUpperCase = (ref >= 'A' && ref <= 'Z');
  63.         std::tstring::value_type maxChar = isUpperCase ? 'Z' : 'z';
  64.         std::tstring::value_type minChar = isUpperCase ? 'A' : 'a';
  65.  
  66.         int32 newKey = (int32)ref - keyOffset;
  67.         newKey -= minChar;
  68.         if(newKey < 0)
  69.             newKey = 26 + newKey;
  70.  
  71.         newKey += minChar;
  72.  
  73.         ref = (std::tstring::value_type)newKey;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment