Hello,
I am trying to incorporated into my program a way for the user to enter there own key to encrypt to.
right now I have it encrypting and decrypting to a default key when the user enters default, but if the user enters there own code at this junction how do I take that new key in and use it?
can someone explain how to do this?
or just give a hint, anything will help.
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main ()
{
string method, map, word, str;
int encryption, decryption;
cout<<"What is the method (encryption or decryption)?"<<endl;
cin>>method;
if (method=="encryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"encrypted word: "<<word<<endl;
}
else
cout<<"not valid"<<endl;
}
elseif (method=="decryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"decrypted word: "<<word<<endl;
}
else
cout<<"not valid"<<endl;
}
else
cout<<"Error: invalid method choice."<<endl;
return 0;
}
I really hope your code isn't formatted like that. :)
Here's an example:
1 2 3 4 5 6 7 8 9 10
std::string Encrypt(const std::string& source, const std::string& key)
{
std::string temp; temp = ""; // Store the encryped text
int tempk; tempk = 0; // Store the key
for(int j = 0; j < key.length(); j++) tempk += (int)key[j]; // Key is the char values added together
for(int i = 0; i < source.length(); i++) temp += (char)source[i] ^ tempk; // Encrypt each letter of the word
return temp;
}
for example
the user chooses encryption
next the user inputs their own key, that is no more than 26 characters.
nhjuyhgf
next the code assigns
a to n
b to h
c to j
d to u
etc...
next the user enters a word... : help
the output would be: belp
Its hard to do it without a specific pattern ._. I know there's another method of caesar chipper instead of using a single charater as a key, it use a word as a key example
The word is "what"
And the key is "abc"
First the key word should be extended up to numbers of letter in descrypted word
So the key will be "abca"
Then encryption begn :D
w -> a = w
h -> b = i
a -> c = c
t -> d = x
Done o,o