This probably has to do with the fact that you are using std::string as a
intermediate container, but maybe this would work (not actually tried to
compile and run it)

int encrypt(byte iv[12], const byte *in, int *size, byte *out)
{
  try
  {
     GCM<AES>::Encryption e;
     e.SetKeyWithIv(key, 32, iv, 12);

     AuthenticatedEncryptionFilter ef(e, new ArraySink(out, *size -
4),false, 4);
     ef.ChannelPut("", in, *size);
     ef.ChannelMessageEnd("");
     *size -= 4;
  }
  catch
  {
     ...  // print exception; return failure
  }
  return (1); // success
}

As you can see, I've removed the intermediary containers. As the
std::string constructor was probably not even looking for anything after
the first 0x00. But this functions assumes that all pointers are valid and
that the out pointer was allocated with enough space. You should probably
be using ByteQueue and ArraySource/ArraySink, and SecByteBlock...

Hope this helps...


-- 
Rafael Vargas


2012/1/19 ZBrown <[email protected]>

> I am having an issue in which I encrypt my data, and it seems to be
> working; however, when I attempt to decrypt the data it returns
> gibberish after a 0x00 value.  For instance I *send in a byte[256]
> that contains all 0x61, with several 0x00* interjected.  I am doing
> this as a proof of concept, being that I would like to use the GCM
> model to encrypt data that may contain 0x00.  I have verified my key,
> iv, and const byte *in (including mac) and all seems to go threw
> encrypt to decrypt correctly.
>
> The issue I have is that the data after any 0x00 is gibberish from the
> statement:
>    if (n > 0) { df.Get(out, n); }
>
> Is this not supported?  Am I implementing it incorrectly for this type
> of execution?
>
> My Encryption:
>
> int encrypt(byte iv[12], const byte *in, int *size, byte *out)
> {
>   string messageData = (char*)in;
>   string tempOut;
>   try
>   {
>      GCM<AES>::Encryption e;
>      e.SetKeyWithIv(key, 32, iv, 12);
>
>      AuthenticatedEncryptionFilter ef(e, new StringSink(tempOut),
> false, 4);
>      ef.ChannelPut("", (const byte*)messageData.data(), *size);
>      ef.ChannelMessageEnd("");
>      memcpy(out, tempOut.data(), *size - 4);
>      *size -= 4;
>   }
>   catch
>   {
>       ...  // print exception; return failure
>   }
>
>   return (1); // success
> }
>
> My Decryption:
>
> int decrypt(byte iv[12], const byte *in, int *size, byte *out)
> {
>   string ciper = (char*)in;
>   try
>   {
>      GCM<AES>::Decryption d;
>      d.SetKeyWithIV(key, 32, iv, 12);
>
>      string enc = cipher.substr(0, *size - 4);
>      string mac = cipher.substr(*size - 4);
>
>      AuthenticatedDecryptionFilter df(d, NULL,
>          AuthenticatedDecryptionFilter::MAC_AT_END ||
>          AuthenticatedDecryptionFilter::THROW_EXCEPTION, 4);
>      df.ChannelPut("", (const byte*)enc.data(), enc.size());
>      df.ChannelPut("", (const byte*)mac.data(), mac.size());
>      df.ChannelMessageEnd("");
>
>      bool b = false;
>      b = df.GetLastResult();
>      assert(true == b);
>
>      size_t n = (size_t)-1;
>      df.SetRetrievalChannel("");
>      n = (size_t)df.MaxRetrievable();
>
>      if (n > 0) { df.Get(out, n); }
>   }
>   catch
>   {
>      ...  // print exception; return failure
>   }
>
>   return(1); // success
> }
>
>
> --
> You received this message because you are subscribed to the "Crypto++
> Users" Google Group.
> To unsubscribe, send an email to
> [email protected].
> More information about Crypto++ and this group is available at
> http://www.cryptopp.com.
>

-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.

Reply via email to