Ok for later reference, the post :

[How to use efficiently]
http://www.mail-archive.com/[email protected]/msg01619.html

is completely useless. The solution that worked for me was:

void Base64ToInteger(const char *base64String, Integer &xmlint)
{
  StringSource ss( base64String, true, new Base64Decoder);
  SecByteBlock result(ss.MaxRetrievable());
  ss.Get(result, result.size());
  xmlint = Integer(result, result.size());
}


Mathieu
Ps: I could not get this one to work:

  std::string decode;
StringSource mod_s( base64String, true, new Base64Decoder(new StringSink(decode)));


Wei Dai wrote:
The reason that Integer constructor takes a byte pointer and a length parameter is because an Integer encoded as a byte array can have 0 bytes in the middle, and can't be treated as a zero terminated C string. Then you went ahead and used strlen() on it anyway. Sigh...

----- Original Message ----- From: "Mathieu Malaterre" <>
To: <[email protected]>
Sent: Wednesday, September 13, 2006 9:54 PM
Subject: CryptoMaterial: this object contains invalid values


Wei Dai wrote:
There is indeed an error. Try using the Integer constructor that takes a byte * and a length (i.e., a byte array) as inputs, instead of char * (i.e., zero-terminated C string).


Wei,

Indeed you were right I have now moved on to the next step. I believe the encoding of the base64 into Integer is working correctly. But now I am getting a new error message:

terminate called after throwing an instance of 'CryptoPP::CryptoMaterial::InvalidMaterial'
  what():  CryptoMaterial: this object contains invalid values


Using the following:

#include "modes.h"
#include "rsa.h"
#include "base64.h"
#include "filters.h"
#include "osrng.h"
#include "hex.h"
#include "files.h"
#include "cryptlib.h"
#include "validate.h"

using namespace CryptoPP;

void Base64ToInteger(const char *base64String, Integer &xmlint)
{
  StringSource mod_s( base64String, true, new Base64Decoder);
  unsigned long mrs = mod_s.MaxRetrievable();
  char* mod_sstr = new char[mrs];
  mod_s.Get( (unsigned char*)mod_sstr, mrs );
  xmlint = Integer((byte*)mod_sstr, strlen(mod_sstr));
  delete[] mod_sstr;
}

int main(int, char *[])
{
  // Construct Key from Modulus and Exponent:
const char modulus[] = "wehzXu32ipNZZkhWqYMVrKoWs4o5AfPKZbdTLVVt8jPfC09yKzC/ajJTeGm87moeU+yZ6Lz8PXsKOmxxNFBE3vQpbMEq++fCjhI0QdC4q/h0H6FePN7MC3WZmR0hpI3yp5sGNfBSBMPNRsnpEGI00ByxQp1N8R9hj9pc0OvwswE=";
  const char exponent[] = "AQAB";
  Integer mod, exp;
  Base64ToInteger(modulus, mod);
  Base64ToInteger(exponent, exp);

  RSA::PublicKey pubkey;
  pubkey.Initialize(mod, exp);

  const char message[] = "hello world";
  RSAES_OAEP_SHA_Encryptor pub(pubkey);
  AutoSeededRandomPool randPool;

  std::string result;
  StringSource(message, true, new PK_EncryptorFilter(randPool, pub,
      new Base64Encoder(new StringSink(result))));

  std::cerr << "Result: " << result << std::endl;

  return 0;
}





Reply via email to