How would I go about modifying this code to accept input from the user rather than using a predetermined string? Specifically, I need the program to require exactly two command line arguments. The first will either be the code "-e" or "-d" to indicate encoding or decoding of a message (this determines adding or subtracting you shift values) and the second parameter will be a single word that will be the keyword that you use for the encryption or decryption.
#include <iostream>
#include <cstring>
#include <algorithm>
// Vigenere Cipher Methods:
// Note: assumes that both strings as arguments have length > 0, and that
// the key only contains letters of the alphabet from [A-Z]
void vigenere_encrypt(std::string& s, std::string key)
{
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
unsigned int j = 0;
for (int i = 0; i < s.length(); i++)
{
if (isalpha(s[i]))
{
s[i] += key[j] - 'A';
if (s[i] > 'Z') s[i] += -'Z' + 'A' - 1;
}
j = j + 1 == key.length() ? 0 : j + 1;
}
}
void vigenere_decrypt(std::string& s, std::string key)
{
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
unsigned int j = 0;
for (int i = 0; i < s.length(); i++)
{
if (isalpha(s[i]))
{
s[i] = s[i] >= key[j] ?
s[i] - key[j] + 'A' :
'A' + ('Z' - key[j] + s[i] - 'A') + 1;
}
j = j + 1 == key.length() ? 0 : j + 1;
}
}
int main(void)
{
std::string s("AceInfinity's Example");
std::string key("Passkey");
vigenere_encrypt(s, key);
std::cout << "Encrypted: " << s << std::endl;
vigenere_decrypt(s, key);
std::cout << "Decrypted: " << s << std::endl;
return 0;
}
Update
Thanks to your input and a few other sources I have re-developed my main code as is shown below. I am having trouble getting the program to decrypt and encrypt strings properly and I am not sure if the error is somewhere in the code itself, or operator error. Does anything look out of the ordinary here and how could I get this code functional to where it can encrypt or decrypt given user input?
#include <iostream>
#include <string>
int usage( const char* exe_name )
{
std::cerr << "Usage: " << exe_name << " -e <text to encrypt>\n"
<< " " << exe_name << " -d <text to decrypt>\n" ;
return 1 ;
}
int main( int argc, char* argv[] )
{
if (argc < 3 ) return usage( argv[0] ) ;
const std::string option = argv[1];
std::string text = argv[2];
// cat the remaining cmd line args
for( int i = 3 ; i < argc ; ++i ) { text += ' ' ; text += argv[i] ; }
const std::string key("Passkey");
if ( option== "-e" )
std::cout << "Encrypt: '" << text << "'\n" ;
else if ( option == "-d" )
std::cout << "Decrypt: '" << text << "'\n" ;
else
{
std::cout << "Unrecognised command line option '" << option << "'\n";
return usage( argv[0] ) ;
}
}
If you want command line arguments, you'll need to change the prototype of your main function a little bit and use the standard
argv
array:Minimal effort made for a quick example, code probably works, e&oe, caveat emptor, etc.
Of course, you'd really be best off using a proper command line argument parser, like getopt, and you'll still need some way to supply the plaintext for encryption or ciphertext for decription, but that's left as an exercise for the reader. Reading from
stdin
by usingstd::cin
is one way of doing so, for example.