Exemplo n.º 1
0
        /// <summary>
        /// AES256 암호화
        /// 원래 키도 파라미터로 있었으나 내부에 선언된 키를 무조건 사용하도록 강제하기 위해 파라미터에서 제외
        /// </summary>
        /// <param name="Input">암호화할 string</param>
        /// <returns>암호화된 값</returns>
        public static String Encrypt(string Input)
        {
            string result = string.Empty;

            try
            {
                RijndaelManaged aes = new RijndaelManaged();
                aes.KeySize   = 256;
                aes.BlockSize = 128;
                aes.Mode      = CipherMode.CBC;
                aes.Padding   = PaddingMode.PKCS7;
                aes.Key       = Encoding.UTF8.GetBytes(AesKey);
                aes.IV        = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                var    encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
                byte[] xBuff   = null;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
                    {
                        byte[] xXml = Encoding.UTF8.GetBytes(Input);
                        cs.Write(xXml, 0, xXml.Length);
                    }

                    xBuff = ms.ToArray();
                }

                result = Convert.ToBase64String(xBuff);
            }
            catch (Exception e)
            {
                string error = string.Format("CSecureUtil.Encrypt: input={0}|key={1}|exception={2}", Input, e.Message);
                CLog.debug(logger, error);
            }

            return(result);
        }