benhj / CryptoStreamPP

Encrypted, seekable, file streams for C++
28 stars 7 forks source link

CryptoStreamPP() alwaysInitKey parameter usage #3

Open vassilisw opened 4 years ago

vassilisw commented 4 years ago

Hello, I was trying to decode an already encoded stream. I was not able to decode the stream correctly until after I instantiated the stream with alwaysInitKey(true). Is this the right (intended) use of this parameter?

Using your example something like this...

#include "cryptostreampp/Algorithms.hpp"
#include "cryptostreampp/CryptoStreamPP.hpp"
#include "cryptostreampp/RandomNumberGenerator.hpp"
#include <iosfwd>
#include <iostream>

int main() {
    using namespace cryptostreampp;
    EncryptionProperties props;

    props.iv  = 6006631236506852901;  
    props.iv2 = 1573596508630574202;
    props.iv3 = 4389739208682704398;
    props.iv4 = 2119482576824948873;
    props.cipher = Algorithm::AES;
    props.password = std::string("password");

    // Create a stream in output mode to create a brand new file called test.txt
    CryptoStreamPP stream("test.txt", props, std::ios::out | std::ios::binary | std::ios::trunc);
    stream.write("Hello, world!", 13);
    stream.flush();
    stream.close();

    // *** Constructing with default value (false) of alwaysInitKey not working! ***
    // CryptoStreamPP stream1("test.txt", props, std::ios::out | std::ios::binary);
       CryptoStreamPP stream1("test.txt", props, true, std::ios::out | std::ios::binary);

    // Read in a buffer of data
    {
        char buffer[14];
        stream1.read(buffer, 13);
        buffer[13] = '\0';

        std::cout << buffer << std::endl;
    }

    stream1.close();
    return 0;

}
benhj commented 3 years ago

Sorry for the late reply. Assuming this is still of interest, I’ll look into this and get back to you. Cheers.

benhj commented 3 years ago

Yep, I think there is a bug in how the stream is being constructed. My fault since I’m doing the same thing in example.cpp. Looks like the open mode is being interpreted as a Boolean here, since the third constructor argument is always interpreted as a Boolean. I think this could be refactored slightly and certainly fixed.