Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void encodeVignère(std::tstring& input, const std::tstring& key) {
- std::function<bool (std::tstring::value_type)> predicate =
- [](std::tstring::value_type value)
- {
- return !((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
- };
- std::tstring::iterator invalidChar = std::find_if(input.begin(), input.end(), predicate);
- if(invalidChar != input.end())
- throw std::exception("Invalid input provided!");
- std::tstring::const_iterator invalidKey = std::find_if(key.begin(), key.end(), predicate);
- if(invalidKey != key.end())
- throw std::exception("Invalid key provided!");
- uint32 keyIndex = 0;
- for(std::tstring::reference ref : input) {
- std::tstring::const_reference keyChar = key[(keyIndex++) % key.length()];
- uint32 keyOffset = 0;
- if(keyChar >= 'A' && keyChar <= 'Z')
- keyOffset = (uint32)(keyChar - 'A');
- else
- keyOffset = (uint32)(keyChar - 'z');
- bool isUpperCase = (ref >= 'A' && ref <= 'Z');
- std::tstring::value_type maxChar = isUpperCase ? 'Z' : 'z';
- std::tstring::value_type minChar = isUpperCase ? 'A' : 'a';
- uint32 newKey = (uint32)ref + keyOffset;
- newKey -= minChar;
- newKey %= 26;
- newKey += minChar;
- ref = (std::tstring::value_type)newKey;
- }
- }
- void decodeVignère(std::tstring& input, const std::tstring& key) {
- std::function<bool (std::tstring::value_type)> predicate =
- [](std::tstring::value_type value)
- {
- return !((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
- };
- std::tstring::iterator invalidChar = std::find_if(input.begin(), input.end(), predicate);
- if(invalidChar != input.end())
- throw std::exception("Invalid input provided!");
- std::tstring::const_iterator invalidKey = std::find_if(key.begin(), key.end(), predicate);
- if(invalidKey != key.end())
- throw std::exception("Invalid key provided!");
- uint32 keyIndex = 0;
- for(std::tstring::reference ref : input) {
- std::tstring::const_reference keyChar = key[(keyIndex++) % key.length()];
- uint32 keyOffset = 0;
- if(keyChar >= 'A' && keyChar <= 'Z')
- keyOffset = (uint32)(keyChar - 'A');
- else
- keyOffset = (uint32)(keyChar - 'z');
- bool isUpperCase = (ref >= 'A' && ref <= 'Z');
- std::tstring::value_type maxChar = isUpperCase ? 'Z' : 'z';
- std::tstring::value_type minChar = isUpperCase ? 'A' : 'a';
- int32 newKey = (int32)ref - keyOffset;
- newKey -= minChar;
- if(newKey < 0)
- newKey = 26 + newKey;
- newKey += minChar;
- ref = (std::tstring::value_type)newKey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment