Ok, at least this gives me a ray of hope. So, to get rid of any interfering 
codelines, I did:

- Start Visual Studio
- New project (MFC Console, static linked MFC, Multibyte (unicode brings up 
the same result)

Full code:
#include <iostream>
#include <string>
#include "cryptlib.h"
#include "filters.h"
#include "eccrypto.h"
#include "files.h"
#include "sha.h"
#include "hex.h"
#include "pem.h"

int main()
{
    using namespace CryptoPP;

    std::string pt =
        "40BA49FCBA45C7EEB2261B1BE0EBC7C14D6484B9EF8A23B060EBE67F97252BBC"
        "987BA49DF364A0C9926F2B6DE1BAF46068A13A2C5C9812B2F3451F48B75719EE";

    HexDecoder decoder;
    decoder.Put((byte*)&pt[0], pt.size());
    decoder.MessageEnd();

    ECP::Point q;
    size_t len = decoder.MaxRetrievable();

    q.identity = false;
    q.x.Decode(decoder, len / 2);
    q.y.Decode(decoder, len / 2);

    ECDSA<ECP, SHA256>::PublicKey pkey;
    pkey.Initialize(ASN1::brainpoolP256r1(), q);

    FileSink fs("pubkey.pem");
    PEM_Save(fs, pkey);

    std::cout << "Hello World!\n";
}

But the result remains the same. OpenSSL:

C:\Program Files (x86)\OpenSSL-Win32\bin>openssl ec -in D:\pubkey.pem 
-inform PEM -pubin -text
read EC key
unable to load Key
22020:error:0908F066:PEM routines:get_header_and_data:bad end 
line:crypto\pem\pem_lib.c:812:

I really hope that I'm just stupid and don't see the obvious and your 
code/result speaks exactly for that. But I don't understand what exactly is 
going wrong here.
Jeffrey Walton schrieb am Mittwoch, 15. September 2021 um 19:43:23 UTC+2:

> On Wed, Sep 15, 2021 at 12:01 PM Jeffrey Walton <nolo...@gmail.com> wrote:
> >
> > > But my problem afterward (which I didn't mention to this point, 
> because I thought it will solve itself when I have a correct y-coordinate) 
> is, that if I try to save this public key as PEM, I only get
> > >
> > > -----BEGIN PUBLIC KEY-----
> > > MFowMFow-----END PUBLIC KEY-----
> > >
> > > as result. This was also my result with wrong y and failed validation.
>
> Here's what I am seeing with your data. It looks OK to me. I think we
> need to see your program to determine what is going sideways.
>
> $ cat test.cxx
> #include <iostream>
> #include <string>
>
> #include "cryptlib.h"
> #include "filters.h"
> #include "eccrypto.h"
> #include "osrng.h"
> #include "files.h"
> #include "sha.h"
> #include "hex.h"
>
> #include "pem.h"
>
> int main(int argc, char* argv[])
> {
> using namespace CryptoPP;
>
> std::string pt =
> "40BA49FCBA45C7EEB2261B1BE0EBC7C14D6484B9EF8A23B060EBE67F97252BBC"
> "987BA49DF364A0C9926F2B6DE1BAF46068A13A2C5C9812B2F3451F48B75719EE";
>
> HexDecoder decoder;
> decoder.Put((byte*)&pt[0], pt.size());
> decoder.MessageEnd();
>
> ECP::Point q;
> size_t len = decoder.MaxRetrievable();
>
> q.identity = false;
> q.x.Decode(decoder, len/2);
> q.y.Decode(decoder, len/2);
>
> ECDSA<ECP, SHA256>::PublicKey pkey;
> pkey.Initialize(ASN1::brainpoolP256r1(), q);
>
> FileSink fs("pubkey.pem");
> PEM_Save(fs, pkey);
>
> return 0;
> }
>
> And:
>
> $ ./test.exe
> $ cat pubkey.pem
> -----BEGIN PUBLIC KEY-----
> MFowFAYHKoZIzj0CAQYJKyQDAwIIAQEHA0IABEC6Sfy6RcfusiYbG+Drx8FNZIS5
> 74ojsGDr5n+XJSu8mHuknfNkoMmSbytt4br0YGihOixcmBKy80UfSLdXGe4=
> -----END PUBLIC KEY-----
>
> And: $ openssl ec -in pubkey.pem -inform PEM -pubin -text
> Public-Key: (256 bit)
> pub:
> 04:40:ba:49:fc:ba:45:c7:ee:b2:26:1b:1b:e0:eb:
> c7:c1:4d:64:84:b9:ef:8a:23:b0:60:eb:e6:7f:97:
> 25:2b:bc:98:7b:a4:9d:f3:64:a0:c9:92:6f:2b:6d:
> e1:ba:f4:60:68:a1:3a:2c:5c:98:12:b2:f3:45:1f:
> 48:b7:57:19:ee
> ASN1 OID: brainpoolP256r1
>
> Jeff
>

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/1371df35-09d7-4805-933f-78994f5629dan%40googlegroups.com.

Reply via email to