Re: Get ASCII value for character when higher than 127
ssetz@wxs.nl wrote:
On 29 mei, 18:04, Alex Blekhman <x...@oohay.moc> wrote:
I would prefer using std::vector< char > instead of std::string to do
the XOR crypt [...].
Actually, `std::valarray<char>' would be even better:
std::valarray<char> v1;
std::valarray<char> v2;
...
v1 ^= v2;
Mmm, oke, you guys are totally losing me now. Please remember I know
hardly anything about C++.
Should I replace my complete XOR function with this code? Should I
change the string parameters to a std::valarray and then just do an
XOR with both arrays? Leave out my for loop? Or should I just assign
value[v] and key[k] to those valarray's before doing the XOR
assignment?
Sorry for not better understanding what you mean :-(
Sorry for confusing you. It's just another way to skin a
cat. Your C++ code looks fine. The `std::valarray' container
is useful when you need to perform some operation on every
element in container. The `std::valarray' has convenient
operators to do this, so you don't need to write tedious
loops by yourself. However, when both valarrays participate
in an operation, then both of them must have the same length.
So, suppose you want to XOR all elements with some value.
Then instead of writing error prone loop you can just write:
valarray<char> v1("Hello", 5);
v1 ^= 42; // v1[i] ^= 42, where i = {1...5}
Considering your problem, I tried following code and it
works well for me:
----- C++ -----
string GetPwdFilePath()
{
const char* timeStringFormat = "%Y-%m-%d_%H-%M-%S";
const size_t timeStringLength = 20;
char timeString[timeStringLength] = { 0 };
time_t t = time(0);
tm *curTime = localtime(&t);
strftime(timeString, timeStringLength,
timeStringFormat, curTime);
string path("C:\\Temp\\");
path.append(timeString);
path.append(".txt");
return path;
}
string ObfuscateUnicodeString(LPCWSTR pwszIn, USHORT nLen)
{
valarray<WCHAR> v(pwszIn, nLen);
v ^= 42;
ostringstream oss;
oss << right;
for(size_t i = 0; i < v.size(); ++i)
{
oss << setw(4) << setfill('0') << v[i] << '-';
}
return oss.str();
}
void WriteXmlMessage(
const string& path,
const string& username,
const string& password
)
{
ofstream outPwd(path.c_str());
outPwd << "<userpwd><username>" << username
<< "</username><password>" << password
<< "</password></userpwd>";
outPwd.close();
}
NTSTATUS
NTAPI
PasswordChangeNotify(
PUNICODE_STRING UserName,
ULONG /*RelativeId*/,
PUNICODE_STRING Password
)
{
const string& username = ObfuscateUnicodeString(
UserName->Buffer, UserName->Length);
const string& password = ObfuscateUnicodeString(
Password->Buffer, Password->Length);
const string& path = GetPwdFilePath();
WriteXmlMessage(path, username, password);
return STATUS_SUCCESS;
}
int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
WCHAR wszUsername[] = L"Alex";
WCHAR wszPassword[] = L"P" L"\xE2" L"ssw" L"\xF5" L"rd";
UNICODE_STRING UserName = { 5, 10, wszUsername };
UNICODE_STRING Password = { 8, 10, wszPassword };
PasswordChangeNotify(&UserName, 0L, &Password);
return 0;
}
----- C++ -----
----- C# -----
static string RestoreUnicodeString(string sIn)
{
char[] cDelim = { '-' };
string[] arrOut = sIn.Split(cDelim,
StringSplitOptions.RemoveEmptyEntries);
string sOut = string.Empty;
foreach (string s in arrOut)
{
int nCh = System.Convert.ToInt32(s) ^ 42;
sOut += System.Convert.ToChar(nCh);
}
return sOut;
}
static void ReadXmlFile()
{
const string sFilepath =
"C:\\Temp\\2007-05-30_16-41-29.txt";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sFilepath);
string sUsername =
xmlDoc.SelectSingleNode("/userpwd/username").FirstChild.Value;
string sPassword =
xmlDoc.SelectSingleNode("/userpwd/password").FirstChild.Value;
Console.WriteLine(RestoreUnicodeString(sUsername));
Console.WriteLine(RestoreUnicodeString(sPassword));
}
----- C# -----
Alex