Conversion between char & string

Hi,

I have a question on conversion between char & string.
I have cut & pasted the part of the code from my C++ code and my function "decryptPwd" uses C style code with "char" etc.

I need to pass a string (mypwd) somehow to this function after conversion & then compare it to another string (newmypwd).

I tried - "decryptPwd(mypwd.c_str())==newmypwd.c_str()" but it did not work.

Any help is much appriciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
#include <string>
..

char* decryptPwd(char hash[64]);

main ()
{

string mypwd;
string newmypwd;

if (decryptPwd(mypwd)==newmypwd)
    cout<<"Password is ..<<endl;

return 0
}
..
char* decryptPwd(char hash[64])
{

  static char password[64];

  
  return password;
  
}
..
.. 


Mathew
Hey Mathew,

You might simply try "String Compare", more commonly known as strcmp().

It has the form:
int strcmp ( const char * str1, const char * str2 );,
and compares 2 strings, character for character, starting at index 0. As soon as a conflicting character is found in the two compared strings strcmp will register that the strings differ. If the comparison process matches the two strings up until the null-character then they are deemed equal.

strcmp() returns an integer value related to the resulting comparison:

0: When returning a 0, the two strings are equal.

1+: str1 is greater than str2

-1: str1 is less than str2

Hope this helps.

here is one way to convert a string to a c_string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
using namespace std;

char* decryptPwd(char hash[64]);

int main ()
{

	string mypwd;
	string newmypwd;
	char myArray[64];
	strcpy(myArray, mypwd.c_str());

	decryptPwd(myArray);

	cin.ignore();
	return 0;
}

	char* decryptPwd(char hash[64])
{

	static char password[64];


	return password;

}


you could also use a for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;

char* decryptPwd(char hash[64]);

int main ()
{
	int i;
	string mypwd;
	string newmypwd;
	char myArray[64];
	for (i = 0; i < mypwd.length(); i++)
	{
		myArray[i] = mypwd.at(i);
	}
	myArray[i] = '\0';

	decryptPwd(myArray);

	cin.ignore();
	return 0;
}

	char* decryptPwd(char hash[64])
{

	static char password[64];


	return password;

}
Last edited on
If you're using C++, don't use C strings.

Your code should look something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
//...

std::string decryptPwd(const std::string& hash);

int main()
{
    std::string mypwd;
    std::string newmypwd;
    //...

    if (decryptPwd(mypwd)==newmypwd)
        std::cout <<"Password is: " << newmypwd << std::endl;

    return 0;
}

std::string decryptPwd(const std::string& hash)
{
    // ...
    return password; 
}
Last edited on
if(decryptPwd(mypwd.c_str())==newmypwd) // just add .c_str() to mypwd

should work, as long as newmypwd is a string, as string can be compared against a char* (not a char)

From: relational operators (string)
http://www.cplusplus.com/reference/string/string/operators/

1
2
3
bool operator== (const string& lhs, const string& rhs);
bool operator== (const char*   lhs, const string& rhs);
bool operator== (const string& lhs, const char*   rhs);


(these are non-member functions)

You could also use string::compare(), which behaves like strcmp().

if (0 == newmypwd.compare(decryptPwd(mypwd.c_str())))

But given the signature of decryptPwd

char* decryptPwd(char hash[64]);

it might just work if you change the signature to

string decryptPwd(const string& hash);

(as kbw has mentioned) as string provides operator[]. This assumes that decryptPwd uses [] internally, as suggested by the parameter).


Edit: After additional thought, the hash length of 64 makes me think this is actually going to be a binary hash. So you won't be able to handle it using std::string (it's just a "coincidence" that bytes and chars are the same thing in C++). But you could use a std::vector<char> or, in the C++11 case, a std::array<char, 64>. So the preferred signature for decryptPwd is probably:

std::string decryptPwd(const std::array<char, 64>& hash) // C++11

or

std::string decryptPwd(const std::vector<char>& hash) // C++03

As all of std::string, std::array, and std::vector provide operator[], decryptPwd might still work if you change the signature to use a standard container types rather than a C array.

Andy

PS From a security perspective, it might be better to use encryptPwd (assuming you have both decryptPwd and encryptPwd?) rather than decryptPwd to check passwords. That way you avoid having plain text passwords in memory. So you get the new password, you encrypt it, and compare it against the existing, encrypted one.
Last edited on
kbw wrote:
If you're using C++, don't use C strings.

This is the right answer.
@Yanson

Given you're using a fixed length buffer with a std::string, it would be better to use strncpy

1
2
3
	const size_t len_wanted = 64;
	char myArray[len_wanted + 1] = {0}; // + 1 for null teminator and zero init the array
	strncpy(myArray, mypwd.c_str(), len_wanted);


And if you use a for-loop, you need to handle the case when string::length() returns a higher value than the buffer length.

Andy

strncpy
http://www.cplusplus.com/reference/cstring/strncpy/
Last edited on
Hi All,

Thank you very much for all the replies. I managed to combine all the replies above & fixed it.

I used "strcpy" and "strcmp" in the function and change the function to be BOOL - bool decryptCiscoPwd(string hashpwd, string pwd) and got the if statemnet in main simplified as "if (decryptCiscoPwd(pwd, newmypwd))".

That means all the conversion between cstring to string is now in BOOL function and in main code, all are in string, as I wanted.

Your help was EXCELLENT. Much appriciated.

Mathew
Topic archived. No new replies allowed.