Пример #1
0
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Models.crip crip = db.crips.Find(id);
            if (crip == 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(crip.Texto);

            // Decriptar
            using (ICryptoTransform decrypt = aes.CreateDecryptor())
            {
                byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                crip.Texto = Encoding.Unicode.GetString(dest);
            }
            return(View(crip));
        }
Пример #2
0
        public ActionResult Create(Models.crip crip)
        {
            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(crip.Texto);


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

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