edvinson
asked on
Simple Character Substitution Encryption
I am trying to make my own simple encryption program to encrypt and decrypt a string.
The string will consist of only numbers. I will be using my own simple substitution method, which is as follows:
Plain alphabet 0 1 2 3 4 5 6 7 8 9
Cipher alphabet 2 4 5 0 8 9 1 3 6 7
Here is my encryption table:
Let's assume we encrypt the password: 456
The encrypted text would be: 891 ( using our cipher alphabet ), right?
That is the program I am trying to write.
Here is the skeleton program I have so far, and I stripped it down to this because I am running into errors trying to manipulate the string characters.
Can someone help me with this simple example I can expand on?
Thanks!
The string will consist of only numbers. I will be using my own simple substitution method, which is as follows:
Plain alphabet 0 1 2 3 4 5 6 7 8 9
Cipher alphabet 2 4 5 0 8 9 1 3 6 7
Here is my encryption table:
Let's assume we encrypt the password: 456
The encrypted text would be: 891 ( using our cipher alphabet ), right?
That is the program I am trying to write.
Here is the skeleton program I have so far, and I stripped it down to this because I am running into errors trying to manipulate the string characters.
Can someone help me with this simple example I can expand on?
Thanks!
#include <iostream>
#include <string>
using namespace std;
//---------------------------------------------------------------------------
// Global Variables
//---------------------------------------------------------------------------
/* This is the correct DECRYPTED password */
string correctNumber("456");
/* String var to hold users guess at password.
This variables value will need to be run through
the DECRYPT function to see if it matches the value
of correctNumber
*/
string guess;
/* This is the encrypted password that will be
built and encrypted based upon what the user
types in. This will ultimately be compared to
the encrypted version of correctNumber
*/
string userBuiltPassword;
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
string encryptString(string strToEncrypt);
string decryptString(string strToDecrypt);
//---------------------------------------------------------------------------
int main()
{
cout << "The password has been generated! You need to guess it." << endl;
cout << "Enter password: ";
cin >> guess;
string tempEnc = encryptString(guess); // Get encrypted version of user guess
string tempCorr = encryptString(correctNumber); // Get encrypted version of correct
if(tempEnc == tempCorr)
{
cout << "You cracked the code!" << endl;
} else
{
cout << "Incorrect. You lose." << endl;
}
return 0;
}
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
/**
* Encrypts a string (using a global constant key)
*
* Takes a string and returns the encrypted version of the string.
* The string is generated using a very simple character substitution
* method.
*
* @param string strToEncrypt String to encrypt
* @return string strEnc Encrypted string
*
*/
string encryptString(string strToEncrypt)
{
string strEnc;
return strEnc;
}
/**
* Decrypts a string
*
* Takes a string and returns the decrypted version of the string.
* The string is generated using a very simple character substitution
* method.
*
* @param string strToDecrypt String to decrypt
* @return string strDec Decrypted string
*
*/
string decryptString(string strDec)
{
string decString;
return decString;
}
ASKER
Stripping this down to it's most simplest....
Let's assume the correct plain text is 0...
this code is not working:
I know I am close, but new to C++ , so probably not as close as I think! lol
Let's assume the correct plain text is 0...
this code is not working:
#include <iostream>
#include <string>
using namespace std;
//---------------------------------------------------------------------------
// Global Variables
//---------------------------------------------------------------------------
/* This is the correct DECRYPTED password */
string correctNumber("0");
/* Not sure if we need this, or how to use this in my solution,
however, something like this seems to make sense.
*/
int plainIntArray [] = {0,1,2,3,4,5,6,7,8,9};
int encIntArray [] = {2,4,5,0,8,9,1,3,6,7};
/* String var to hold users guess at password.
This variables value will need to be run through
the DECRYPT function to see if it matches the value
of correctNumber
*/
string guess;
/* This is the encrypted password that will be
built and encrypted based upon what the user
types in. This will ultimately be compared to
the encrypted version of correctNumber
*/
string userBuiltPassword;
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
string encryptString(string strToEncrypt);
//---------------------------------------------------------------------------
int main()
{
cout << "The password has been generated! You need to guess it." << endl;
cout << "Enter password: ";
cin >> guess;
string tempEnc = encryptString(guess);
string tempCorr = encryptString(correctNumber); // Get encrypted version of correct
if(tempEnc == tempCorr)
{
cout << "You cracked the code!" << endl;
} else
{
cout << "Incorrect. You lose." << endl;
}
return 0;
}
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
/**
* Encrypts a string (using a global constant key)
*
* Takes a string and returns the encrypted version of the string.
* The string is generated using a very simple character substitution
* method.
*
* @param string strToEncrypt String to encrypt
* @return string strEnc Encrypted string
*
*/
string encryptString(string strToEncrypt)
{
// Loop through all characters of parameter
for(int i = 0; i<= strToEncrypt.length(); i++)
{
// Check for "0"
if(strToEncrypt.substr(i,1) == "0")
{
userBuiltPassword.substr(i,1) = "2";
}
}
return userBuiltPassword;
}
I know I am close, but new to C++ , so probably not as close as I think! lol
>> 83 : for(int i = 0; i<= strToEncrypt.length(); i++)
The characters in the string are starting at index 0, and ending at index (strToEncrypt.length() - 1). Make sure not to read too much.
>> 90 : userBuiltPassword.substr(i ,1) = "2";
That's not really how things work with std::string's.
Try something more like this :
userBuiltPassword += "2";
which would add "2" to the end of the existing string (given that you start with an empty string).
The characters in the string are starting at index 0, and ending at index (strToEncrypt.length() - 1). Make sure not to read too much.
>> 90 : userBuiltPassword.substr(i
That's not really how things work with std::string's.
Try something more like this :
userBuiltPassword += "2";
which would add "2" to the end of the existing string (given that you start with an empty string).
ASKER
hmmm, not working.
As expected, this simple problem is proving more difficult than expected.
In psuedocode, I want to:
1. encrypt the plain text correct number 0 , which would be: 2
2. and simply compare that to the number entered by the user.
I still don't see why my code is not working?
And not only that, I need to know how to check for more than one number, which would include arrays, right? And again, in my code, I don't see where I am going wrong.
Can you post some working code as proof of concept for your solution? I simply can't get it to work!
Ugh...
As expected, this simple problem is proving more difficult than expected.
In psuedocode, I want to:
1. encrypt the plain text correct number 0 , which would be: 2
2. and simply compare that to the number entered by the user.
I still don't see why my code is not working?
And not only that, I need to know how to check for more than one number, which would include arrays, right? And again, in my code, I don't see where I am going wrong.
Can you post some working code as proof of concept for your solution? I simply can't get it to work!
Ugh...
>> I still don't see why my code is not working?
I can't see it either, since you seem to have forgotten to post the new code ;)
I can't see it either, since you seem to have forgotten to post the new code ;)
ASKER
Here is latest code, incorporating your suggestion:
Not working still..
#include <iostream>
#include <string>
using namespace std;
//---------------------------------------------------------------------------
// Global Variables
//---------------------------------------------------------------------------
/* This is the correct DECRYPTED password */
string correctNumber("0");
/* Not sure if we need this, or how to use this in my solution,
however, something like this seems to make sense.
*/
int plainIntArray [] = {0,1,2,3,4,5,6,7,8,9};
int encIntArray [] = {2,4,5,0,8,9,1,3,6,7};
/* String var to hold users guess at password.
This variables value will need to be run through
the DECRYPT function to see if it matches the value
of correctNumber
*/
string guess;
/* This is the encrypted password that will be
built and encrypted based upon what the user
types in. This will ultimately be compared to
the encrypted version of correctNumber
*/
string userBuiltPassword;
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
string encryptString(string strToEncrypt);
//---------------------------------------------------------------------------
int main()
{
cout << "The password has been generated! You need to guess it." << endl;
cout << "Enter password: ";
cin >> guess;
string tempEnc = encryptString(guess);
string tempCorr = encryptString(correctNumber);; // Get encrypted version of correct
if(tempEnc == tempCorr )
{
cout << "You cracked the code!" << endl;
} else
{
cout << "Incorrect. You lose." << endl;
}
return 0;
}
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
/**
* Encrypts a string (using a global constant key)
*
* Takes a string and returns the encrypted version of the string.
* The string is generated using a very simple character substitution
* method.
*
* @param string strToEncrypt String to encrypt
* @return string strEnc Encrypted string
*
*/
string encryptString(string strToEncrypt)
{
// Loop through all characters of parameter
for(int i = 0; i<= strToEncrypt.length(); i++)
{
// Check for "0"
if(strToEncrypt.substr(i,1) == "0")
{
userBuiltPassword += "2";
}
// Check for "2"
if(strToEncrypt.substr(i,1) == "2")
{
userBuiltPassword += "0";
}
}
return userBuiltPassword;
}
Not working still..
I'm not sure why you're translating a 2 into a 0 - that's not according to your substitution alphabet.
But the real issue is because of what I said earlier :
>> (given that you start with an empty string).
That was an important pre-condition for the approach to work : userBuiltPassword needs to be empty at the start of the encryptString function.
But, since you are using a global variable for it, and call the same function more than once, the second time the function is called, userBuiltPassword is not empty.
Why did you use a global variable in the first place ? Why not a local variable ?
But the real issue is because of what I said earlier :
>> (given that you start with an empty string).
That was an important pre-condition for the approach to work : userBuiltPassword needs to be empty at the start of the encryptString function.
But, since you are using a global variable for it, and call the same function more than once, the second time the function is called, userBuiltPassword is not empty.
Why did you use a global variable in the first place ? Why not a local variable ?
ASKER
I think I have it working, could you please double check?
I know there is an easier way, but it appears to work. Can you just double check and offer suggestions?
Thanks.
#include <iostream>
#include <string>
using namespace std;
//---------------------------------------------------------------------------
// Global Variables
//---------------------------------------------------------------------------
/* This is the correct DECRYPTED password */
string correctNumber("240");
/* Not sure if we need this, or how to use this in my solution,
however, something like this seems to make sense.
*/
int plainIntArray [] = {0,1,2,3,4,5,6,7,8,9};
int encIntArray [] = {2,4,5,0,8,9,1,3,6,7};
/* String var to hold users guess at password.
This variables value will need to be run through
the DECRYPT function to see if it matches the value
of correctNumber
*/
string guess;
/* This is the encrypted password that will be
built and encrypted based upon what the user
types in. This will ultimately be compared to
the encrypted version of correctNumber
*/
string userBuiltPassword;
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
string encryptString(string strToEncrypt);
DWORD GetSerial();
//---------------------------------------------------------------------------
int main()
{
cout << "The password has been generated! You need to guess it." << endl;
cout << "Enter password: ";
cin >> guess;
string tempEnc = guess;
string tempCorr = encryptString(correctNumber); // Get encrypted version of correct
if(tempEnc == tempCorr )
{
cout << "You cracked the code!" << endl;
} else
{
cout << "Incorrect. You lose." << endl;
}
return 0;
}
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
/**
* Encrypts a string (using a global constant key)
*
* Takes a string and returns the encrypted version of the string.
* The string is generated using a very simple character substitution
* method.
*
* @param string strToEncrypt String to encrypt
* @return string strEnc Encrypted string
*
*/
string encryptString(string strToEncrypt)
{
string returnString;
// Loop through all characters of parameter
for(int i = 0; i<= strToEncrypt.length(); i++)
{
// Check for "0"
if(strToEncrypt.substr(i,1) == "0")
{
returnString += "2";
}
// Check for "1"
if(strToEncrypt.substr(i,1) == "1")
{
returnString += "4";
}
// Check for "2"
if(strToEncrypt.substr(i,1) == "2")
{
returnString += "5";
}
// Check for "3"
if(strToEncrypt.substr(i,1) == "3")
{
returnString += "0";
}
// Check for "4"
if(strToEncrypt.substr(i,1) == "4")
{
returnString += "8";
}
// Check for "5"
if(strToEncrypt.substr(i,1) == "5")
{
returnString += "9";
}
// Check for "6"
if(strToEncrypt.substr(i,1) == "6")
{
returnString += "1";
}
// Check for "7"
if(strToEncrypt.substr(i,1) == "7")
{
returnString += "3";
}
// Check for "8"
if(strToEncrypt.substr(i,1) == "8")
{
returnString += "6";
}
// Check for "9"
if(strToEncrypt.substr(i,1) == "9")
{
returnString += "7";
}
}
return returnString;
}
I know there is an easier way, but it appears to work. Can you just double check and offer suggestions?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I agree with your solution suggestions.
Thank you very much,
Thank you very much,
What did you try ? How far did you get ?