Example #1
0
        public IActionResult GetTimeBound()
        {
            var protectorTimeBound = _protector.ToTimeLimitedDataProtector();

            string plainText     = "Nattawat Getwichit";
            string encryptedText = protectorTimeBound.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));
            string decryptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(new { plainText, encryptedText, decryptedText }));
        }
        public IActionResult Index()
        {
            var products = _productSeed.Products.ToList();

            var timeLimitedProtector = _dataProtector.ToTimeLimitedDataProtector();

            products.ForEach(x =>
            {
                //x.EncrypedId = _dataProtector.Protect(x.Id.ToString());
                x.EncrypedId = timeLimitedProtector.Protect(x.Id.ToString(), TimeSpan.FromSeconds(5));
                x.Id         = null;
            });
            return(View(products));
        }
        // GET: Products
        public async Task <IActionResult> Index()
        {
            var products = await _context.Product.ToListAsync();

            var timeLimitedProtector = _dataProtector.ToTimeLimitedDataProtector();

            products.ForEach(x =>
            {
                x.EncryptedId = timeLimitedProtector.Protect(x.Id.ToString(), TimeSpan.FromSeconds(5));
            });


            return(View(products));
        }
 public ValuesController(IDataProtectionProvider provider, IKeyManager keyManager)
 {
     _provider                 = provider;
     _dataProtector            = provider.CreateProtector("3BCE558E2AD3E0E34A7743EAB5AEA2A9BD2575A0");
     _timeLimitedDataProtector = _dataProtector.ToTimeLimitedDataProtector();
     _keyManager               = keyManager;
 }
Example #5
0
 private void EjemploDeEncriptacionLimitadaPorTiempo()
 {
     var    protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();
     string textoPlano         = "Felipe Gavilán";
     string textoCifrado       = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));
     string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);
 }
 private void EjemploDeEncriptacionLimitadaPorTiempo()
 {
     //indicar que va a estar encriptado por tiempo
     var    protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();
     string textoPlano = "Felipe Gavilán";
     //encriptar y asignar un tiempo de encriptacion
     string textoCifrado       = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));
     string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);
 }
        public ActionResult <IEnumerable <string> > EjemploDeEncriptacionLimitadaPorTiempo(int id)
        {
            var    protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();
            string textoPlano         = "Felipe Gavilán";
            string textoCifrado       = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));
            string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);

            return(Ok(new { textoPlano, textoCifrado, textoDesencriptado, id }));
        }
Example #8
0
        public ParamsProtectionResourceFilter(IDataProtectionProvider protectionProvider, ILogger <ParamsProtectionResourceFilter> logger, IOptions <ParamsProtectionOptions> options)
        {
            _option    = options.Value;
            _protector = protectionProvider.CreateProtector(_option.ProtectorPurpose ?? ParamsProtectionHelper.DefaultPurpose);
            if (_option.ExpiresIn.GetValueOrDefault(0) > 0)
            {
                _protector = _protector.ToTimeLimitedDataProtector();
            }

            _logger = logger;
        }
        public async Task <IActionResult> GetTimeBound()
        {
            var    protectorTimeBound = _protector.ToTimeLimitedDataProtector();
            string plaintext          = "Oluwaseyi Ademola";
            string encryptedText      = protectorTimeBound.Protect(plaintext, lifetime: TimeSpan.FromSeconds(5));
            await Task.Delay(6000);

            string decryptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(new { plaintext, encryptedText, decryptedText }));
        }
Example #10
0
        //only for the given time you are allowed to decrypt the text
        public async Task <IActionResult> GetTimeBound()
        {
            var protectorTimeBound = _protector.ToTimeLimitedDataProtector();

            string plainText     = "Vivek Arya";
            string encryptedText = protectorTimeBound.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));
            await Task.Delay(6000);//we are delaying 6 seconds to check if we'll be allowed to decrypt the text after 6 seconds or not

            string decryptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(new { plainText, encryptedText, decryptedText }));
        }
        public ActionResult <string> ObtenerCadenaEncriptada()
        {
            var protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();

            string textoPlano   = "Richard Negron";
            string textoCifrado = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));

            Thread.Sleep(6000);
            string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);

            return(Ok(new { textoPlano, textoCifrado, textoDesencriptado }));
        }
Example #12
0
        public ActionResult <string> Get(int id)
        {
            var    protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();
            string textoPlano = "Carlos";
            //string textoCifrado = _protector.Protect(textoPlano, TimeSpan);
            string textoCifrado = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));

            Thread.Sleep(6000);
            string textoDesencriptado = _protector.Unprotect(textoCifrado);

            return(Ok(new { textoPlano, textoCifrado, textoDesencriptado }));
        }
        private readonly ITimeLimitedDataProtector _timeLimitedDataProtector;                          //Şifrelediğimiz veriye ömür biçmek için kullanırız.

        public ProductController(AppDbContext context, IDataProtectionProvider dataProtectionProvider) //IDataProtectionProvider ile IDataProtector'ı dolduracağız.
        {
            _context = context;

            /*
             * CreateProtector içerisinde vereceğim isim Unique'dir. DataProtector'ları birbirinden ayırmak için kullanırız.
             * Farklı bir controller içerisinde de DataProtector kullanabileceğimizden ötürü, bunları birbirinden ayırmak mahiyetinde isimlendirme yapıyoruz.
             */
            _dataProtector            = dataProtectionProvider.CreateProtector(nameof(ProductController));
            _timeLimitedDataProtector = _dataProtector.ToTimeLimitedDataProtector(); //Şifrelediğimiz veriye ömür biçmek için kullanırız.

            //_dataProtector = dataProtectionProvider.CreateProtector(GetType().FullName); | GetType().FullName ile otomatik olarak da isimlendirebiliriz.
        }
Example #14
0
        public async Task <IActionResult> GetTimebound()
        {
            var protectorTimeBound = _protector.ToTimeLimitedDataProtector();

            string plainText     = "Thomas Margraff";
            string encryptedText = protectorTimeBound.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));
            await Task.Delay(6000);

            // throws exception because timeout (5 seconds) expired
            string decryptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(new { plainText, encryptedText, decryptedText }));
        }
Example #15
0
        public async Task <IActionResult> GetTimeBound()
        {
            var protectorTimeBound = _protector.ToTimeLimitedDataProtector();

            string plainText     = "Dimitris Kosmas";
            string encryptedText = protectorTimeBound.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));

            //For test purposes only
            //await Task.Delay(6000);

            string decreptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(new { plainText, encryptedText, decreptedText }));
        }
Example #16
0
        public ActionResult <string> GetEncriptacionTime()
        {
            string textoPlano = "name";

            //delimitado por 5 segundos
            var    protectorLimitadoPorTiempo = _protector.ToTimeLimitedDataProtector();
            string textoCifrado = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));

            //espera 6s
            Thread.Sleep(6000);
            string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);

            return(Ok(new { textoPlano, textoCifrado, textoDesencriptado }));
        }
        public ActionResult EncriptarPorTiempo()
        {
            var protectorLimitadoPorTiempo = dataProtector.ToTimeLimitedDataProtector();

            var textoPlano   = "Felipe Gavilán";
            var textoCifrado = protectorLimitadoPorTiempo.Protect(textoPlano, lifetime: TimeSpan.FromSeconds(5));

            Thread.Sleep(6000);
            var textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);

            return(Ok(new
            {
                textoPlano = textoPlano,
                textoCifrado = textoCifrado,
                textoDesencriptado = textoDesencriptado
            }));
        }
        // [ResponseCache(Duration = 15)]
        public ActionResult <object> Get()
        {
            var protectorLimitadoPorTiempo = protector.ToTimeLimitedDataProtector();

            // Proteccion limitada por tiempo
            string textoPlano   = "Nelson Marro";
            string textoCifrado = protectorLimitadoPorTiempo.Protect(textoPlano, TimeSpan.FromSeconds(5));

            Thread.Sleep(6000);
            string textoDesencriptado = protectorLimitadoPorTiempo.Unprotect(textoCifrado);

            // Proteccion normal
            // string textoPlano = "Nelson Marro";
            // string textoCifrado = protector.Protect(textoPlano);
            // string textoDesencriptado = protector.Unprotect(textoCifrado);
            return(Ok(new { textoPlano, textoCifrado, textoDesencriptado }));
        }
Example #19
0
        public ActionResult <string> Get(int id)
        {
            //This is an example how we can use IDataProtectionProvider and IDataProtector classes

            //this var allow to create a time scope
            var protectorLimit = _protector.ToTimeLimitedDataProtector();


            string text    = "Carlos Fabregas";
            string textEnc = protectorLimit.Protect(text, TimeSpan.FromSeconds(5));
            //Stop the Process for o seconds
            // Thread.Sleep(6000);
            string text1 = protectorLimit.Unprotect(textEnc);

            //string textEnc = _protector.Protect(text);
            //string text1 = _protector.Unprotect(textEnc);
            return(Ok(new { text, textEnc, text1 }));
        }
Example #20
0
        public async Task <IActionResult> GetTimeBound()
        {
            var limitedDataProtector = _protector.ToTimeLimitedDataProtector();
            //متنی که میخواهیم رمز گذاری کنیم
            var plainText = "Ali Chavoshi";
            //lifeTime=عمر این رمز گذاری چقدر است
            //عمر مفید 5 ثانیه بود
            var encryptedText = limitedDataProtector.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));

            //گفتم بعد از 6 ثانیه خروجی را نشان بده که ببینم بعد از 5 ثانیه معتبر هست؟؟
            //باید قبل از اینکه رمز گشایی کنیم 6 ثانیه صبر کنیم
            await Task.Delay(6000);


            string decryptedText = limitedDataProtector.Unprotect(encryptedText);

            return(Ok(new { plainText, encryptedText, decryptedText }));
        }
        public ActionResult <string> Get(int id)
        {
            var protectorlimitedbytime = _protector.ToTimeLimitedDataProtector();

            string textoplano = "douglasLoaiza ";
            //Encript the textoplano
            //string textocifrado = _protector.Protect(textoplano);
            //encript by time
            string textocifrado = protectorlimitedbytime.Protect(textoplano, TimeSpan.FromSeconds(5));

            //Encript textoplano
            //string textodesncriptado = _protector.Unprotect(textocifrado);
            //Encript by time
            Thread.Sleep(6000);
            string textodesncriptado = protectorlimitedbytime.Unprotect(textocifrado);

            return(Ok(new { textoplano, textocifrado, textodesncriptado }));
        }
        public async Task <ActionResult> TimeBound()
        {
            // protectorTimeBound se le pone limite al tiempo de cifrado
            var protectorTimeBound = protectionProvider.ToTimeLimitedDataProtector();

            string plainText     = "DavidJuajinoy";
            string encryptedText = protectorTimeBound.Protect(plainText, lifetime: TimeSpan.FromSeconds(5));
            await Task.Delay(6000);

            string decryptedText = protectorTimeBound.Unprotect(encryptedText);

            return(Ok(
                       new
            {
                plainText = plainText,
                encryptedText = encryptedText,
                decryptedText = decryptedText
            }
                       ));
        }
 public DataProtectorService(IDataProtectionProvider provider)
 {
     _dataProtector            = provider.CreateProtector(GetType().FullName);
     _timeLimitedDataProtector = _dataProtector.ToTimeLimitedDataProtector();
 }
 // the 'provider' parameter is provided by DI
 public DataProtectionHelper(IDataProtectionProvider provider)
 {
     _protector            = provider.CreateProtector("Contoso.democlass.v1");
     _timeLimitedProtector = _protector.ToTimeLimitedDataProtector();
 }
Example #25
0
 /// <summary>
 /// Encrypts a string.
 /// </summary>
 /// <param name="plaintext">The string to encrypt.</param>
 /// <param name="expiration">The expiration of the protected string.</param>
 /// <returns>Returns an encrypted string representation.</returns>
 public string Encrypt(string plaintext, DateTimeOffset expiration)
 {
     return(_protector.ToTimeLimitedDataProtector().Protect(plaintext, expiration));
 }
Example #26
0
    public byte[] Encrypt(byte[] array, TimeSpan time)
    {
        var protectorTimeLimit = protector.ToTimeLimitedDataProtector();

        return(protectorTimeLimit.Protect(array, time));
    }