好的,我正在为一门大学课程构建一个程序,它是一个简单的程序,使用结构来模拟建立一个用户信息数据库,包括用户名和密码。在实验中可以加分,我们可以对密码进行加密。这并不是什么特别的事情……我们没有使用MD5或其他高级技术。
我只需要能够将大写字母转换为小写字母,将小写字母转换为大写字母,最后,我遇到问题的部分是将十进制整数转换为十六进制。
我将尝试发布程序的相关部分而不是整个程序。
以下是结构:
注意:这是一个结构体的动态数组。
我们还需要翻转字符串,但这相当简单。我的两个问题是:
1. 有没有更简单的方法?
2. 我怎么也想不出正确的算法来解密密码,我需要这样做,以便用户登录时,计算机可以将他们输入的内容与其密码的明文版本进行比较。
注意:我绝不是专业的编码人员,所以请对新手仁慈,尽量不要让我太超前。我已经到处找过,但我找不到任何真正帮助我解决特定情况的东西。因此,我把自己投入了互联网的怀抱。 :D
我只需要能够将大写字母转换为小写字母,将小写字母转换为大写字母,最后,我遇到问题的部分是将十进制整数转换为十六进制。
我将尝试发布程序的相关部分而不是整个程序。
以下是结构:
struct Info
{
string sFname;
string sLname;
string sUname;
string sPword;
string sAddress;
string sEmail;
string sPhone;
};
注意:这是一个结构体的动态数组。
Info *Users;
Users = new Info[size];
这是我目前编写的加密密码代码:
//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;
因此,如果您的密码是:
456Pass45word87
加密后的内容将如下所示:
1c8pASS2dWORD57
我们还需要翻转字符串,但这相当简单。我的两个问题是:
1. 有没有更简单的方法?
2. 我怎么也想不出正确的算法来解密密码,我需要这样做,以便用户登录时,计算机可以将他们输入的内容与其密码的明文版本进行比较。
注意:我绝不是专业的编码人员,所以请对新手仁慈,尽量不要让我太超前。我已经到处找过,但我找不到任何真正帮助我解决特定情况的东西。因此,我把自己投入了互联网的怀抱。 :D
123AbCd
加密后变成了7baBcD
。你怎么知道a
不再是你的密码的一部分了呢?你需要像分隔符这样的东西。因此,即使考虑到解密的教育价值,我仍然会选择allejo的方法。 - Kiruse