Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: rand() ... How many random numbers before cycle repeats  (Read 9796 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
rand() ... How many random numbers before cycle repeats
« on: December 31, 2016, 09:38:28 AM »
I was looking online and cant find anything out there other than this one link below. Maybe my search is using wrong search words. I am curious with the standard rand() algorithm for a Windows based computer ( Stating Windows because I heard that Linux generates rand() differently ) how many numbers can be generated before the cycle repeats, basically the limitations of the algorithm. I expected this to be well documented and nothing to be found yet.

http://justbasic.conforums.com/index.cgi?board=tutorial&action=display&num=1170007376

What I found was this one hit that was the closest match but no cigar.

The random generators algorithm should repeat, but how many iterations can it run before it ends up back where it started in which the next numbers generated become predictable because it made a full circle?

At the link above someone else was already where I am now and they seemed to give up at 2 Billion generated Numbers with no repeating found.

Quote
1) JB have at least 2 000 000 000 un-repeating random numbers;
2) I do not know if they repeat anythere soon after 2 000 000 000 (remember, that 256^4 = 4 294 967 296)
3) probably JB not supposed to run for several days in a row non-stop. (Well, It might be a coincidence. I just not in mood to start this program for another 3 days).

Had a laugh at this part:

Quote
(Well, It might be a coincidence. I just not in mood to start this program for another 3 days)

Last year I ran a program looking for the weakest key for my program. A key used that when info put into it, the output would be clear text. I gave up after 1 Billion searches for this worst key ever and it took almost a week on a 8-core AMD FX-8350 at 4Ghz with each core processing 125 Million searches for the worst key since my C++ programs are single-threaded to make the most use of my processing power. This program was very fast and very small and just took an string, shuffled it, and tested the output result to see if it matched the initial string. The string length was 85 characters and that has since grown to 89 characters which means it will take even longer to find that worst key ever. I have since implemented in my code a key strength tester that looks for duplicates between the character placement of the initial string and the output to look for characters that after shuffled end up back in their original location as for this shuffle with too many back in their original position would mean that some information would be clear text, and the more characters that didnt change placement in the shuffle, the easier it is to crack the secret message etc.

If I was to brute force search for the end of the algorithm, I was thinking that storing the first 10 very large numbers generated and then have a counter that counts how many iterations the random generator ran to display in the end, and test each next group of 10 randomly generated numbers to the initial group of 10 might be the way to find that point when it makes a full circle, however its also possible that the algorithm that is like the tracks of a roller coaster has as exactly curved section of track that is of 10 numbers, but the 11th onwards dont match up, in which then I have to expand my search to say a group of 20 as a exact match and then test the 21st value and see if that matches the next.

Multiple computers could be used  if you used a manually injected seed to place each of the systems into their own crunching quadrant of numbers to work with similar to how I took my 8-core processor and broke it up into 125 Million iterations each core for the 1 Billion keys tested, I could have each core of each system set to start at a different skipped ahead position from the other so that each core is working as a team with all cores of all computers involved to hunt for that initial group of 10 very large numbers and then stops showing that it found a match to the 10. Once that match detected it shows the value of iterations and I know the seed value. Then removing the seed value from the iterations the exact number of iterations should be able to be calculated. And then the limit position in which the random generator made a full circle is then known.

But instead of brute forcing to find this number at the cost of an elevated electric bill, its always best to see if someone else out there knows this magic number before the algorithm repeats. Additionally, there may be different algorithms with a different number before full circle. I am, working with a Windows 7 64-bit computer and C++

The point behind wanting to know where the algorithm makes a full circle is to then understand how many keys are available before keys of a different value give the same result as key #1 such as Key #1 and Key # 298,371,000,000,000 might be the same because of the full circle of the algorithm.  ;D And only the first billion I know are unique in green. with the unknown in red. *Also with 89 characters in a string shuffle, I think its 89 to the power of 89 which is a huge number of different shuffles.  ::) The first billion were tested under an earlier version of my program that only used 85 characters for 85 to power of 85.  :P

Sometimes I think so far outside of the box that I forget about K.I.S.S so if there is a better way to hunt for these if they need to be hunted if undocumented to find that full circle of the algorithm please share a better method.  ;D If the algorithm formula was known it might also be a means of knowing its limit in which it makes full circle. Additionally it might be a long long time before it repeats if its a really well constructed algorithm in which it might be completely pointless to hunt for it since it could take a very very long time to repeat with all the processing power that can be thrown at it.  ::) Example being the best server out there taking 514 years before it makes full circle.  :P

I really liked this part of the thread i read from the link:
Quote
"If we had a infinite amount of monkeys randomly pressing keys on an infinite amount of typewriters then eventually one of them will type out the entire Old Testament, correct to the last full stop."
  :P ;D

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #1 on: December 31, 2016, 10:03:28 AM »
the psuedorandom number generator will repeat individual numbers but the whole cycle never repeats, except in the same sense that Pi's digits repeat- that is since it can be drawn out to infinity then the statistically improbable become a certainty. If you generate 2 billion random numbers, the chance of the first billion being the same sequence as the second billion is infinitesimal. However if you instead generate an infinite sequence of random numbers, the chance that there is a sequence of one billion values followed directly by the exact same sequence of a billion values is a statistical certainty.

But computationally generating such sequences where that phenomena could be observed would probably take even all the fastest supercomputers many thousands of years to discover (and that's an optimistic estimate).
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #2 on: December 31, 2016, 10:28:36 AM »
Thanks for your input on this BC...

Plus I was also feeling it would be a crazy amount of processing power to discover with a very long time to make that discovery.

Quote
But computationally generating such sequences where that phenomena could be observed would probably take even all the fastest supercomputers many thousands of years to discover (and that's an optimistic estimate).

I guess I will just go with the fact that the first billion keys are safe from spitting out the input as the output in clear text. Additionally I have a key strength test in the program that allows you to set the strength for key to be used where all characters need to change position from their origin within the array - or - you can allow some to remain their initial character so that if trying to crack a message it is unknown if a character is truly substituted or is truly itself. Such as out of 89 characters only 2 could be not swapped for example if the value of 2 is given to the strength test function vs all swapped from their location.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #3 on: December 31, 2016, 10:56:14 AM »
As far as your specific shuffling test, The default shuffle will swap every element with another randomly chosen element in the sequence. It is literally impossible for "RANDOM" to be shuffled into "RANDOM" regardless; the R, for example, will be randomly swapped with one of the other letters, and cannot swap with itself so will always be moved, as will all the other letters. The only way the same letter can appear in the same location in the input and output is if it appears more than once and is swapped with one of that letters other appearances.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #4 on: December 31, 2016, 04:45:55 PM »
What is the goal?
Do you want real random numbers?
The use a real random number generator.
http://arstechnica.com/security/2013/12/we-cannot-trust-intel-and-vias-chip-based-crypto-freebsd-developers-say/
This is about hardware that has been in Intel chips.

Also:
http://stackoverflow.com/questions/7901829/making-use-of-sandy-bridges-hardware-true-random-number-generator
Quote
Be aware that rdrand is actually a 128-bit pseudo-random number generator with hardware-generated entropy. (The upcoming Broadwell architecture will provide an rdseed instruction to access the true random number generator.) The details of the difference and its implications can be found under the "Long Answer" heading at http://software.intel.com/en-us/blogs/2012/11/17/the-difference-between-rdrand-and-rdseed.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #5 on: December 31, 2016, 04:51:56 PM »
First link is about how Intel and VIA hardware Crypto random number generation cannot be trusted.

Second link is about how to utilize the Intel RDRAND instruction.

Dave is using an AMD CPU.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #6 on: December 31, 2016, 05:29:36 PM »
...
Second link is about how to utilize the Intel RDRAND instruction.
Dave is using an AMD CPU.
AMD did it.
http://support.amd.com/TechDocs/24594.pdf
see page 34
Quote
Support for the RDRAND instruction is optional. On processors that  support the instruction, CPUID Fn0000_0001_ECX[RDRAND] = 1

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #7 on: January 01, 2017, 12:39:02 AM »
It is only implemented in their latest line of CPUs (Excavator Core, Athlon X4 Desktop CPU and 8000 series APU). It is not an instruction available for use on the 8350FX.



I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #8 on: January 01, 2017, 02:19:19 AM »
Thank you  BC for providing some usefull information.
I founds  this:
http://crypto.stackexchange.com/questions/29894/amds-implementation-of-rdrand-instruction
So, exactly how the hardware does it is either a mystery or they do not think we need to know. Does this mean programmersare goingto avoid ysing it? So the software methodwill continue to be tghe standard?

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #9 on: January 01, 2017, 02:42:21 AM »
As I understand it the main difference is that the distribution is cryptographically secure, so it is preferable for generating random numbers as part of a encryption algorithm. But outside of that pseudorandom is good enough. As an example a lot of  games use random numbers to control certain AI decisions or game events or what-have-you, but there is no need for the number generator to have a cryptographically secure distribution.

Even in Dave's specific case I don't think a cryptographically secure distribution would serve to improve a random cipher. (Could be wrong about that)
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #10 on: January 01, 2017, 04:45:39 AM »
Quote
Even in Dave's specific case I don't think a cryptographically secure distribution would serve to improve a random cipher. (Could be wrong about that)

One problem I could see using a random generator that is from one specific class of processors is that if the data was masked under a shuffle and substitution under one processor and then brought to a different processor that didnt have this instruction set function, the keys to unlock that info back to its original form, the inverse as the best choice of words for the original random function would likely not give the same outcome and so the information would still look scrambled. I havent tested my program among different CPUs yet because it was my understanding that the rand that I am using in C++ is one that is tied to the OS that is in use with the pseudorandom. I suppose I could go down the path to use a CPUs instruction set for a better nonpseudo random but the info I have to mask out from eyes of anyone accidentally finding access to my information isnt that critical. The existing means that i have now with a key that has to specifically be the one and only key and I have available keys that far exceed 1 billion of them, and I have seen how much processing power is needed to test for 1 billion keys, solving for 1 billion would take even far more processing power.

The next revision of my code is going to be to remove the static string by which the program starts with for substitution so that it injects the string from a file This way the starting string before the shuffle is unknown vs a static known.  ;D

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #11 on: January 01, 2017, 10:56:13 AM »
Quote
As far as your specific shuffling test, The default shuffle will swap every element with another randomly chosen element in the sequence. It is literally impossible for "RANDOM" to be shuffled into "RANDOM" regardless; the R, for example, will be randomly swapped with one of the other letters, and cannot swap with itself so will always be moved, as will all the other letters. The only way the same letter can appear in the same location in the input and output is if it appears more than once and is swapped with one of that letters other appearances.

The C++ Shuffle Function I am using I have detected where characters shuffled end up back in their starting position. This is why i had to implement a key strength routine to my code to give choice of strong or weaker keys. Having a choice of strong or weaker keys allows for all keys to be considered unknown to whether a string or weaker key was used when shuffling the information. I ran a smaller test program that wrote to file keys that were strongest and there are quite a few keys that are weaker with 2 characters or more remaining in their starting position such as

ABCDEF shuffled to FEDCBA is every character is no longer in its original position in the string, so the array that has these characters for substitution if the message to be scrambled up is BAD FACE it would be EFC AFDB

But in a weaker shuffle where a weaker long long int key is used to seed the random shuffle if ABCDEF was shuffled as ABCDFE now the message to scramble is seen in the output saved to file as BAD EACF and BAD is plain as day and it could then be assumed that BAD are 3 letters that werent shuffled out of their original placement within the starting string. So the ( A ) in EACF is very probable the A of the next word.

Here is more on the Random_Shuffle function: http://www.cplusplus.com/reference/algorithm/random_shuffle/

Quote
Randomly rearrange elements in range
Rearranges the elements in the range [first,last) randomly.

The function swaps the value of each element with that of some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.


This function states that it carries out this action as a for each character
:
Quote
The function swaps the value of each element with that of some other randomly picked element

Initially I thought just as you stated that RANDOM would be a scramble where every character was placed different from its original position, but I think the issue is in the fact that if there are 15 characters, it is doing a for each character shuffle stepping say left to right.

So you can have this happen below:

ABCDEFGHIJKLMNO = Starting string

Random_Shuffle then swaps first character or element A with C

CBADEFGHIJKLMNO

Random_Shuffle then swaps next character B with O

COADEFAHIJKLMNB

Random_Shuffle then swaps next character A with C ( *Here is how A and C get back to their original place within the string )

AOCDEFAHIJKLMNB

It will perform this for 15 iterations for 15 characters and the worst case scenario would be that the shuffle placed everything back into its original order of

ABCDEFGHIJKLMNO

However its more probable that a few characters are in their original location and this makes for an long long int value that is used as a key value for the seed of the random_shuffle to be a weaker key since this seed value shuffles the string in a way that some characters are not moved out of their original place within the string.

When I first read up on this random_shuffle function having never used it until this program as for in the past I have made my own shuffler function vs using a built in function, I read this as the fact that every character would be shuffled so that none would ever end up back in their original placement, but then i got thinking about it and realized that its probably not testing in any way to avoid a character from going back to its original place within the string. So I needed to test for this. To me it was important for it to be able to make for weaker keys, otherwise if only strong keys (strong seeds) were used, then you could throw out a very large list of keys as not in use for scrambling data. So for the fact that it can shuffle in a way that characters can make their way back to their original position and choice to use a strong or weaker key and no one knowing if you used a stronger or weaker key, you then wouldnt be excluding a whole bunch of keys from use.

I dont have the source code on me to list here the test program, I have it at home and i can share it here so you can see how its placing characters back into their original location. I have a loop that you tell it how many times to run say for 10,000 times and it steps through each seed value and performs a string compare between the original static string and that of the output on a character for character ( element for element ) basis between the original and the shuffled. It then has flags that when x-many matches are found to display that iteration of the 10,000 to display what seed values match to that weak key with say 4 characters that end up back in their original location after the random_shuffle has completed.

The random_shuffle initially I thought was playing 52 pickup  ;D in which throw a deck of cards into the air and pick the cards back up and there is the chance that you could pick them up and have 2 through ACE in the 4 suits perfect as the day you first opened the pack of cards, but from testing with it it seems like its doing a 1 for 1 for as many characters are in that string so its running left to right with substituting  a 1 for 1 until end of the string which would use less memory at the cost of clock cycles. But with as fast as computers are today extra clock cycles for a program like this is just about unnoticeable. Its too bad there is no way to pause the random_shuffle function through its execution in which you can step the clock cycle for the program and see the inner workings of it to see if its 52 card pick up or a 1 for 1 which I think its a 1 for 1 left to right possibly. The seed to the generator in a 52 pickup would be the trajectory of the cards and where they landed and to which order they are picked up in a very bent analogy that sounds godlike quantum magic knowing exactly as something would fall and land based on all parameters of the environment in which that one seed value alone is the fate of all cards in the 52 pickup as to how they will reenter the deck form in known order based on the seed alone and rules that dont change so outcome is always the same based on that one single long long int.  ::) ;D


Quote
Size of long long types is 8 bytes
Signed long long min: -9,223,372,036,854,775,808 max: 9,223,372,036,854,775,807
Unsigned long long min: 0 max: 18,446,744,073,709,551,615

Note that int and long are the same size and if you want a 64 bit integer then you need to use long long (or unsigned long long).

Quite a number of keys to pick from with the Long Long Int seed.  ;D And I have only played around with the first Billion of them and that took a FX-8350 8-Core 4Ghz almost a week to complete testing full tilt 125 Million keys per core with 8 copies of my program running testing keys on a Windows 7 64-bit based system with 8GB RAM.  :P The computer became a good loud fan noisy space heater for that week since I dont have liquid cooling, just the stock aluminum block heatsink and it levels out at 63C with CPU fan on maximum RPMs.  ::)


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: rand() ... How many random numbers before cycle repeats
« Reply #12 on: January 01, 2017, 11:46:28 AM »
I see, I assumed it would use the well-established fisher-yates/Knuth shuffle, which works as I described and would result in the output never having a letter in the same position in the output as the input. But it looks like most implementations are arguably awful and merely swap each element with a randomly chosen element from the entire sequence, and it can even choose to swap with itself.

The idea that a shuffle algorithm could return output that is the same as it's input and call it "shuffled" is poor design IMO.

if you want to avoid the possibility completely, you could use your own fisher-yates/Knuth shuffle. Basic algorithm is to go through each element and swap it with any element before that element chosen at random. Just do a manual run through: (using random.org)

{1, 2, 3, 4, 5}

1. for the first element there are no elements before it
for the second, we have only one choice, so we have:

{2, 1, 3, 4 ,5}

The third index chooses between the first and second element. It chooses the second element:

{2, 3, 1, 4, 5}

Fourth index chooses between the first three; It chooses the second.

{2, 4, 1, 3, 5}

Final index swaps between one of the first four. It chooses the third.

{2, 4, 5, 3, 1}

Which is our final result.

Heres an implementation using rand(), (mind you the randint() function exhibits low-bias due to the modulus unless the number of elements is evenly divisible by the maximum int value)

Code: [Select]
int randint(int min, int max)
{
return rand() % (max - min + 1) + min;
}
void yates_shuffle(vector<int>& v)
{
int n = v.size();
for (int a = n - 1; a >= 0; a--)
{

int j = randint(a, n-1);
int tmp = v[a];
v[a] = v[j];
v[j] = tmp;
}
}





One interesting note regarding random numbers, however, is that rand() and srand() are actually strongly discouraged, in preference to some new way of doing it which presumably uses classes rather than being global state.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #13 on: January 01, 2017, 01:19:19 PM »
Quote
The idea that a shuffle algorithm could return output that is the same as it's input and call it "shuffled" is poor design IMO.

I too thought the same, however if you need all combinations of A thru Z you wouldnt want to exclude A thru Z I suppose from the output. A better example would be the shell game with 3 shells and which one has the marble under it. Your starting position is say the shell furthest left. You wouldnt want people having a 50/50 chance that it has to be the middle or the right, you would want it to remain at 33% that all 3 locations for the marble to be located are in play.  :P However a shuffle back to original form does seem kind of silly for so many other purposes where you dont want to have the output the same as the input. Catastrophic for a video game with a mortar range that is random. Range = 0 .... EPIC SHUFFLE FAIL  ;D

And that was why i went on the hunt for that magic number in the vast pool of Long Long Int looking for that magical worst seed value possible in which the output is the same as the input after a shuffle, and its not within the first Billion. I instead adapted a key strength test to the program so it will only allow use of a key thats strength matches what you allow. If allowance is 0 then all characters have to be in different placement for a complete shuffle an if you set it to 2 then 2 characters can be in their original spot as the allowance, but greater than 2 it rejects the key and says try another because the key you used is too weak. I should have just went this route to begin with vs running a 8-core computer for a week in search for that magical shuffle back to its original state of what was 85 characters and now expended to 89 characters. At some point it will be 90 characters with space considered as a character, but havent added that yet. Currently I use underscores or other characters in the message as word breaks such as [  Hello_World-It's~January_First ] to avoid [ HelloWorldIt'sJanuaryFirst ] so with 90 character support with the ASCII for space added I would have the ability eventually to  have [ Hello World It's January First ] However spaces in crypt are a weakness because it would stand out like a sore thumb with a single string substitution where every character substituted for another you can figure out vowels pretty quickly and then pick away at it and crack it pretty fast.  So my use of [ _-~ ] as word break spacers makes the word length a little more difficult to see vs such as if space was substituted for * you would see something like [ h7ss@*K0(sm*$9:*PqzVx(+*#u(^X ] where the * frequency sound be used as a space identifier and using a character of choice as a space holder would make something like this instead [ h7ss@?K0(sml$9:8PqzVx(+A#u(^X ] where word length is suddenly harder to find to dig at vowel probability.

The latest program that uses the flipflop uses 2 different shuffles and picks from them alternating so that something like AAAABBBBCCCCDDDD end up showing up as DxDx8v8vQwQw$c$c, additionally with two different shuffles to pull from you also have probability for characters to be used to substitute for two different characters where say L could be a D or a 7 depending on if its in the string at an even or odd interval withing the string so that [ HELLO ] could be an outcome of [ 4&D7P ] and now that D7 is unknown that its a double LL. This way if you look at the end result and see WW you cant say ohhhh that is a double letter or number and rule out the WW's in the text file as being high probability as being specific alpha characters in words or 00 thru 99.

Its been a lot of fun building up on this project and tinkering on it as I have time. I am learning from my mistakes and adding features reusing test code in the main program for example to exclude weak keys as an option based on what you want for key (shuffle strength).

Currently working on the inverse to this next version to convert the DxDx8v8vQwQw$c$c back to AAAABBBBCCCCDDDD which requires 2 Long Long Int keys to be entered and correct to get back to the initial input. And looking for why it gets stuck in an endless loop where it should run for as many iterations as is the length of the input so if you type in ABCD it should process 4 loops and end. Just like the flipflop problem I had with the other program its probably right in front of me and I will work at finding it. I have a condition where if a character is not recognized it will state that fact. If it hits this, then it means that a character wasnt satisfied and an unsupported character was used. I started looking at my arrays first to make sure I didnt make the mistake of arrays starting at 1 instead of that they start at 0 for first element in which you could essentially call to an array outside of the scope of the string in which you then are injecting whatever is in the memory at say array element 4 instead of 3 for the letter D and 4 is returned as garbage input that is not a supported ASCII character. Currently I have debugger lines added in there that I enable and disable through //commenting out to see that its flipping between 01010101 so I know thats correct but the fact that its doing whats seen below in an infinite loop vs ending after say 4 iterations for ABCD is strange and its a flaw in the code that I will find. Sometimes walking away from code and coming back to it it will all of a sudden stand out like a sore thumb.  ;D

unsupported character
0
unsupported character
1
unsupported character
0
unsupported character
1




DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: rand() ... How many random numbers before cycle repeats
« Reply #14 on: January 02, 2017, 01:40:27 PM »
Here is an example in how the shuffle can give out a weak shuffle. I ran looking for 9 characters that were shuffled back to their original position and found the seed/key value of 379120 to shuffle 9 characters back to their original starting position.

Ran the Poor Key Finder to get the key of 379120
Code: [Select]
Poor Key Finder - Search for 9 or more matches
Searching through Key Values of 1 thru 1000000

      WARNING - Program Running Please Wait...

Greater or Equal to = 9 ===== 379120
100% Complete
Completed = 1000000


Press any key to continue . . .

Code: [Select]
#include <iostream>
#include <string>


int delay;
long long int counter1=0; // Add LL beyond 9 digits
long long int endcount=0; // while loop end counter
//long long int match=0;
//long long int nomatch=0;
long long int seed1=0;
int match2=0;
int ST=0;
int flag=0;
int progress=0; //Fixed float issue with 25% exact never found, removed float and placed int.
int step1=0;
int step2=0;
int step3=0;


int main ()
{
    system("color b0");
    std::cout<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
    std::cout<<"  Poor Key Finder Version 1.0\n";
    std::cout<<"      Build 01/30/2016\n";
    std::cout<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
    std::cout<<" Enter Starting Key Value\n";
    std::cin>>counter1;
    std::cout<<" Enter Ending Key Value\n";
    std::cin>>endcount;
    std::cout<<" Enter Duplicate Character Counter Value\n";
    std::cin>>flag;
    system("cls");

    std::string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;";
    std::string str2="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;";
    system("@echo. Started on %date% at %time%>>LogKey.txt");
    system("color f0");
    std::cout<<"Poor Key Finder - Search for "<<flag<<" or more matches \n";
    std::cout<<"Searching through Key Values of "<<counter1<<" thru "<<endcount<<"\n\n";
    std::cout<<"      WARNING - Program Running Please Wait...\n\n";
    while(counter1<=endcount){                     
    seed1=counter1;                           
    srand(seed1);
    random_shuffle(str.begin(), str.end()); // Shuffle the string
ST=0;
match2=0;

progress=((100*counter1)/endcount);


if(progress==25){
                 step1++;
                 if(step1==1){
                 std::cout<<"25% Complete\n";
                 }
                 else{}
                 }
                 else if(progress==50){
                 step2++;
                 if(step2==1){
                 std::cout<<"50% Complete\n";
                 }
                 else{}
                 }
                 else if(progress==75){
                 step3++;
                 if(step3==1){
                 std::cout<<"75% Complete\n";
                 }
                 else {}
                 }
                 else if(endcount==counter1){
                 std::cout<<"100% Complete\n";
                 }
                 else{
                      }
    while(ST<=85){
                  if(str[ST]==str2[ST]){
                                        match2++;
                                        }
                                        else{
                                             }
                                             ST++;
                                             }
                                             if(match2>=flag){                   
    std::cout<<"Greater or Equal to = "<<flag<<" ===== "<<seed1<<"\n";
}
else{
     }
       counter1++;
       }
       std::cout<<"Completed = "<<endcount<<"\n\n\n";
       system("@echo. Ended on %date% at %time%>>LogKey.txt");
       system("pause");
//std::cin>>delay;
  return 0;
}

Use the weak key in the old version of the crypto to get the crypt string key to copy paste that to word to line up that with the starting string in the screenshot below. Red is Bad below with characters that either never left their position or were shuffled back to their original position. My latest program uses the test of the poor key finder to allow you to know before use if a key is string enough or not.
Code: [Select]
Crypt Version 1.05

XXXXXXXXXXXXXXXXXXXXX
 Enter Integer Seed:
XXXXXXXXXXXXXXXXXXXXX

379120


Crypt String Key =

wLxDK8Ucfaj4nsVTINSM9oW!,pt_y:-;AYG%u/73m#q>z*^v$C.g1~H<X6&e\)P@OFkl?i(bQ=dZhJrB
5+20RE


Enter Info to Crypt in correct case
Close Window to Exit

Pic attached shows the results with 9 characters that are shuffled back to their original position with a single random_shuffle.

So the Worst Key Ever to use with this is out there, the long long int value that shuffles everything back to its original position to where everything is clear text, however with the key strength test before key use, thats not a problem because the program wont allow you to use that worst key ever or anything not matching the key strength.  ;D



[attachment deleted by admin to conserve space]