Unable to create SymmetricKey - c++

First of all version of Botan I'm using is Botan-1.10.9
And I'm writing a managed wrapper in Visual C++
Following this example, I'm trying to create a SymmetricKey from the hash of a string so I can pass it into the fe1_encrypt method of the FPE module
Signature of fe1_encrypt is
BigInt FPE::fe1_encrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector<byte> &tweak)
I want the value I pass into the key parameter to be hash of the plaintext (not possibility to decrypt) So really I don't care about it being a SymmetricKey, just need that type because the method requires it as a parameter.
But in their example they have passed the SymmetricKey their hash method that returns an std:vector
However there is no constructor for SymmetricKey that takes this type.
Anyone have any ideas?
EDIT: I tried this with no luck
std::vector<byte> re = SHA_1(plaintextAsString);
Botan::OctetString key(re, re.size());
ERROR
Error 15 error C2664: 'Botan::OctetString::OctetString(Botan::RandomNumberGenerator &,size_t)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'Botan::RandomNumberGenerator &'

In the documentation, SymmetricKey (which is just a typedef for OctetString) can take a byte array and length as constructor. Alternatively, you can encode the key as a hex string. If you already have the key as std::vector<byte>, then this should suffice:
std::vector<byte> keybytes;
// ...fill the vector...
SymmetricKey key( keybytes.data(), keybytes.size() );
Later versions of Botan define another constructor OctetString (const std::vector<byte> &in).

Related

Using rapidjson and ATL CString

I am attempting to use the rapidjson library with Microsoft ATL CString type, as shown in the example below.
#include "stdafx.h"
#include "rapidjson\document.h"
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
int main()
{
WDocument document;
CString hello = _T("Hello");
document.SetObject();
document.AddMember(_T("Hello"), hello, document.GetAllocator());
return 0;
}
This fails with the compiler error
'rapidjson::GenericValue::GenericValue(rapidjson::GenericValue &&)': cannot convert argument 1 from 'CString' to 'rapidjson::Type' rapidjson document.h 1020
which does imply that a conversion between CString and a format which rapidjson would need is required. I know that rapidjson internally uses wchar_t as the encoding for the UTF16 version of its functions, however I am not sure how to convert a CString to a wchar_t (or array of wchar_t) in a way that rapidjson will be able to use the string as it uses strings defined by the _T macro.
I have looked at the msdn resources on converting between string types here but this only gives a way to return a pointer to the first member of an array of wchar_t, which rapidjson cannot then use.
The correct way to do this is to use one of the constructors rapidjson provides for its GenericValue class, namely the constructor for a pointer to a character encoding type and a character length.
GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(StringRef(s, length)); }
This constructor can take a pointer to any of the character types which rapidjson accepts along with a length and then read this into a value. For the ATL::CString class, this can be accomplished with the .GetString() and .GetLength() methods available on a CString object. A function to return a Value which can be used in a DOM tree would look like this:
typedef GenericValue<UTF16<> > WValue;
WValue CStringToRapidjsonValue(CString in)
{
WValue out(in.GetString(), in.GetLength());
return out;
}

Passing an [out] array to C++ function

I have a C++\CLI managed class method that takes an out array. I want to pass this out array to the underlying C++ function that takes a vector< char >&. This C++ functions fills the array with values.
bool MyLib::GetBits([Out] array<unsigned char>^ %bits)
{
MyCppClass->GetBits(bits); // ????
// ERROR: C2664: cannot convert from 'cli::array<Type> ^' to 'std::vector<_Ty> &'
}
'GetBits' is declared as MyCppClass::GetBits(vector<char> &bits);
Have you any reason to expect that array<unsigned char>^ %bits can be converted to vector<char> &bits?
You can try to modify MyCppClass, adding a member that returns a reference to a static vector. In GetBits you can clear it, and iterate through bits adding the chars to it. You may also find Marshaling in C++ useful.

error C2664: 'callToPrint' : cannot convert parameter 1 from 'std::wstring' to 'LPTSTR'

I converted a narrow string to the wide string as follows :
string nameOfPrinter;
getline( cin , nameOfPrinter );
wstring WprinterName;
int number = MultiByteToWideChar( CP_UTF8 , 0 , nameOfPrinter.c_str() , nameOfPrinter.size() , &WprinterName , 0 );
// then i make a call to the function whose prototype is callToPrint( LPTSTR , LPVOID , DWORD , string )
// the calling statement is :
callToPrint( WprinterName , -----all other arguments-----,);
// But this call produces the following error error C2664: 'callToPrint' : cannot convert parameter 1 from 'std::wstring' to 'LPTSTR'
Why is it so ? and please tell me how can i fix it ?
You also need to use .c_str() here.
Also, I'd read the printer name directly into WprinterName, using
getline(wcin, Wprintername);
Your problem is that callToPrint basically states it expects a C string it can modify, i.e. not const. Hence the use if LPTSTR instead of LPTCSTR VC macro. Whether it in fact changes the buffer or not depends on its implementation. Now, w_string.c_str() returns a const wchar_t*, which according to the definition of c_str() you must not change (even if you can cast it to a wchar_t*, in which case your code will compile.
Since callToPrint is declared that way, you must provide it with a non-const C string. For that, you can drop the use of wstring WprinterName, and use a raw array of wchar_t (or TCHAR, if you want to stick to VC types). Use that buffer in MultiByteToWideChar and callToPrint, and don't forget to free it at the end...
If you do need the wstring for further processing, read: Convert std::string to const char* or char* for some additional suggestions.

How to create a CString from an array of chars?

Need to log the content of buf using the LogMethod() below the problem is that
LogMethos only accepts a "Const CString&"
char buf[1024];
strcpy(buf, cErrorMsg);
// need to pass to LogMethod "buf" how do i do that?
log.LogMethod(const CString &);
Thans
Rev
Reversed
If you're talking about MFC CString, as far as I can tell, it should have a non-explicit constructor taking TCHAR const *. In other words, the following should work.
log.LogMethod(buf);
If it doesn't, please post the error message.
log.LogMethod(CString(buf));
This will avoid the problem where the compiler won't automatically create the CString object using the appropriate constructor since the argument is a reference (It would have if the argument was a "plain" CString).
CString cs;
cs = buf;
log.LogMethod(cs)

C++ Variable Conversion

I need to pass one of my parameters to a write() function. It is asking for a type of 'const void*' I am a PHP guy and don't know C++ very well.
Here is my parameter:
const fmx::Text& distance = dataVect.AtAsText(3);
I don't know of any other way to pull in that field. I would love to just declare it const void* but I don't know how.
I guess just converting it would be easier than trying to pull it in the correct way??
The error message: cannot convert const fmx::Text to const void* for argument 2
write(fd, distance, 4);
I know this worked so can I just convert?
const void* s = "5000";
This is for a plugin in FileMaker so I don't really get c++ here.
Is there more anyone would need to help me solve this??
Thanks so much!
If fmx::Text was a pointer type, the compiler would automatically convert a reference to it into a void*. You need to use the address-of operator to give the function a pointer to work with:
write(fd, &distance, 4);
I don't really know filemaker, but this link shows that fmx::Text has a GetBytes function. You can then pass the pointer to the buffer filled with this function.
I'm assuming you actually want the text string.
I think you need to check the api for fmx::Text to get the string you want. Here is something I found to get the string out.
Looks like the type stores the data as UTF16, so you have to run a bit of code to get a string out, then pass it to your write function:
//a function to convert to a normal string
std::string getString(fmx::Text& Text)
{
char buffer[512] = {0}; //NOTE YOU HAVE A STRING SIZE LIMIT
// convert original text to ASCII text
outText.GetBytes( buffer, sizeof(buffer)-1, 0, Text.GetSize(), fmx::Text::kEncoding_Native );
return buffer;
}
Then call the function
std::string myString = getString(distance);
write(fd, myString.c_str(), myString.size());
Note I'm assuming a lot here...that you want a string in the current encoding, and not the raw UTF16 data from 'distance'. AND that GetBytes will not mangle the null characters in buffer....
You'll also need to include <string> in your c++ file.