Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sludgy character array problem

sludgy character array problem

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuressecuritydebugginghelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Aldrich
    wrote on last edited by
    #1

    I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:

    // seed a random number generator with the current
    // tick count. we will use this as an iterator in
    // the szRandomBytes array a bit later

    srand(GetTickCount());

    // set some values so that we only get legal characters
    // from the szStringArray which will be used to fill
    // the szRandomBytes array with legal characters

    int nElementCount = 0;
    int nElementValue = 32;

    // set up our two arrays. szStringArray gets filled
    // with legal character values. szRandomBytes uses
    // the values in szStringArray to generate a random
    // character string that we can use for encryption

    char szStringArray[95] = "";
    char szRandomBytes[64] = "";

    // filling the array

    while ( nElementCount <= 94 )
    {
    szStringArray[nElementCount] = nElementValue;
    nElementCount++;
    nElementValue++;
    }

    // set iterator to zero

    nElementCount = 0;

    // pull 64 random characters out of the array
    // to create our random string for encoding

    while (nElementCount <=63)
    {

    // get a random number
    
    
    int nRandomNumber = rand();
    
    
    // checking it out.  if its greater than 95
    // we divide it until its with the bounds of
    // the szStringArray.
    
    
    while (nRandomNumber >= 95)
    {
    	nRandomNumber = nRandomNumber / 2;
    }
    
    
    // using nRandomNumber and szStringArray to assign
    // a value to the Element Number that szRandomBytes
    // is currently on up to a possible 64 characters
    
    
    szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\];
    
    
    // incrementing the element count so
    // that the next value gets filled.
    
    
    nElementCount++;
    

    }

    // debug check

    MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);


    It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you

    R J 2 Replies Last reply
    0
    • J John Aldrich

      I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:

      // seed a random number generator with the current
      // tick count. we will use this as an iterator in
      // the szRandomBytes array a bit later

      srand(GetTickCount());

      // set some values so that we only get legal characters
      // from the szStringArray which will be used to fill
      // the szRandomBytes array with legal characters

      int nElementCount = 0;
      int nElementValue = 32;

      // set up our two arrays. szStringArray gets filled
      // with legal character values. szRandomBytes uses
      // the values in szStringArray to generate a random
      // character string that we can use for encryption

      char szStringArray[95] = "";
      char szRandomBytes[64] = "";

      // filling the array

      while ( nElementCount <= 94 )
      {
      szStringArray[nElementCount] = nElementValue;
      nElementCount++;
      nElementValue++;
      }

      // set iterator to zero

      nElementCount = 0;

      // pull 64 random characters out of the array
      // to create our random string for encoding

      while (nElementCount <=63)
      {

      // get a random number
      
      
      int nRandomNumber = rand();
      
      
      // checking it out.  if its greater than 95
      // we divide it until its with the bounds of
      // the szStringArray.
      
      
      while (nRandomNumber >= 95)
      {
      	nRandomNumber = nRandomNumber / 2;
      }
      
      
      // using nRandomNumber and szStringArray to assign
      // a value to the Element Number that szRandomBytes
      // is currently on up to a possible 64 characters
      
      
      szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\];
      
      
      // incrementing the element count so
      // that the next value gets filled.
      
      
      nElementCount++;
      

      }

      // debug check

      MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);


      It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you

      R Offline
      R Offline
      Ranjan Banerji
      wrote on last edited by
      #2

      Put a terminating character at teh end of your string. The last character in your array should be '\0'. You are trying to display a string in your MessageBox by putting the array of char but are not giving your array of char a termination indicating this is where the string ends.

      1 Reply Last reply
      0
      • J John Aldrich

        I'm having problems with an array tacking a bunch of sludge on to the end of a array of random characters. It needs to generate an array of 64 random bytes for use inan encryption routine. I'm getting the 64 bytes of information that I want at the front end of the array, BUT..it's also tacking on the contents of the original array that I filled with a sequence from ASCII 32 to ASCII 127 character values. My question is how to get the sludge off the end of the array so it consists of just teh 64 random byte values. Source Code Follows:

        // seed a random number generator with the current
        // tick count. we will use this as an iterator in
        // the szRandomBytes array a bit later

        srand(GetTickCount());

        // set some values so that we only get legal characters
        // from the szStringArray which will be used to fill
        // the szRandomBytes array with legal characters

        int nElementCount = 0;
        int nElementValue = 32;

        // set up our two arrays. szStringArray gets filled
        // with legal character values. szRandomBytes uses
        // the values in szStringArray to generate a random
        // character string that we can use for encryption

        char szStringArray[95] = "";
        char szRandomBytes[64] = "";

        // filling the array

        while ( nElementCount <= 94 )
        {
        szStringArray[nElementCount] = nElementValue;
        nElementCount++;
        nElementValue++;
        }

        // set iterator to zero

        nElementCount = 0;

        // pull 64 random characters out of the array
        // to create our random string for encoding

        while (nElementCount <=63)
        {

        // get a random number
        
        
        int nRandomNumber = rand();
        
        
        // checking it out.  if its greater than 95
        // we divide it until its with the bounds of
        // the szStringArray.
        
        
        while (nRandomNumber >= 95)
        {
        	nRandomNumber = nRandomNumber / 2;
        }
        
        
        // using nRandomNumber and szStringArray to assign
        // a value to the Element Number that szRandomBytes
        // is currently on up to a possible 64 characters
        
        
        szRandomBytes\[nElementCount\] = szStringArray\[nRandomNumber\];
        
        
        // incrementing the element count so
        // that the next value gets filled.
        
        
        nElementCount++;
        

        }

        // debug check

        MessageBox(NULL, szRandomBytes, "Random String From Array", MB_OK);


        It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you

        J Offline
        J Offline
        jmkhael
        wrote on last edited by
        #3

        char szStringArray[95] = ""; char szRandomBytes[64] = ""; memset(szStringArray,0,95); memset(szRandomBytes,0,64); And off u go Papa Murex Co. while (TRUE) Papa.WillLove ( Bebe ) ;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups