[Botan-devel] DH public key generation

rippel tippel rippeltippel at gmail.com
Tue Oct 7 11:47:05 EDT 2008

Thanks for the quick reply.

All you need to do is any of the following to get the public key:
> DH_PublicKey my_public_key = my_private_key;
> std::string my_public_key_str = X509::PEM_encode(my_private_key);
> SecureVector<byte> my_public_key_bits = my_private_key.public_value();

That doesn't seem to work, but I may be wrong. Here's my code:

int main(int argc, char *argv[])
        LibraryInitializer init;

        DH_PublicKey public_server;
        SymmetricKey shared_key;

        // Generate private and public keys
        DH_PrivateKey private_client(DL_Group("modp/ietf/1024"));
        DH_PublicKey public_client = private_client;

        // Print the keys
        cout << "-------------------------------" << endl;
    catch (Exception &e)
        cout << "Exception " << e.what() << endl;
        return -1;

    return 0;

void print_key(DH_PublicKey &k)
    byte *buffer;

    buffer = (byte*)malloc(sizeof(byte) * 1024);
    memcpy(buffer, k.public_value(), sizeof(byte) * 1024);

    for (unsigned int i = 0; i < sizeof(int) * 1024; ++i)
        printf("%02x", (int)buffer[i]);


When I print the two keys, they are exactly the same.
I also tried to use two separate print_key() functions, one for
DH_PublickKey and another one for DH_PrivateKey, but the result is still the
If the print_key() function is correct, in a DH key exchange Alice would
send its private key to Bob! So I guess I am missing something...

Another problem that I have with DH key exchange is that, when Bob receives
Alice's (public?) key, I have a segfault.
I am sending the key using an UDP socket:

DH_PrivateKey alice_private(DL_Group("modp/ietf/1024"));
DH_PublicKey alice_public = alice_private;
sendto(skt, (DH_PublicKey*)&alice_public, sizeof(DH_PublicKey), 0, (struct
sockaddr*)&udp_bob_addr, len);

recvfrom(skt, (DH_PublicKey*)&alice_public, sizeof(DH_PublicKey), 0, (struct
sockaddr*)&udp_alice_addr, (socklen_t*)&len);
DH_PrivateKey bob_private(alice_public.get_domain());   // <-- SEGMENTATION
DH_PublicKey bob_public = bob_private;

What's wrong with that?

Thank you,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.randombit.net/pipermail/botan-devel/attachments/20081007/e9c73599/attachment.html>

More information about the botan-devel mailing list