Thread: How to create a string of alphanumeric values???

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    Question How to create a string of alphanumeric values???

    Hi all,

    I want to create a string of alphanumeric values. Actually it is for security purpose. I am using C++ and I want the fuction for that...string must be like this:4gh5js83gk6l0s
    what is the exact code to do this??

    Can anybody help regarding this?? Plzzzzzzzzzzz

    Best Regards,
    Symbee

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Do you mean you want to generate a random string?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Yes..!!

    I want this for the security purpose..like..when I open application ,some random values must be sent to the system for the validation...

    Can u help me to get this..

    Thanx n regards
    Symbee

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I'm not at home right now. When I get there, I will send you an example of a program I made which generated a random word. That program generated a random word and used some letters more than the others. Thats why I used a char string, where it chooses a letter from:
    Code:
    char letters[]={"aaabccdeefghhiiijkkkll"};
    And so on. If there's more of one letter, it appears more often.
    I think your program can be made quite easily, because you can just use ASCII character values.
    Once I get home I will post some examples.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    ok maxorator ..thanx for yr kindness..

    Send me those examples n also try to send me the code which also includes numbers with char like aa5bc72h1kc9

    Thanx again..waiting for ur reply

    regards
    symbee

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    srand( time(0) );
    
    // letters_numbers is a string type which has all the small and capital letters, with numbers too
    string letters_numbers = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    
    // This is where the 'random' string is stored
    string RandomString = "";
    
    for ( loop for whatever amount of times you want )
    {
        RandomString += letters_numbers[rand() % letters_numbers.size()]; 
    // Possibly add 1 to the % letters_numbers.size()
    }
    
    cout<< "Random string: " << RandomString;
    EDIT - you could do it with ASCII values, but it's simpler this way, IMO
    Last edited by twomers; 09-26-2006 at 03:41 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Thnx twomers)

    I will try this n will come back to u if would find ny trouble

    regards
    Symbee

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Using rand() to generate your random string isn't very secure (at all).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    I got the output....

    thanx all

    Symbee

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Salem
    Using rand() to generate your random string isn't very secure (at all).
    Why do you need security when generating random things?
    Let's say you get e8rt1aadhse4y88t. Even if it sometimes repeats the last letter, it is not guessable.

    I am still not home... long day...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Ya ..u r right max,

    Actually I am working on symbian which is a mobile technology(u might be knowing) and the platform is c++. I want to send a random string of such values through SMS to the main server.

    That seraver will create another string through some algorithm which uses our previously sent string.and will return it to the mobile. both would be validated on mobile also..n the purpose would be achieved!!(hope this explanation may not confuse u!!)

    So now I want to use some algorithm..So that the random string will be converted into some another string. which one would be best for this??

    Regards,
    Symbee

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lemme guess, you're trying to do this
    http://en.wikipedia.org/wiki/Challen...authentication
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Hi Salem,

    Yes, you can say...!!The idea is same but they havn't give anything in C++ prospect

    So anybody can help me to do the same???

    Regards,
    Symbee

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by maxorator
    Why do you need security when generating random things?
    Let's say you get e8rt1aadhse4y88t. Even if it sometimes repeats the last letter, it is not guessable.

    I am still not home... long day...
    rand() is deterministic; each value it returns is mathematically based on the previous value. It is not really random at all. If you srand() with the time, as many do, then simply knowing the approximate time the string was created could narrow the search to only a few thousand possibilities.

    Even worse, if you had an app that seeded once and then generated many passwords, if you determined the seed value, you'd know every single password that it would generate until the generator was re-seeded.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    rand() is deterministic; each value it returns is mathematically based on the previous value. It is not really random at all.
    This is true, but a cryptographic PRNG used correctly would suffice for security, even though it too would be deterministic.

    If you srand() with the time, as many do, then simply knowing the approximate time the string was created could narrow the search to only a few thousand possibilities.
    That is not the fault of rand(), but of the way the seeding was done.

    Even worse, if you had an app that seeded once and then generated many passwords, if you determined the seed value, you'd know every single password that it would generate until the generator was re-seeded.
    The point is that with the typical implementations of rand(), an attacker may be able to determine the seed value. With a cryptographic PRNG, an attacker's job would be considerably more difficult.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM