cryptography-utils/vigenere.cc

38 lines
648 B
C++

#include <iostream>
int keyvalue(char c)
{
return std::tolower(c) - 'a';
}
int main(int argc, char** argv)
{
char* text;
char* key;
if(argc < 3)
{
std::cout << "Usage: " << argv[0] << " <plain text> <key>" << std::endl;
return 0;
}
text = argv[1];
key = argv[2];
int ki = 0;
for(int pi = 0; text[pi] != 0; ++pi)
{
if(text[pi] == ' ')
{
std::cout << ' ';
continue;
}
// has to be int, because the result can sometimes be > 127
int c = std::tolower(text[pi]) + keyvalue(key[ki]);
if(c < 'a') c+=26;
if(c > 'z') c-=26;
std::cout << (char)c;
if(key[++ki] == 0) ki = 0;
}
std::cout << std::endl;
return 0;
}