我在写密码程序。程序首先要求用户输入包含8个或更多包含字符和标点符号的字符的密码。密码将无法正确加密(携带太多字符)
后,该方案将通过在中间的密码的第二半移动到前增加一个“+”,然后将第一半附加到端“加密”的密码。
我得到的问题是,每当我输入密码,那就是奇怪的字符它复制的最后一个字符两次,是打破我的程序。我该如何解决?我知道我必须添加一个条件,因为有奇怪的字符,但是它应该包含idk。这里是我的代码:
// write a function to encrypt the password:
// return a dynamically allocated C string of the exact size (no extra room) with:
// the last half of the password, the + symbol, and then the first half of the password
char * encrypt (char pw[])
{
int exactsize = strlen(pw) + 2;
char * encPW = new char[exactsize];
int mid = exactsize/2;
strcpy(encPW, pw + mid);
strcat(encPW, "+");
strncat(encPW, pw, exactsize - mid);
return encPW;
}
// write the same (overloaded) function for a C++ String
char * encrypt (string pw)
{
int exactsize = pw.length() + 2;
char * encPW = new char[exactsize];
int mid = exactsize/2;
string temp(pw, mid);
string temp2(pw, 0 ,mid);
temp = temp + "+" + temp2;
strcpy(encPW, temp.c_str());
return encPW;
}
here is the output i am getting and what i am talking about:
Enter your password: salehrocks93
add punctuation
Enter your password: salehrocks93/
Valid password format. Encrypting...
Encrypted password: cks93/+salehroc
Enter your new password: salehrocks93/
New Encrypted password: cks93/+salehro
1
Press any key to continue . . .
当它要求输入密码的第一次加密是重复的C两次
什么是理想的行为时,该密码是奇数的长度?例如,“密码”的加密应该是什么? – crockeea
一些无法回答的观察:1.您的API不平衡:函数分配内存,但需要调用者释放它 - 这是一个不好的做法。 2.你的重载是通过* value *来获得一个'std :: string',这意味着它在被调用时会创建一个本地副本,这使得它成为一个糟糕的设计选择。把它作为一个'const std :: string&'来引用它。 3.'温度=温度+“+” + TEMP2;'想什么,做... – kfsone
@Eric密码的加密应该始终是相同的取密码下半年把它放在第一的前面,在中间添加一个“+”。 – SalehK