コード例 #1
0
        /// <summary>
        /// 以CBC模式加密明文二进制数组
        /// </summary>
        /// <param name="iv">输出本次加密使用的初始向量。算法会使用Randomizer成员初始化这个数组</param>
        /// <param name="plain">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的数据块(不包括初始向量)</returns>
        public static byte[] EncryptCBC(this IBlockEncryption enc, out byte[] iv, byte[] plain, byte[] key)
        {
            int BlockSize = enc.BlockSize;

            byte[] p = enc.Padder(plain);
            iv = new byte[BlockSize];
            enc.Randomizer(iv); Debug.Print(iv.ToHexString());

            int block_count = p.Length / BlockSize;

            for (int i = 0; i < block_count; i++)
            {
                int this_block = i * BlockSize;
                int pre_block  = this_block - BlockSize;

                if (i == 0)
                {
                    XorBlock(enc, iv, 0, p, this_block);
                }
                else
                {
                    XorBlock(enc, p, pre_block, p, this_block);
                }
                enc.Encryptor(p, this_block, key);
            }
            return(p);
        }
コード例 #2
0
        /// <summary>
        /// 以EBC模式加密明文二进制数组
        /// </summary>
        /// <param name="plain">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public static byte[] EncryptEBC(this IBlockEncryption enc, byte[] plain, byte[] key)
        {
            int BlockSize = enc.BlockSize;

            byte[] p           = enc.Padder(plain);
            int    block_count = p.Length / BlockSize;

            for (int i = 0; i < block_count; i++)
            {
                enc.Encryptor(p, i * BlockSize, key);
            }
            return(p);
        }