internal static byte[] _encrypt(ref UInt64[] state, byte[] payload, byte rounds)
        {
            // absorb header, payload, or footer with domain separation constant
            if (payload == null || payload.LongLength == 0)
            {
                return(null);
            }
            byte[] output = new byte[payload.Length];

            UInt64[] state_buffer = new UInt64[NORX_RateSizeBytes / sizeof(UInt64)];
            for (int i = 0; i < payload.Length + 1; i += NORX_RateSizeBytes) // the length + 1 ensures the FOR loop runs one additional time
            {
                state_buffer.Initialize();
                if (i + NORX_RateSizeBytes >= payload.Length)
                {
                    byte[] LastBlock = new byte[NORX_RateSizeBytes];
                    if (i < payload.Length)
                    {
                        Buffer.BlockCopy(payload, i, LastBlock, 0, payload.Length % NORX_RateSizeBytes);
                    }
                    LastBlock[payload.Length % NORX_RateSizeBytes] = 0x01;
                    LastBlock[LastBlock.Length - 1] |= 0x80;
                    Buffer.BlockCopy(LastBlock, 0, state_buffer, 0, LastBlock.Length);
                }
                else
                {
                    Buffer.BlockCopy(payload, i, state_buffer, 0, NORX_RateSizeBytes);
                }
                for (int j = 0; j < state_buffer.Length; j++)
                {
                    state[j] ^= state_buffer[j];
                }
                ;
                if (i + NORX_RateSizeBytes >= payload.Length)
                {
                    Buffer.BlockCopy(state, 0, output, i, payload.Length % NORX_RateSizeBytes);
                }
                else
                {
                    Buffer.BlockCopy(state, 0, output, i, NORX_RateSizeBytes);
                }
            }
            return(output);
        }
        internal static void _absorb(ref UInt64[] state, byte[] payload, byte domain, byte rounds)
        {
            // absorb header, payload, or footer with domain separation constant
            if (payload.LongLength == 0)
            {
                return;
            }

            UInt64[] state_buffer = new UInt64[NORX_RateSizeBytes / sizeof(UInt64)];
            for (int i = 0; i < payload.Length + 1; i += NORX_RateSizeBytes) // the length + 1 ensures the FOR loop runs one additional time
            {
                state[15] ^= (UInt64)domain;
                _F(ref state, rounds);
                state_buffer.Initialize();
                if (i + NORX_RateSizeBytes >= payload.Length)
                {
                    byte[] LastBlock = new byte[NORX_RateSizeBytes];
                    if (i < payload.Length)
                    {
                        Buffer.BlockCopy(payload, i, LastBlock, 0, payload.Length % NORX_RateSizeBytes);
                    }
                    LastBlock[payload.Length % NORX_RateSizeBytes] = 0x01;
                    LastBlock[LastBlock.Length - 1] |= 0x80;
                    Buffer.BlockCopy(LastBlock, 0, state_buffer, 0, LastBlock.Length);
                }
                else
                {
                    Buffer.BlockCopy(payload, i, state_buffer, 0, NORX_RateSizeBytes);
                }
                for (int j = 0; j < state_buffer.Length; j++)
                {
                    state[j] ^= state_buffer[j];
                }
                ;
            }
        }
Exemple #3
0
        /// <summary>
        /// 使用传统OTSU算法寻找二值化阈值
        /// </summary>
        /// <param name="OriginalImage">原图(灰度图)</param>
        /// <param name="Rate_Reduction">降维系数</param>
        /// <returns>找到的阈值</returns>
        public UInt16 FindThreshold_OTSUNormal(Bitmap OriginalImage, int Rate_Reduction)
        {
            UInt16 Threshold = 0;

            int Image_Width  = OriginalImage.Width;
            int Image_Height = OriginalImage.Height;

            const int GrayScale = 256;                                                          //单通道图像总灰度256级

            UInt64[] pixCount = new UInt64[GrayScale];                                          //每个灰度值所占像素个数
            double   pixSum   = Convert.ToDouble(Image_Width) * Convert.ToDouble(Image_Height); //图像总像素点

            double[] pixPro = new double[GrayScale];                                            //每个灰度值所占总像素比例

            pixCount.Initialize();
            pixPro.Initialize();

            //统计每个灰度级中像素的个数
            for (int i = 0; i < Image_Width; i += Rate_Reduction)
            {
                for (int j = 0; j < Image_Height; j += Rate_Reduction)
                {
                    int Pixel = OriginalImage.GetPixel(i, j).R;
                    pixCount[Pixel]++;
                }
            }

            //计算每个像素在整幅图像中的比例
            for (int i = 0; i < GrayScale; i++)
            {
                pixPro[i] = Convert.ToDouble(pixCount[i]) / pixSum;
            }

            double deltaMax = 0;

            //遍历所有从0到255灰度级的阈值分割条件,测试哪一个的类间方差最大
            for (int i = 0; i < GrayScale; i++)
            {
                double w0       = 0;
                double w1       = 0;
                double u0tmp    = 0;
                double u1tmp    = 0;
                double u0       = 0;
                double u1       = 0;
                double deltaTmp = 0;

                for (int j = 0; j < GrayScale; j++)
                {
                    if (j <= i)//背景
                    {
                        w0    += pixPro[j];
                        u0tmp += j * pixPro[j];
                    }
                    else//前景
                    {
                        w1    += pixPro[j];
                        u1tmp += j * pixPro[j];
                    }
                }
                u0       = u0tmp / w0;
                u1       = u1tmp / w1;
                deltaTmp = Convert.ToDouble((w0 * w1 * (u0 - u1) * (u0 - u1))); //类间方差公式 g = w1 * w2 * (u1 - u2) ^ 2
                if (deltaTmp > deltaMax)
                {
                    deltaMax  = deltaTmp;
                    Threshold = Convert.ToUInt16(i);
                }
            }

            return(Threshold);
        }