コード例 #1
0
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Models.Encriptar encriptar = db.Encriptars.Find(id);
            if (encriptar == null)
            {
                return(HttpNotFound());
            }

            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

            aes.BlockSize = 128;
            aes.KeySize   = 256;
            aes.IV        = Encoding.UTF8.GetBytes(AesIV256BD);
            aes.Key       = Encoding.UTF8.GetBytes(AesKey256BD);
            aes.Mode      = CipherMode.CBC;
            aes.Padding   = PaddingMode.PKCS7;

            // Converter a String para um byte array 64 bists
            byte[] src = Convert.FromBase64String(encriptar.Txt);

            // Decriptar
            using (ICryptoTransform decrypt = aes.CreateDecryptor())
            {
                byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                encriptar.Txt = Encoding.Unicode.GetString(dest);
            }
            return(View(encriptar));
        }
コード例 #2
0
        public ActionResult Create(Models.Encriptar encriptar)
        {
            if (ModelState.IsValid)
            {
                //AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize   = 256;
                aes.IV        = Encoding.UTF8.GetBytes(AesIV256BD);
                aes.Key       = Encoding.UTF8.GetBytes(AesKey256BD);
                aes.Mode      = CipherMode.CBC;
                aes.Padding   = PaddingMode.PKCS7;

                //Convertendo string para byte array
                byte[] src = Encoding.Unicode.GetBytes(encriptar.Txt);


                //Encriptação
                using (ICryptoTransform encrypt = aes.CreateEncryptor())
                {
                    byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

                    //Converte byte array para string de base 64
                    encriptar.Txt = Convert.ToBase64String(dest);
                }
                db.Encriptars.Add(encriptar);
                db.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(encriptar));
        }