Thread: CryptGenRandom

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    78

    CryptGenRandom

    This is a part of my program:

    Code:
    long long int expand(long long int part)
    {
       HCRYPTPROV hCryptProv = NULL;
       LPCWSTR UserName = L"My";
       BYTE add[8] = {0};
    
       if(!(CryptAcquireContextW(&hCryptProv, UserName, 0, PROV_RSA_FULL, 0)))
       {
       CryptAcquireContextW(&hCryptProv, UserName, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET);
       }
    
       CryptGenRandom(hCryptProv, 4, add);
    
       part |= ((long long int)add[0] << 56) + ((long long int)add[1] << 48) + ((long long int)add[2] << 40) + ((long long int)add[3] << 32);
    
       CryptReleaseContext(hCryptProv, 0);
    
       return part;
    }
    
    
    
    int main()
    {
        long long int my = expand(100);   //100 example number
        printf("%lli\n", my);
    
        printf("%lli\n", expand(100));   //100 example number
    
        fflush(NULL);
        getchar();
        return 0;
    }

    There are two problem:
    The two functions printf return different values and on each execution this program always return the same values.

    Any suggestions?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Check return results, and check GetLastError()
    What on earth is fflush(NULL);?! You should should probably remove that.
    There is no need to pass anything into the expand method.
    Don't use an array of 8 bytes, just declare a long long int and use a cast to when passing its address to CryptGenRandom.
    Read more on MSDN about the functions you are using here. E.g. CryptAcquireContext function
    I know the API is hard to use at first, so you may be best looking for someone else's working code sample.
    I've only used it at work and I'm at home right now, so I can't help much more just yet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    What on earth is fflush(NULL);?!
    The lazy way of using fflush without specifying the stream by flushing all streams (except those to which it does not apply).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah I could have just looked that up. It doesn't look like it's going to serve any useful purpose here though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed