Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void encodeVignère_Legacy(char* input, const char* key) {
- uint32 lenInput = strlen(input);
- uint32 lenKey = strlen(key);
- for(uint32 i = 0; i < max(lenInput, lenKey); ++i) {
- if(i < lenInput)
- {
- char value = input[i];
- if(!((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')))
- throw std::exception("Invalid input provided!");
- }
- if(i < lenKey)
- {
- char value = key[i];
- if(!((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')))
- throw std::exception("Invalid key provided!");
- }
- }
- for(uint32 i = 0, keyIndex = 0; i < lenInput; ++i, ++keyIndex) {
- char& ref = input[i];
- const char& keyChar = key[keyIndex % lenKey];
- uint32 keyOffset = 0;
- if(keyChar >= 'A' && keyChar <= 'Z')
- keyOffset = (uint32)(keyChar - 'A');
- else
- keyOffset = (uint32)(keyChar - 'z');
- bool isUpperCase = (ref >= 'A' && ref <= 'Z');
- char minChar = isUpperCase ? 'A' : 'a';
- uint32 newKey = (uint32)ref + keyOffset;
- newKey -= minChar;
- newKey %= 26;
- newKey += minChar;
- ref = (char)newKey;
- }
- }
- void decodeVignère_Legacy(char* input, const char* key) {
- uint32 lenInput = strlen(input);
- uint32 lenKey = strlen(key);
- for(uint32 i = 0; i < max(lenInput, lenKey); ++i) {
- if(i < lenInput)
- {
- char value = input[i];
- if(!((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')))
- throw std::exception("Invalid input provided!");
- }
- if(i < lenKey)
- {
- char value = key[i];
- if(!((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')))
- throw std::exception("Invalid key provided!");
- }
- }
- for(uint32 i = 0, keyIndex = 0; i < lenInput; ++i, ++keyIndex) {
- char& ref = input[i];
- const char& keyChar = key[keyIndex % lenKey];
- uint32 keyOffset = 0;
- if(keyChar >= 'A' && keyChar <= 'Z')
- keyOffset = (uint32)(keyChar - 'A');
- else
- keyOffset = (uint32)(keyChar - 'z');
- bool isUpperCase = (ref >= 'A' && ref <= 'Z');
- char minChar = isUpperCase ? 'A' : 'a';
- int32 newKey = (int32)ref - keyOffset;
- newKey -= minChar;
- if(newKey < 0)
- newKey = 26 + newKey;
- newKey += minChar;
- ref = (char)newKey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment