gpt4 book ai didi

c++ - 学校作业的简单密码加密

转载 作者:太空狗 更新时间:2023-10-29 23:05:23 24 4
gpt4 key购买 nike

好的,我正在为大学类(class)构建一个程序,这是一个简单的程序,它使用结构来模拟构建用户信息以及用户名和密码的数据库。对于实验室的额外学分,我们可以加密密码。这没什么特别的......我们没有使用 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

我们还必须反转字符串,但这相当简单。

我的两个问题是:

  1. 有没有更简单的方法来做到这一点?

  2. 我这辈子都无法找出正确的算法来解密密码,但我需要这样做,以便在用户登录时,计算机可以将他们键入的内容与纯文本进行比较他们密码的版本。

注意:我绝不是一个专业的编码员,所以请对菜鸟手下留情,不要对我太高级。我找遍了所有地方,但找不到任何对我的具体情况真正有帮助的东西。

因此,我将自己置于互联网的摆布之下。 :D

最佳答案

  1. 有没有更简单的方法来做到这一点?

-- 我觉得你的方法不错。您的方法很清楚也很简单。

  1. 我这辈子都无法找出正确的算法来解密密码,但我需要这样做,以便当用户登录时,计算机可以将他们键入的内容与他们的纯文本版本进行比较密码。

-- 你不应该尝试解密你的密码。用户登录时,使用加密后的密码进行比对。

关于c++ - 学校作业的简单密码加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com