I have been trying to wrap my head around how to use the CryptUnprotectData function in c++, but I can seem to get it correct. As of now, the function doesn't seem to be doing anything (the value returned is the value given. When I use GetLastError(), I get an error code of 87 (invalid parameter).
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string data = ("data I want to decode");
cout << "Encrypted data: " << data << endl;
DATA_BLOB DataOut;
BYTE* pbDataOutput = (BYTE*)data.data();
DataOut.pbData = pbDataOutput;
DataOut.cbData = strlen((char*)pbDataOutput);
DATA_BLOB DataVerify;
LPWSTR pDescrOut = NULL;
if (CryptUnprotectData(&DataOut, NULL, NULL, NULL, NULL,0,&DataVerify)){
cout << "decrypted data: " << DataVerify.pbData << endl;
LocalFree(DataVerify.pbData);
}
else{
cout << "Decryption error: ";
cout << GetLastError() << endl;
}
return 0;
}
I have tried copying the code from the documentation here and whittling it down until it just decrypts a string, but I get the same error.
Related
I am trying to just get user name. i.e., it's equivalent to whoami in ubuntu machine. But I am unable to get. I have tried following snippets.
method-1:
std::string get_username() {
struct passwd *pwd = getpwuid(getuid());
if (pwd)
return pwd->pw_name;
else
return "(?)";
}
method-2:
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
cout << "Name is : " << text;
pclose(name);
cout << endl;
return 0;
}
method-3:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << getenv("USER") << endl;
cout << getenv("HOME") << endl;
return 0;
}
The all methods are returning value as I expected. But, When I integrate this code into my Cmake project, it always returns root. I am confused why I am always getting root as response when I try with Cmake.
How to get right value instead of root?
I am trying to call the CreateProcessW() function to call dos command icacls in my C++ code . However this CreateProcessW() is not responding correctly to icacls command . I can say this because for negative testing i passed wrong icacls command to CreateProcessW() function an it just didnt throw any error.
However same command was failing in command prompt
I am attaching the code here
#include <vector>
#include <algorithm>
#include <atomic>
#include <memory>
#include <type_traits>
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
std::wstring command_line(L"icacls XXXCCCCC");//throws error with command prompt
STARTUPINFOW startup_info;
PROCESS_INFORMATION process_info;
size_t len(command_line.size());
std::vector<wchar_t> buff(len + 1);
std::copy(command_line.begin(), command_line.end(), buff.begin());
buff[len] = 0;
GetStartupInfoW(&startup_info);
BOOL result(CreateProcessW(nullptr, &buff[0], 0, 0, false, CREATE_NO_WINDOW, 0, 0,
&startup_info, &process_info));
if (!result)
{
std::cout << "FAIL" << std::endl;
std::wcout << " command line: <" << command_line << ">" << std::endl;
}
else
{
std::cout << "PASS" << std::endl;
std::wcout << " command line: <" << command_line << ">" << std::endl;
}
return 0;
}
I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.
My program checks for uppercase letters in German language.
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/locale.hpp>
using namespace std;
int main()
{
locale::global(locale("Germany_german"));
//locale::global(locale("de_DE.UTF-8")); //Also tried "de_DE.UTF-8", but does not work
string str1 = "über";
cout << boolalpha << any_of(str1.begin(), str1.end(), boost::algorithm::is_upper()) << endl;
string str2 = "Ää";
cout << boolalpha << any_of(str2.begin(), str2.end(), boost::algorithm::is_upper()) << endl;
return 0;
}
program crashes with error on console
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
I don't know what that exact locale string is, "de_DE.UTF-8" doesn't work as well.
Is there any way I can get exact locale name strings for all locales supported by OS. May be there is a list somewhere in header files, but I don't see anything <locale> header.
I wrote a program to print all supported locale names.
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ostream>
#include <iterator>
using namespace std;
vector<wstring> locals;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
locals.push_back(pStr);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
for (vector<wstring>::const_iterator str = locals.begin(); str != locals.end(); ++str)
wcout << *str << endl;
wcout << "Total " << locals.size() << " locals found." << endl;
return 0;
}
Works great.
...
de
de-AT
de-CH
de-DE
de-DE_phoneb
de-LI
de-LU
...
Total 429 locals found.
#user1 The following might do the same as your elegant code. I can't test it because of the C1189 compiler error.
#include <Winnls.h>
#include <iostream>
#include <ostream>
using namespace std;
int size = 0;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam) {
size++;
wcout << *pStr << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
wcout << "Total " << size << " locales found." << endl;
return 0;
}
This is my code:
#include <Windows.h>
#include <ShlObj.h>
#include <iostream>
using namespace std;
int main()
{
LPTSTR myPath = NULL;
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
if(myPath != NULL)
cout << "It returns something" << endl;
else
cout << "It returns nothing" << endl;
system("PAUSE");
return 0;
}
But myPath returns nothing. I just want to obtain the Desktop path. I'm on Windows 7 64 bits.
You need to give it room to put the data into:
T_CHAR myPath[ MAX_PATH ];
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);