简体   繁体   中英

How to put a specific byte from a byte array into a single byte

I have here a code where i set up a byte[1] to fill with a random byte which i then need to have this random generated byte from array [0] into a single byte to be able to compare it. (x is either 16 or 32, z is always staring with 0)

byte compareByte = 0x00;
byte[] rndByte = new byte[1];
byte[] buffer = new byte[x];
Random rnd = new Random();

for (int i = 0; i < dalmatinerRound.Length; i++)
{
    while (z != x)
    {
        Application.DoEvents();
        rnd.NextBytes(rndByte);
        compareByte = (byte) rndByte[0];

        if (compareByte == dalmatinerRound[i])
        {
            buffer[z] = compareByte;
            z++;

            if (z == x)
            {
                string str = Encoding.ASCII.GetString(buffer);
                textPass.Text = str;
            }
        }
    }
}

The problem is that compareByte is everytime "A". Regardless of how often i trie. Or even if i use the random byte to compare like:

if (rndByte[0] == dalmatinerRound[i])

it also returns "A". I can't get the byte from offset 0x00 of the array into a single byte. But when i do some test and use:

string str = Encoding.ASCII.GetString(rndByte);
textPass.Text = str;

then it works and i get everytime a other letter. To be more clear. This code will generate a Random passwort in length of 16 or 32 diggis. The dalmatinerRound is a Array of bytes in length of 101 contaning Alphabetical letters, lower and upper case, 0-9 and also !"§$%&/()=?*+}][{

thanks

Why not just use (byte)rnd.Next(0, 256)? In any case, rnd.NextBytes works just fine, and you get the first byte by using rndByte[0] just the way you did. In other words, the error must be somewhere else, not in the random generator or reading the byte value. Is this really the code, or have you made some changes? In any case, your code seems incredibly complicated and wasteful. You should probably just use your array of allowable values (no need to have it a byte array, chars are more useful) and use rnd.Next(0, dalmatinerRound.Length); to get a random allowed character.

What your code actually does is that it loops until you get a "random" byte... which is equal to (byte)'A'. Your loops are all wrong. Instead, you can use this:

StringBuilder pwd = new StringBuilder(wantedLength);

for (var i = 0; i < wantedLength; i++)
   pwd.Append(dalmatinerRound[rnd.Next(0, dalmatinerRound.Length)]);

And there you have your random password :)

This expects that dalmatinerRound is an array of strings, which is quite useful anyway, so you should do that.

You're looping over the array of eligible characters, I assume starting "ABC...", and for each of those doing something. With your while loop you're trying to generate a count of x bytes in your buffer, but you don't stop until this is done - you never get a chance to increment i until you finish filling up the buffer.

You're also generating a random byte and only adding it to your buffer if it happens to be the current "candidate" character in dalmatinerRound . So the buffer sloooowly fills up with "A" characters. Then once this is full, the next time i is incremented, the while loop immediately exits, so no other characters are tried.

You should instead loop over the character index i in the target buffer , generating one random character in each iteration - just think how you would go about this process by hand.

It looks like your for loop is in the wrong place. Currently, the whole password is generated on the first (and presumably the only) iteration of the for loop. That means your comparison only ever compares the random byte to the first entry in dalmatinerRound , which is presumably the letter A.

You need to put the for loop inside the while loop, so that each random byte gets compared to every element of dalmatinerRound . (Make sure you put the loop after you generate the random byte!)

As a side note, there are much better ways of generating what you need. Since you have an array of all valid characters, you could just pick a random element from that to get a password digit (ie generate a random number between 0 and the length of the array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM