0

I'm using CRijndael ( http://www.codeproject.com/Articles/1380/A-C-Implementation-of-the-Rijndael-Encryption-Decr ) for encryption using a null based iv (I know that's an issue but for certain reasons I'm stuck with having to use that).

For strings that are longer (or contain a few ampersands) I'm only ever getting the first 32 bytes encrypted. Shorter strings are encrypted without any issues. Code is below, any ideas?

    char dataIn[] = "LONG STRING HERE";
    string preInput = dataIn;


    CRijndael aRijndael;
    aRijndael.MakeKey("32-BIT-KEY-HERE", CRijndael::sm_chain0, 32, 16);



    while (preInput.length() % 16 != 0) {
        preInput += '\0';
    }
    const char *encInput = preInput.c_str();
    char szReq[1000];
    aRijndael.Encrypt(preInput.c_str(), szReq, preInput.size(), CRijndael::CBC);

    const std::string preBase64 = szReq;
    std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(preBase64.c_str()), preBase64.length());
avalore
  • 103
  • 1
  • 9
  • 1
    `const std::string preBase64 = szReq;` This stops at the first 0 byte `szReq` may contain. Apparently, in your case it contains one near the beginning of the buffer. – Igor Tandetnik Nov 24 '14 at 17:40
  • Aha, thanks for pointing that out. How should I get szReq into a string or ideally `const unsigned char*` so I can base64 encode it? – avalore Nov 24 '14 at 18:00
  • `const unsigned char* p = (const unsigned char*)szReq;` – Igor Tandetnik Nov 24 '14 at 18:02
  • Thanks, it's a lot better already. It's mostly encrypted apart from the very end (the last couple of characters are gibberish when decrypted). Possibly to do with the length in base64_encode? – avalore Nov 24 '14 at 18:37
  • Possibly. Lacking mind-reading abilities or a working crystal ball, I find it rather challenging to comment on code you have not shown. – Igor Tandetnik Nov 24 '14 at 18:50
  • Thank you so much for the insight! [base64_encode](http://www.adp-gmbh.ch/cpp/common/base64.html) - `base64_encode(unsignedSzReq, preInput.length());` – avalore Nov 24 '14 at 19:33
  • Nah, those last few bytes are just the stuff in memory straight behind the plaintext. You will probably need to do the padding up to the block size yourself. You shouldn't use source code that isn't maintained or changed since 2002, and only has a single person editing it (if that). – Maarten Bodewes Nov 24 '14 at 20:06

0 Answers0