Example #1
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="input">明文字节数组</param>
        /// <param name="key">秘钥</param>
        /// <param name="iv">初始化向量</param>
        /// <returns>密文字节数组</returns>
        public static byte[] Encrypt(byte[] inputBytes, byte[] key, byte[] iv)
        {
            if (inputBytes == null || inputBytes.Length <= 0)
            {
                throw new ArgumentNullException("inputBytes");
            }
            if (key == null || key.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            //创建AES管理工具
            using (AesManaged myAes = CreateAesManaged(key, iv))
            {
                //创建加密算法
                var encrypt = myAes.CreateEncryptor(key, iv);
                using (MemoryStream inputStream = new MemoryStream(inputBytes))
                {
                    Stream outStream = new MemoryStream();
                    try
                    {
                        CryptoTransform.TransformByRead(inputStream, encrypt, ref outStream);

                        return((outStream as MemoryStream).ToArray());
                    }
                    finally
                    {
                        if (outStream != null)
                        {
                            outStream.Close();
                        }
                    }
                }
            }
        }
Example #2
0
 /// <summary>
 /// 创建秘钥,无向量
 /// </summary>
 /// <returns></returns>
 public static String CreateKey()
 {
     return(CryptoTransform.CreateKey());
 }