Stuck loading Private Key from Byte Array

964 views
Skip to first unread message

TkD08

unread,
Jun 14, 2011, 4:26:52 PM6/14/11
to cryptop...@googlegroups.com

Hi all,

So I've looked around enough, and it seems others have had almost the same
issue, but has been solved differently.

Anyway on to what I'm trying to accomplish: I have a private key being sent
over a socket by a Java Server to decrypt a string, and a Qt/C++ client that
is trying to accept this key. While I was able to do so by sending the key
to a file first, and loading it as such:

CryptoPP::RSA::PrivateKey privKey;

CryptoPP::ByteQueue queue;
CryptoPP::FileSource file(filename.c_str(), true /*pumpAll*/);
file.TransferTo(queue);
queue.MessageEnd();
privKey.Load(queue);

I was not able to reproduce the same results using a byte array, I've tried
a couple ways:

//----------------------------- 1) -----------------------------------
QByteArray keyData = keySocket.readAll(); // reads all available data from
the socket (double checked)
char *data = keyData.data(); // returns a pointer to a '/0' terminated
string

CryptoPP::ByteQueue queue;
CryptoPP::StringSource keySource(data, true);

keySource.TransferTo(queue);
queue.MessageEnd();
privKey.Load(queue);

//------------------------------- 2) ----------------------------------
QByteArray keyData = keySocket.readAll(); // reads all available data from
the socket (double checked)
std::string dataString = QString(keyData).toStdString(); // creates a std
string from the byte array

CryptoPP::StringSource keySource(dataString, true);

privKey.Load(keySource);

Both times I end up with a "BER decode error" exception when trying to load
the key.

Am I just doing this completely wrong? I know there must be something
missing, but I've been going crazy trying so many ways!

Please help! It would be much appreciated!

--
View this message in context: http://old.nabble.com/Stuck-loading-Private-Key-from-Byte-Array-tp31846337p31846337.html
Sent from the Crypto++ Users mailing list archive at Nabble.com.

TkD08

unread,
Jun 15, 2011, 5:19:14 PM6/15/11
to cryptop...@googlegroups.com

Got a response, thought I'd post it here for everyone else!

privKey.Load(CryptoPP::StringStore((const
byte*)data,(size_t)dataLen).Ref());

That's it, that's all!

data is a char * containing the bytes of the key, and dataLen as you can
probably guess is a const int with the length of data!

Thanks Da Co!

--
View this message in context: http://old.nabble.com/Stuck-loading-Private-Key-from-Byte-Array-tp31846337p31855303.html

Amin

unread,
Aug 25, 2011, 2:59:13 AM8/25/11
to cryptop...@googlegroups.com
Hi,

Can u elaborate a bit more. I have exactly the same problem but in C++.

here is the link to my question. Your help would be appreciated.

Amin

unread,
Aug 25, 2011, 6:32:17 AM8/25/11
to cryptop...@googlegroups.com
I'm using this to save the public key in an Array but still got BER decoding error on Loading.

publicKey.Save((CryptoPP::ArraySink(pukArray,57)).Ref());
Reply all
Reply to author
Forward
0 new messages