Just and FYI, I found the issue: string messageData = (char *)in; this was taking in the null terminating string, so I implemented Encrypt without using string intermediates. Works like a charm now!
On Jan 19, 3:40 pm, ZBrown <[email protected]> wrote: > First, thanks for the response. I implemented it without using the > intermediate string, but it came back with the same results [gibberish > after first 0x00]. I am using this simply as an interface to GCM > encryption/decryption, and thus I have valid pointers [along with > their validation] accounted for. In my test I have [simplified] > > int main(int argc, char *argv[]) > { > TEXT_SIZE = 256; // technically a passed in argument, but for > simplicity > byte key[32]; > byte iv[12]; > byte plainText[TEXT_SIZE]; > byte *encoded, *decoded; > int size, status; > > // assignments (char_to_byte, is verified to be working turning hex > as text to hex values) > iv = char_to_byte("000101353334331718192021", 12); > key = > char_to_byte("81cf7b4b4cca0330e1ef876e8cdc5beef20da8ba2b075a4b6f5748fed2859 > f2f", > 32); > > memset(plainText, 0x61, TEXT_SIZE); > memset((plainText+20), 0x00, 1); /// example 0x00 entry > > encoded = (byte*)malloc(TEXT_SIZE+4); > memset(encoded, 0x00, TEXT_SIZE+4); > decoded = (byte*)malloc(TEXT_SIZE); > memset(decoded, 0x00, TEXT_SIZE); > > // print data ... > > // ENCRYPTION > size = TEXT_SIZE; > status = encrypt(iv, plainText, &size, encoded); > if (status = FAILURE) > { > ... // print failure, exit > } > // print encoded ... > > status = decrypt(iv, encoded, &size, decoded); > if (status = FAILURE) > { > ... // print failure, exit > } > // print decoded ... > // the decoded values are correct up to the point of the first > 0x00 instance, after that it seems like gibberish > > } > > On Jan 19, 2:08 pm, Rafael Vargas <[email protected]> wrote: > > > > > > > > > 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.
