Esempio n. 1
0
        /// <summary>
        /// AES  Encrypt include hex convert
        /// </summary>
        /// <param name="pToEncrypt">param to encrypt</param>
        /// <param name="isHex">convert hex</param>
        /// <returns>encrypted result</returns>
        /// ------------------------------------------------
        /// Change History:
        /// Date			Who		            Changes Made
        /// 2010-05-11		Lasko li		    Mark method
        ///-------------------------------------------------
        public string AesEncrypt(string pToEncrypt, bool isHex)
        {
            string str = "";

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, AesProvider.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    if (isHex)
                    {
                        byte[]        inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                        StringBuilder ret            = new StringBuilder();
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        foreach (byte b in ms.ToArray())
                        {
                            ret.AppendFormat("{0:X2}", b);
                        }
                        str = ret.ToString();
                    }
                    else
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(pToEncrypt);
                            sw.Flush();
                            cs.FlushFinalBlock();


                            str = Convert.ToBase64String(ms.ToArray());
                        }
                    }
                }
            }
            return(str);
        }