demo_encriptación_datos

Post on 28-Jan-2018

128 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Encriptación de datos

Asegurando nuestra información

Introducción

Las aplicaciones Web muy a menudo necesitan almacenar datos sensibles, como las conexiones a las bases de datos o las credenciales de una cuenta de usuario en archivos de configuración. Por razones de seguridad, este tipo de información nunca debe ser almacenada en texto plano y siempre debe ser cifrada antes de su almacenamiento.

Objetivos

En este módulo aprenderemos a: • Utilizar la clase librería que encapsula las

llamadas a la Data Protection API (DPAPI) contenida en “Crypt32.dll” para cifrar y descifrar datos.

• Cifrar las cadenas de conexión y almacenarlas en el Web.config

• Crear una aplicación Web para cifrar y descifrar datos.

Requerimientos

• Imports System.Text

• Imports Dataprotection

Las referencias a emplear son:

Referencia y uso de Crypt32.dll para DPAPI

Creación del proyecto Dataprotection que emplea la librería DPAPIWin32® Data Protection API (DPAPI).

Una de las caracteristicas de DPAPI es que maneja las llavesde cifrado por nosotros.

Pantalla de pruebas

Cifrando cadenasPrivate Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click Dim dp As New DataProtector(DataProtector.Store.USE_MACHINE_STORE) Try Dim dataToEncrypt() As Byte = Encoding.ASCII.GetBytes(txtDataToEncrypt.Text) txtEncryptedData.Text = Convert.ToBase64String(dp.Encrypt(dataToEncrypt, Nothing)) Catch ex As Exception lblError.ForeColor = Color.Red lblError.Text = "Exception.<br>" + ex.Message Return End Try lblError.Text = "" End Sub

Descifrando cadenas

Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click Dim dp As New DataProtector(DataProtector.Store.USE_MACHINE_STORE) Try Dim dataToDecrypt() As Byte = Convert.FromBase64String(txtEncryptedData.Text) txtDecryptedData.Text = Encoding.ASCII.GetString(dp.Decrypt(dataToDecrypt, Nothing)) Catch ex As Exception lblError.ForeColor = Color.Red lblError.Text = "Exception.<br>" + ex.Message Return End Try lblError.Text = "" End Sub

Descifrando valores establecidos en Config.Web

<appSettings>

<add key="connectionString" value="AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAn5RQ1aAJME2AMmEpBWkVlgQAAAACAAAAAAADZgAAqAAAABAAAACwMLkvl305PsddA94tM4E8AAAAAASAAACgAAAAEAAAAN+WvQ7eCbS3W3aZ1X8SEytIAAAAUwjfzcZvNVs4Bp439waR13/T5tWYSdh/4nvuC1NO/0JWFrmW1ve19U6GPjxzxJo6QhUWdW8g0267d/GuUpZXnYDWjuOl9gEnFAAAAA1H2b97Jaz4+YXpYthxSlDZXQLY" />

</appSettings>

En el archivo Web.Config

Descifrando valores establecidos en Config.Web

Private Sub btnDecryptConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecryptConfig.Click Dim dp As New DataProtector(DataProtector.Store.USE_MACHINE_STORE) Try Dim appSettingValue As String = ConfigurationSettings.AppSettings("connectionString") Dim dataToDecrypt() As Byte = Convert.FromBase64String(appSettingValue) txtDecryptedData.Text = Encoding.ASCII.GetString(dp.Decrypt(dataToDecrypt, Nothing)) Catch ex As Exception lblError.ForeColor = Color.Red lblError.Text = "Exception.<br>" + ex.Message Return End Try lblError.Text = "" End Sub

DEMO

http://localhost/DPAPIClientWeb/WebForm1.aspx

top related