CryptGenRandom

This is a discussion on CryptGenRandom within the C Programming forums, part of the General Programming Boards category; This is a part of my program: Code: long long int expand(long long int part) { HCRYPTPROV hCryptProv = NULL; ...

  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,299
    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
    21,598
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,299
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21