1

I am trying to implement Diffie–Hellman key exchange in C++ based on Crypto++ library (https://www.cryptopp.com/wiki/Diffie-Hellman).

The other side of the key agreement can be implemented in any language such as Java. Base64 is the common format to both sides.

This is my code snippet:

    DH dh;  
    dh.AccessGroupParameters().Initialize(p, g);


    SecByteBlock privKey(dh.PrivateKeyLength());
    SecByteBlock pubKey(dh.PublicKeyLength());
    dh.GenerateKeyPair(rnd, privKey, pubKey);

How can I encode the public key to Base64?

jps
  • 20,041
  • 15
  • 75
  • 79
  • I don't see what you tried to base64 encode the data. Isn't that simply a function call that you need to encode the data? Also, if the question is about to base64 encode something, most of the tags don't seem to be relevant for the question. – jps Jun 10 '20 at 08:19
  • Hi @jps! I am trying to encode the "pubKey" object that is a "SecByteBlock". – victormaehira Jun 10 '20 at 12:49
  • Sorry, didn't see that. Then [this](https://stackoverflow.com/questions/23095254/value-after-cryptopp-base64-encoding-decoding-not-the-same) might help, I hope. Always try to find the answer first and if you can't find it, make clear what's special in your question. – jps Jun 10 '20 at 21:09
  • I came up with thies solution below, however it is still not compatible with the key exchange. Maybe the problem resides in the key creation. string base64Key; Base64Encoder b(new StringSink(base64Key)); b.Put(pubKey.data(), pubKey.size()); b.MessageEnd(); – victormaehira Jun 21 '20 at 19:21
  • All the base64 public keys generated in Java start with "MII", while in C++ do not. Maybe, the "raw" public key in C++ should be converted into DER or another format previously. – victormaehira Jun 21 '20 at 20:43

0 Answers0