Пример #1
0
        public static byte[] CreateIV(string salt, string secretKey)
        {
            var saltBytes          = Convert.FromBase64String(salt);
            var secretKeyHashBytes = Utils.CreateMD5Hash(TextEncoding.GetBytes(secretKey));

            //calculate offset of secretKeyHashBytes by summ of
            //first secretKeyByte and first salt byte in proportion to 256
            var offset = (int)(((secretKeyHashBytes[0] + saltBytes[0]) / 256d) * 16);

            if (offset > 16)
            {
                offset -= 16;
            }
            var targetSecretKeyBytes = new byte[8];

            for (var i = 0; i < targetSecretKeyBytes.Length; i++)
            {
                var index = offset + i;
                if (index >= 8)
                {
                    index -= 8;
                }
                targetSecretKeyBytes[i] = secretKeyHashBytes[index];
            }
            var buff = new byte[16];
            var odd  = secretKeyHashBytes[0] > 123; //half byte

            for (var i = 0; i < buff.Length; i++)
            {
                buff[i] = odd ? targetSecretKeyBytes[i / 2] : saltBytes[i / 2];
                odd     = !odd;
            }

            return(buff);
        }