Okay, so I'm building a program for a college class, it's a simple program that uses structures to simulate building a database of user information along with usernames and passwords. For extra credit on the lab, we can encrypt the passwords. This isn't anything special....we're not using MD5 or anything advanced like that.
All I need to do is be able to switch uppercase characters to lowercase, lowercase characters to uppercase, and, finally, the part I'm having trouble with, converting decimal integers into hexadecimal.
I'll try to post just the relevant parts of the program instead of the whole thing.
Here's the structure:
struct Info
{
string sFname;
string sLname;
string sUname;
string sPword;
string sAddress;
string sEmail;
string sPhone;
};
Note: This is a dynamic array of structures
Info *Users;
Users = new Info[size];
And here's the code to encrypt the password that I have so far:
//string length for controlling loops
strlen = Users[iUsrCount].sPword.length();
//temp string to hold the encrypted version of password
string temp;
//switch uppercase characters to lowercase and vice versa, and convert
//decimal integers into hexadecimal
for(int i=0; i<strlen; i++)
{
cout << "\n\nInside encryption for loop iteration " << i << "\n\n";
if(islower(Users[iUsrCount].sPword[i]))
{
temp += toupper(Users[iUsrCount].sPword[i]);
continue;
}
else if(isupper(Users[iUsrCount].sPword[i]))
{
temp += tolower(Users[iUsrCount].sPword[i]);
continue;
}
else if(isdigit(Users[iUsrCount].sPword[i]))
{
char charNum = Users[iUsrCount].sPword[i];
int iDec = charNum - '0';
//get integer
while((i+1) < strlen && isdigit(Users[iUsrCount].sPword[i+1]))
{
i++;
iDec = iDec * 10 + Users[iUsrCount].sPword[i] - '0';
cout << " " << iDec << " ";
}
char hexTemp[10];
//convert
sprintf(hexTemp, "%x", iDec);
temp += hexTemp;
//debugging cout to make sure hexes are properly calculated
cout << " " << hexTemp << " ";
continue;
}
}
//debugging cout to make sure that password is properly encrypted
cout << endl << endl << temp << endl << endl;
strlen = temp.length();
//overwrite the plain text password with the encrypted version
for(int i=0; i<strlen; i++)
Users[iUsrCount].sPword[i] = temp[i];
//debugging cout to make sure copy was successful
cout << endl << endl << Users[iUsrCount].sPword;
So if your password was:
456Pass45word87
It would look like this when encrypted:
1c8pASS2dWORD57
We also have to reverse the string, but that's fairly simple.
My 2 questions are this:
Is there any easier way to do this?
I cannot, for the life of me, figure out the right algorithm to DECRYPT the password, which I will need to do so that when the user logs in, the computer can compare what they type to the plain text version of their password.
Note: I am by no means a professional coder, so please have mercy on a noob and try not to get too super-advanced on me. I've looked all over the place but I can't find anything that is really helping me with my specific situation.
And so I throw myself upon the mercy of the Internets. :D