示例#1
0
        /// <summary>
        /// 以EBC模式解密密文二进制数组
        /// </summary>
        /// <param name="cipher">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static byte[] DecryptEBC(this IBlockEncryption enc, byte[] cipher, byte[] key)
        {
            int BlockSize = enc.BlockSize;

            byte[] c           = (byte[])cipher.Clone();
            int    block_count = c.Length / BlockSize;

            for (int i = block_count - 1; i > 0; i--)
            {
                enc.Decryptor(c, i * BlockSize, key);
            }
            return(c);
        }
示例#2
0
        /// <summary>
        /// 以CBC模式解密密文二进制数组
        /// </summary>
        /// <param name="iv">初始向量。用于解密第一个数据块</param>
        /// <param name="cipher">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>解密后的数据块(不包括初始向量)</returns>
        public static byte[] DecryptCBC(this IBlockEncryption enc, byte[] iv, byte[] cipher, byte[] key)
        {
            int BlockSize = enc.BlockSize;

            byte[] c           = (byte[])cipher.Clone();
            int    block_count = c.Length / BlockSize;

            for (int i = block_count - 1; i >= 0; i--)
            {
                int this_block = i * BlockSize;
                int pre_block  = this_block - BlockSize;

                enc.Decryptor(c, this_block, key);
                if (i == 0)
                {
                    XorBlock(enc, iv, 0, c, this_block);
                }
                else
                {
                    XorBlock(enc, c, pre_block, c, this_block);
                }
            }
            return(c);
        }