[prev in list] [next in list] [prev in thread] [next in thread]
List: botan-devel
Subject: Re: [botan-devel] El Gamal keys import problem
From: "Giacomo D'Amico" <speed1625 () gmail ! com>
Date: 2012-09-19 17:49:58
Message-ID: CAH49JJPksVm=b4=eBjtGecg8BLQDx5xUkOqjU+EiuV823f2=Xg () mail ! gmail ! com
[Download RAW message or body]
Thanks very much!! But I have another problem....."Exception caught:
Botan: Decoding error: X.509 public key decoding failed".
The modified code is:
#include <string>
#include <botan/botan.h>
#include <botan/elgamal.h>
#include <botan/look_pk.h>
using namespace Botan;
#include <fstream>
#include <iostream>
#include <memory>
std::ofstream pub("elgpub.pem");
std::ofstream priv("elgpriv.pem");
void start();
void load();
int main(int argc, char* argv[])
{
Botan::LibraryInitializer init;
#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,4)
Botan::global_state().set("oid2str", "1.3.6.1.4.1.3029.1.2.1", "ElGamal");
Botan::global_state().set("str2oid", "ElGamal", "1.3.6.1.4.1.3029.1.2.1");
#endif
AutoSeeded_RNG rng;
start();
load();
return 0;
}
void start() {
try
{
AutoSeeded_RNG rng;
DL_Group gruppo("modp/ietf/1024");
// generate a 2048 bit ElGamal key
ElGamal_PrivateKey key(rng, gruppo);
pub << X509::PEM_encode(key);
priv << PKCS8::PEM_encode(key);
pub.flush();
priv.flush();
}
catch(std::exception& e)
{
std::cout << "Exception caught: " << e.what() << std::endl;
}
}
void load()
{
try
{
DataSource_Stream pubb("elgpub.pem");
X509_PublicKey* pubkey = X509::load_key(pubb);
}
catch(std::exception& e)
{
std::cout << "Exception caught: " << e.what() << std::endl;
}
}
Thanks a lot!
2012/9/19 Jack Lloyd <lloyd@randombit.net>:
>
> Likely this is caused by the source being empty.
>
> You write to elgpub.pem but the ofstream was not closed or flushed, so
> the contents are just waiting in an buffer rather than going to disk.
> So when you later try to reread the file, it exists but has no
> contents. Probably if you add something like `pub.flush()` after you
> write to the stream the problem would go away because the key will
> have made it to the filesystem.
>
> -Jack
>
> On Wed, Sep 19, 2012 at 07:22:01PM +0200, Giacomo D'Amico wrote:
>> I have a problem with El Gamal key import (botan release 1.8.13). I
>> use the code below and the result is "Exception caught: Botan: I/O
>> error: ASN1::maybe_BER: Source was empty", would someone help me?
>> Thanks a lot.
>>
>> #include <string>
>> #include <botan/botan.h>
>> #include <botan/elgamal.h>
>> #include <botan/look_pk.h>
>>
>> using namespace Botan;
>>
>> #include <fstream>
>> #include <iostream>
>> #include <memory>
>>
>> std::ofstream pub("elgpub.pem");
>> std::ofstream priv("elgpriv.pem");
>>
>> void start();
>> void load();
>>
>> int main(int argc, char* argv[])
>> {
>> Botan::LibraryInitializer init;
>> #if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,9,4)
>> Botan::global_state().set("oid2str", "1.3.6.1.4.1.3029.1.2.1", "ElGamal");
>> Botan::global_state().set("str2oid", "ElGamal", "1.3.6.1.4.1.3029.1.2.1");
>> #endif
>> AutoSeeded_RNG rng;
>> start();
>> load();
>> return 0;
>> }
>>
>> void start() {
>> try
>> {
>> AutoSeeded_RNG rng;
>> DL_Group gruppo("modp/ietf/1024");
>>
>> // generate a 2048 bit ElGamal key
>>
>> ElGamal_PrivateKey key(rng, gruppo);
>> pub << X509::PEM_encode(key);
>> priv << PKCS8::PEM_encode(key);
>> }
>> catch(std::exception& e)
>> {
>> std::cout << "Exception caught: " << e.what() << std::endl;
>> }
>> }
>>
>> void load()
>> {
>> try
>> {
>> DataSource_Stream pubb("elgpub.pem");
>> X509_PublicKey* pubkey = X509::load_key(pubb);
>> }
>> catch(std::exception& e)
>> {
>> std::cout << "Exception caught: " << e.what() << std::endl;
>> }
>>
>> }
>> _______________________________________________
>> botan-devel mailing list
>> botan-devel@randombit.net
>> http://lists.randombit.net/mailman/listinfo/botan-devel
> _______________________________________________
> botan-devel mailing list
> botan-devel@randombit.net
> http://lists.randombit.net/mailman/listinfo/botan-devel
_______________________________________________
botan-devel mailing list
botan-devel@randombit.net
http://lists.randombit.net/mailman/listinfo/botan-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic