filling array with rand numbers

Discussion in 'C Programming' started by Bill Cunningham, Aug 4, 2013.

  1. Alice and Bob are the people trying to communicate, and Eve is the eavesdropper, or enemy signals intelligence people wanting to know
    what they are saying. GCHQ is our boys' eavesdropping centre, and also
    the home of the electronic computer. (You can argue about whether the first
    real computer was British or American, because of technicalities about the
    memory, but the British version was first, and it was installed at what
    became GCHQ).
    The rand() based method is extremely easy to code. Maybe ten lines of C.
    There are lots of uses for encryption. Making automatic filtering and
    monitoring too expensive to be practical is one of them.
    Malcolm McLean, Aug 5, 2013
    1. Advertisements

  2. (snip)
    Two men are walking through the woods and see a bear.
    One start running. The other tells him he can't outrun the bear.
    The first replies: "I don't have to outrun the bear, I only
    have to outrun you!"

    As for cryptography, you only have to do it well enough to
    discourage those looking.

    The usual rand() is probably good enough for card games, maybe
    better than most people actually shuffle a deck.

    -- glen
    glen herrmannsfeldt, Aug 5, 2013
    1. Advertisements

  3. Bill Cunningham

    osmium Guest

    Hell, I still don't understand magnetism. And I've been working on that
    since I was six years old. How could I possibly understand a mind messed up
    with chemicals? I took Paxil for a few days once, and it made me a believer
    in the kind of thing that Bill has talked about.

    I have always liked challenges and the older I get the longer my "do not
    understand this" list gets. But I keep going bacl to the list. I see
    something like that in Bill; but I have had better luck working on my list
    that he
    has had.

    I also think programming is almost unique, having a seductive aura of
    unleashed power. Also the instant gratification is rare in the things one
    can actually do. And it's doable, you don't need a better lens, a bigger
    planer, an electron microscope. Just you, your mind, and a cheap computer.
    osmium, Aug 5, 2013
  4. Geoff wrote:

    Yes. I placed r=rand(); into the for loop and it seems to work. The
    encrypt() function and setkey() functions I would try next. The second
    parameter of encrypt is 0 or 1 for decrypt or encrypt. 'man encrypt' on a
    linux machine shows this. But encrypt is OT.

    Bill Cunningham, Aug 5, 2013
  5. Bill Cunningham

    Ike Naar Guest

    Burditt, please learn to quote.
    Ike Naar, Aug 5, 2013
  6. Yes, I meant people playing at home, and not for real money.
    Even so, for a 32 or more bit rand() it would take some time
    before you had enough data to figure it out. (Assume you have
    the card sequence, but not the program source.)
    Yes, again, I meant people playing at home with family or friends.
    Most people are too lazy to shuffle enough times. Seems like one
    could argue for at least log2(52) shuffle passes, maybe twice as
    many as each isn't quite as good as one might hope. (And a perfect
    shuffle will result in less randomness.)

    -- glen
    glen herrmannsfeldt, Aug 5, 2013
  7. Bill Cunningham

    BruceS Guest

    That's mostly what I see from Bill, but it goes further than that. He
    seems to come up with questions that are extremely basic (and mostly not
    really C issues), designed to get as many responses as possible. Some
    of his posts also present him as doing things *far* beyond what other
    posts would indicate was his capability. If he isn't trolling, he's
    doing an admirable job of appearing to do so.

    With all that said, sometimes a troll post has some value, in starting a
    discussion that may help someone understand part of the language, or of
    programming in general.
    BruceS, Aug 6, 2013
  8. Bill Cunningham

    Geoff Guest

    Use encrypt(text) and decrypt(msg) functions. There is no need to use
    a single function named encrypt with two arguments to change it's
    mode. This makes the interface more complex than it needs to be and
    complicates the function. This kind of problem is good for
    demonstrating the partitioning of the tasks into concise functions,
    one of the first principles of C.
    Geoff, Aug 6, 2013
  9. Bill Cunningham

    Geoff Guest

    I don't believe Bill is a troll. I suspect he's a retired old
    gentleman with nothing better to do with his time and an interest in
    the topic and the challenge of programming. Whether he's medicated or
    not is not for me to judge. Whether he learns or not is not my
    responsibility nor do I feel obliged to reply to him or make him
    learn. We all have our deficiencies and our strengths.

    I also feel that anyone who feels he's a troll should ignore his posts
    and simply behave like civil human beings and stop adding to the noise
    in threads started by him. Those who troll the troll are trolls
    Geoff, Aug 6, 2013
  10. I don't remember his other posts, and doing psychology in a C newsgroup
    probably isn't a good idea, but maybe it is related to dyslexia.
    Maybe he didn't realize that the assignment was outside the loop.

    Or, maybe it isn't so obvious how C should work. While it seems
    obvious that the assignment is done once when the statement is
    outside the loop, maybe that isn't so obvious.

    I was recently reading about how (at least one version of) ALGOL
    allocates dynamic arrays. It seems that they are allocated when
    first referenced, but to the size declared. In C99 terms:

    double x[n];

    Now, it is obvious in C that can't work, as n is assigned after
    the array is allocated, but it seems not so obvious in ALGOL.
    (Then again, no-one would ever think of call-by-name in a
    C context.)
    -- glen
    glen herrmannsfeldt, Aug 6, 2013
  11. Bill Cunningham

    Geoff Guest

    It's a flaw in C that it permits the programmer to leave braces out of
    loop statement bodies. This should have been a requirement from the
    beginning since it now has to be defined as a best-practice. I imagine
    Bill changed is program from

    int main()
    int i, r;
    char key[64];
    char txt[64] = { 0 };
    r = rand();
    for (i = 0; i < 64; i++)
    key = r;
    printf("%d%d\n", key[0], key[1]);


    int main()
    int i, r;
    char key[64];
    char txt[64] = { 0 };
    for (i = 0; i < 64; i++)
    r = rand();
    key = r;
    printf("%d%d\n", key[0], key[1]);

    instead of

    int main()
    int i, r;
    char key[64];
    char txt[64] = { 0 };
    for (i = 0; i < 64; i++)
    r = rand();
    key = r;
    printf("%d%d\n", key[0], key[1]);

    and as a result is as likely to misinterpret the garbage value of the
    uninitialized members of key[] as his random values.

    The problem with "teaching" in newsgroups is a simple one - it's
    open-loop. You don't know whether what you are teaching is being
    interpreted correctly without feedback.

    This problem is compounded by topic drift and trolling and generalized
    nit picking that's a direct result of people in the group who, on
    occasion, seem to be occupied with proving how much smarter they are
    or how extensive is their knowledge of minutiae. One former boss had a
    name for this, he called it mental masturbation. In this newsgroup it
    seems to be a group sport.
    Geoff, Aug 6, 2013
  12. He knows how; he just refuses to provide attributions, for reasons that
    make no sense to me.
    Keith Thompson, Aug 6, 2013
  13. Bill Cunningham

    osmium Guest

    You misunderstood my question. There was not a one bit message. There were
    enough bits in the encoded message to represent 12 characters. The computer
    had a 15- bit byte and the native character code was 5-bit Baudot. A PRNG
    generated a string of bits, a field of five bits were selected and XORed
    against each plain text character. Many PRNG streams can be postulated and
    they will produce many 12 character plain text messages. Postulate all
    candidate PRNG streams (different constants and different seeds and
    different five-bit fields) and number them. Let's say stream 123618236
    yields atackatdawn and stream 0449852 yields sueforpeace. When used for
    decoding, most of the streams produce garbage instead of English. There's a
    big hint! The plain text was in English.

    Now describe the brute force attack.
    osmium, Aug 6, 2013
    Kenny McCormack, Aug 6, 2013
  15. In 1941 the British sent two commando units, a detachment of Royal Engineers,
    some Norwegian volunteers, and a flotilla of supporting destroyers to
    raid the Lofoten Isands. The raid was spectacular success, with about 200
    Germans captured, and only only officer wounded. The real objective though,
    which had to be kept secret, was the capture of a German Enigma machine.

    Now had the Germans spotted the ships and elected to fight, the Enigma
    machine would probably still have been captured, but the force would have
    taken casualties, maybe heavy casualties. You can get enemy algorithms.
    But it's not cheap.
    Malcolm McLean, Aug 6, 2013
  16. Bill Cunningham

    Geoff Guest

    I don't believe that is true. At least I do not believe the real
    objective was to obtain a machine. Post-raid, I expect the primary
    goal would have been to keep the secret that they had captured a
    military-grade machine.

    Commercial Enigma machines could be bought on the open market before
    1927. The Polish intercepted (accidentally) a model D machine and
    duplicated it. They were breaking Enigma traffic by 1932 and shared
    their work with the British.

    Military Enigma machines differed from the commercial ones with the
    addition of the plugboard. There were also models that added more
    wheels or had a greater number to select from.

    The flaw of Enigma was that a letter was never encrypted as itself.
    The Germans also it used to encrypt weather reports which were always
    prepared to a standard form. This enabled known-plaintext attacks. You
    could simply slide a plaintext word across an encrypted message and if
    the encrypted message encrypted the same letter twice you could slide
    past that point in the message since it could never contain that word.

    The British were manually decrypting Enigma messages before 1940. They
    were using the Bombe by 1941 and the Americans had more than 120
    machines by 1942 and could crack five 4-rotor messages before an
    Enigma operator could decrypt one.
    Geoff, Aug 6, 2013
  17. Bill Cunningham

    osmium Guest

    Thanks, that was very helpful. The thing I was trying to focus on was false
    decoded results, especially with short messages. I really have no idea of
    how many plain text messages one can expect to find in say 2^32 seeds. Are
    there papers on that?

    I would expect the "breaking" algorithm to focus on the first n characters
    and see if they contain an English word or phrase. I would think n could be
    as little as 7 or so. If this test is passed, decode some more of the
    message, perhaps all of it, and let a human look at it. With ASCII in an
    eight-bit byte, there will be very few surviving seeds with n as small as 7.

    To slow down the codebreakers, make a few spelling mistakes in the first few
    characters of the plain text message. The human recipient will handle the
    problem fine, the computer attacker will have to guess at the actual message
    length so he can offset the starting point of his test. Why not make 4096
    bytes the minimum transmitted message length? If the attacker starts his
    test too far out in the message, he will try to decode white noise or some
    other artifact. The sender doesn't even have to tell anyone he has
    introduced spelling errors. He can just accept the abuse he gets as a bad

    I chose Baudot code in my "setup" because it will give a high concentration
    of latin characters, tending to increase the load on the codebreaker.

    I realize this is kind of pointless and there are better ways to provide
    cryptographic security, I just think it's a lot of fun. Come to think of
    it, a lot of fun things are pointless.
    osmium, Aug 6, 2013
  18. (snip on Enigma)
    Yes, the biggest mistake.
    As well as I remember it, though, they had to keep up with continuing
    improvements. Each was an incremental change, which they then broke.
    If they had to start with the final version, it might not have
    been done.

    The second mistake was confidence in its security.
    -- glen
    glen herrmannsfeldt, Aug 6, 2013
  19. (snip regarding random numbers and card shuffling)
    Yes, but it isn't likely in a family card game with everyone
    sitting in the same room. If you see your brother check his
    phone after every deal, you should get suspicious.
    Say you use the usual algorithm of assigning a random number
    to each card, and then sorting based on those numbers. Seems
    to me that it would take more than six cards in that case.
    Also, you assume that you get to see all the cards. In poker,
    for example, you often don't see the losing hands. But I didn't
    go all the way through the math to be sure.

    And I doubt Nevada casinos will let you stand around typing
    cards into your phone for even one deal.

    -- glen
    glen herrmannsfeldt, Aug 6, 2013
  20. Bill Cunningham

    Nobody Guest

    Bill Cunningham:

    Sjouke Burry:

    Keith Thompson:

    Let's go back to Bill's original post (emphasis mine):

    If you just move the rand() inside the loop, there's no guarantee that all
    of the numbers will actually be different.

    At least, I think that's what Sjouke was getting at.

    Tangentially related:
    Nobody, Aug 7, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.