Hi,

I'm trying to create a program which provides a key strength. Visual studio don't show errors, but the program hangs on QuadWordFromBigEndian functions. Whats wrong?
Program stop workin on this line:

01.x = (           (((UInt64)block[0]) << 56) | (((UInt64)block[1]) << 48) |   
02.                  (((UInt64)block[2]) << 40) | (((UInt64)block[3]) << 32) |   
03.                  (((UInt64)block[4]) << 24) | (((UInt64)block[5]) << 16) |   
04.                  (((UInt64)block[6]) << 8) | ((UInt64)block[7])   
05.                  );

An show this error:

"Index was outside the bounds of the array."

But I do not know how to fix. What should be the array index.

Thanks

private void button1_Click(object sender, EventArgs e)
        {
            byte[] sec_key = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
            if (IsWeakKey(sec_key)) { label1.Text = "Yes"; }
            else { label1.Text = "No"; }
        }

        private static bool IsLegalKeySize(byte[] sec_key) { 
            if (sec_key.Length == 8) return(true); 
            return(false); 
        }

        private static UInt64 QuadWordFromBigEndian(byte[] block)
        {
            UInt64 x;
            x = (
                  (((UInt64)block[0]) << 56) | (((UInt64)block[1]) << 48) |
                  (((UInt64)block[2]) << 40) | (((UInt64)block[3]) << 32) |
                  (((UInt64)block[4]) << 24) | (((UInt64)block[5]) << 16) |
                  (((UInt64)block[6]) << 8) | ((UInt64)block[7])
                  );
            return (x);
        } 

        public static bool IsWeakKey(byte[] sec_key)
        {

            UInt64 key = QuadWordFromBigEndian(sec_key);
            if ((key == 0x0101010101010101) ||
                (key == 0xfefefefefefefefe) ||
                (key == 0x1f1f1f1f0e0e0e0e) ||
                (key == 0xe0e0e0e0f1f1f1f1))
            {
                return (true);
            }
            else
            {
                return (false);
            }
        }

Recommended Answers

All 2 Replies

Have you ensured the length of the data you're feeding to QuadWordFromBigEndian is 8 (or more) bytes?
...Either by forcing the call to IsLegalKeySize() or by running it in Debug mode with an assert...

private static UInt64 QuadWordFromBigEndian(byte[] block)
      {
         System.Diagnostics.Debug.Assert(block.Length.Equals(8));

         UInt64 x;
         x = (
               (((UInt64)block[0]) << 56) | (((UInt64)block[1]) << 48) |
               (((UInt64)block[2]) << 40) | (((UInt64)block[3]) << 32) |
               (((UInt64)block[4]) << 24) | (((UInt64)block[5]) << 16) |
               (((UInt64)block[6]) << 8)  | ((UInt64)block[7])
               );

         return (x);
      }

Then test it with something like this:

static void Main(string[] args)
      {
         byte[] block = {2,2,2,2,2,2,2,2};
         UInt64 x = QuadWordFromBigEndian(block);
         System.Diagnostics.Trace.WriteLine(x);
      }

Thanks, I did not know that the minutes should 8 bytes.

But if I put sec_key = "AAAAbbbb"; and "asdr34bf" i have answer
same. Why?
I sting convert to bytes[]. (byte[] sec_key = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.