【发布时间】:2013-11-14 17:37:18
【问题描述】:
好的,我正在为大学课程构建一个程序,它是一个简单的程序,它使用结构来模拟构建用户信息以及用户名和密码的数据库。对于实验室的额外信用,我们可以加密密码。这没什么特别的……我们没有使用 MD5 或类似的高级工具。
我需要做的就是能够将大写字符转换为小写字母,将小写字符转换为大写字母,最后是我遇到问题的部分,将十进制整数转换为十六进制。
我将尝试只发布程序的相关部分而不是整个内容。
结构如下:
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
我们还必须反转字符串,但这很简单。
我的 2 个问题是这样的:
有没有更简单的方法来做到这一点?
在我的一生中,我无法找出正确的算法来解密密码,我需要这样做,以便当用户登录时,计算机可以将他们键入的内容与纯文本进行比较他们的密码版本。
注意:我绝不是专业的编码员,所以请对菜鸟手下留情,尽量不要对我太超前。我找遍了整个地方,但找不到任何对我的具体情况真正有帮助的东西。
因此,我将自己置于互联网的摆布之下。 :D
【问题讨论】:
-
您永远不应该解密密码,只需将他们输入的密码加密并加密,然后将其与加密后的密码进行比较。
-
完全同意 allejo。尽管如此,由于这只是一个知道如何反转加密过程的任务,因此仍然具有教育意义。您需要做的是向后应用加密过程。例如。首先反转字符串,从大到小反之亦然,然后将十六进制转换回十进制形式。
-
这不是真正的加密算法,因为它是不可逆的。 “ab”可能意味着输入是“AB”,也可能意味着输入是“171”。
-
考虑异或的工作原理。例如,当您使用空格字符对 ASCII 字符集中的字符进行异或运算时,这些字符会发生什么情况。
-
我一辈子都想不出正确的算法来解密密码。为什么?考虑:
123AbCd加密:7baBcD你怎么知道a不再是你密码的一部分?你需要像分隔符这样的东西。因此,即使在解密的教育价值方面,我仍然会采用 allejo 的方法。
标签: c++ encryption passwords hex