Está en la página 1de 11

Crear aplicaciones ASP.

NET
seguras
Autenticacin, autorizacin y comunicacin
segura
Consulte la Pgina de entrada como punto de partida y para obtener una descripcin
completa del documento Crear aplicaciones ASP.NET seguras.
Resumen
En este artculo se muestra cmo crear una biblioteca de clases administradas para
proporcionar funcionalidad de cifrado para las aplicaciones. Permite que una aplicacin
elija el algoritmo de cifrado. Entre los algoritmos admitidos se incluyen DES, riple DES,
!C" y !ijndael.
Cmo: Crear una biblioteca de cifrado
En este artculo se describe cmo crear una biblioteca de cifrado gen#rica que se puede
utili$ar para cifrar y descifrar datos con los siguientes algoritmos%
DES &Digital Encryption Standard, Estndar de cifrado digital'
riple DES
!ijndael
!C"
Si desea (er un ejemplo de aplicacin en la que se utili$a la biblioteca de clases que se
crea en este artculo, consulte )Cmo% *lmacenar cadenas de cone+in cifradas en el
!egistro) en la seccin !eferencia de esta gua.
Requisitos
* continuacin se describen las recomendaciones de ,ard-are, soft-are, infraestructura
de red, conocimientos y Ser(ice Pac. que se necesitan.
Sistema operati(o /icrosoft0 1indo-s0 "222
Sistema de desarrollo /icrosoft 3isual Studio0 .4E

Para lle(ar a cabo los procedimientos de este artculo, tambi#n es necesario que tenga
conocimientos de la ,erramienta de desarrollo /icrosoft 3isual C56.
Resumen
En este artculo se incluyen los siguientes procedimientos%
7. Crear una biblioteca de clases en C5
". Crear una aplicacin de prueba de consola

7
1. Crear una biblioteca de clases en C#
En este procedimiento se crea una biblioteca de clases en C5 que proporcionar
funcionalidad de cifrado y descifrado.
Para crear una biblioteca de clases en C#
7. 8nicie 3isual Studio .4E y cree un nue(o proyecto de biblioteca de clases de C5
con el nombre Encryption.
". 9tilice el E+plorador de soluciones para cambiar el nombre del arc,i(o class7.cs
por Encryptransformer.cs.
:. En el arc,i(o Encryptransformer.cs, cambie el nombre de Class1 por
EncryptTransformer.
;. Cambie el mbito de la clase de public a internal.
internal class EncryptTransformer
<. *gregue la siguiente instruccin using al principio del arc,i(o.
using System.Security.Cryptography;
=. *gregue el siguiente tipo enumerado en el espacio de nombres Encryption.
public enum EncryptionAlgorithm {Des = 1, Rc2, Rijndael, TripleDes};
>. *gregue a la clase EncryptTransformer las siguientes (ariables miembro
pri(adas.
private EncryptionAlgorithm algorithmID;
private byte[] initVec;
private byte[] encKey;
?. !eemplace el constructor predeterminado por el siguiente constructor.
internal EncryptTransformer(EncryptionAlgorithm algId)
{
//Save the algorithm being used.
algorithmID = algId;
}
@. *gregue a la clase el m#todo siguiente.
internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey)
{
// Pick the provider.
switch (algorithmID)
{
case EncryptionAlgorithm.Des:
{
DES des = new DESCryptoServiceProvider();
"
des.Mode = CipherMode.CBC;
// See if a key was provided
if (null == bytesKey)
{
encKey = des.Key;
}
else
{
des.Key = bytesKey;
encKey = des.Key;
}
// See if the client provided an initialization vector
if (null == initVec)
{ // Have the algorithm create one
initVec = des.IV;
}
else
{ //No, give it to the algorithm
des.IV = initVec;
}
return des.CreateEncryptor();
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES des3 = new TripleDESCryptoServiceProvider();
des3.Mode = CipherMode.CBC;
// See if a key was provided
if (null == bytesKey)
{
encKey = des3.Key;
}
else
{
des3.Key = bytesKey;
encKey = des3.Key;
}
// See if the client provided an IV
if (null == initVec)
{ //Yes, have the alg create one
initVec = des3.IV;
}
else
{ //No, give it to the alg.
des3.IV = initVec;
}
return des3.CreateEncryptor();
}
case EncryptionAlgorithm.Rc2:
:
{
RC2 rc2 = new RC2CryptoServiceProvider();
rc2.Mode = CipherMode.CBC;
// Test to see if a key was provided
if (null == bytesKey)
{
encKey = rc2.Key;
}
else
{
rc2.Key = bytesKey;
encKey = rc2.Key;
}
// See if the client provided an IV
if (null == initVec)
{ //Yes, have the alg create one
initVec = rc2.IV;
}
else
{ //No, give it to the alg.
rc2.IV = initVec;
}
return rc2.CreateEncryptor();
}
case EncryptionAlgorithm.Rijndael:
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
// Test to see if a key was provided
if(null == bytesKey)
{
encKey = rijndael.Key;
}
else
{
rijndael.Key = bytesKey;
encKey = rijndael.Key;
}
// See if the client provided an IV
if(null == initVec)
{ //Yes, have the alg create one
initVec = rijndael.IV;
}
else
{ //No, give it to the alg.
rijndael.IV = initVec;
}
return rijndael.CreateEncryptor();
}
;
default:
{
throw new CryptographicException("Algorithm ID '" + algorithmID +
"' not supported.");
}
}
}
72. *gregue a la clase las siguientes propiedades.
internal byte[] IV
{
get{return initVec;}
set{initVec = value;}
}
internal byte[] Key
{
get{return encKey;}
}
77. *gregue al proyecto una nue(a clase con el nombre DecryptTransformer.
7". *gregue la siguiente instruccin using al principio del arc,i(o
Decryptransformer.cs.
using System.Security.Cryptography;
7:. Cambie el mbito de la clase de public a internal.
7;. !eemplace el constructor predeterminado por el siguiente constructor.
internal DecryptTransformer(EncryptionAlgorithm deCryptId)
{
algorithmID = deCryptId;
}
7<. *gregue a la clase las siguientes (ariables pri(adas.
private EncryptionAlgorithm algorithmID;
private byte[] initVec;
7=. *gregue a la clase el m#todo siguiente.
internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey)
{
// Pick the provider.
switch (algorithmID)
{
case EncryptionAlgorithm.Des:
{
<
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
des.Key = bytesKey;
des.IV = initVec;
return des.CreateDecryptor();
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES des3 = new TripleDESCryptoServiceProvider();
des3.Mode = CipherMode.CBC;
return des3.CreateDecryptor(bytesKey, initVec);
}
case EncryptionAlgorithm.Rc2:
{
RC2 rc2 = new RC2CryptoServiceProvider();
rc2.Mode = CipherMode.CBC;
return rc2.CreateDecryptor(bytesKey, initVec);
}
case EncryptionAlgorithm.Rijndael:
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
return rijndael.CreateDecryptor(bytesKey, initVec);
}
default:
{
throw new CryptographicException("Algorithm ID '" + algorithmID +
"' not supported.");
}
}
} //end GetCryptoServiceProvider
7>. *gregue a la clase la siguiente propiedad.
internal byte[] IV
{
set{initVec = value;}
}
7?. *gregue al proyecto una nue(a clase con el nombre Encryptor.
7@. *gregue las siguientes instrucciones using al principio del arc,i(o Encryptor.cs.
using System.Security.Cryptography;
using System.IO;
"2. !eemplace el constructor predeterminado por el siguiente constructor.
public Encryptor(EncryptionAlgorithm algId)
{
=
transformer = new EncryptTransformer(algId);
}
"7. *gregue a la clase las siguientes (ariables miembro pri(adas.
private EncryptTransformer transformer;
private byte[] initVec;
private byte[] encKey;
"". *gregue a la clase el siguiente m#todo Encrypt.
public byte[] Encrypt(byte[] bytesData, byte[] bytesKey)
{
//Set up the stream that will hold the encrypted data.
MemoryStream memStreamEncryptedData = new MemoryStream();
transformer.IV = initVec;
ICryptoTransform transform = transformer.GetCryptoServiceProvider(bytesKey);
CryptoStream encStream = new CryptoStream(memStreamEncryptedData,
transform,
CryptoStreamMode.Write);
try
{
//Encrypt the data, write it to the memory stream.
encStream.Write(bytesData, 0, bytesData.Length);
}
catch(Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream: \n"
+ ex.Message);
}
//Set the IV and key for the client to retrieve
encKey = transformer.Key;
initVec = transformer.IV;
encStream.FlushFinalBlock();
encStream.Close();
//Send the data back.
return memStreamEncryptedData.ToArray();
}//end Encrypt
":. *gregue a la clase las siguientes propiedades.
public byte[] IV
{
get{return initVec;}
set{initVec = value;}
}
>
public byte[] Key
{
get{return encKey;}
}
";. *gregue al proyecto una nue(a clase con el nombre Decryptor.
"<. *gregue las siguientes instrucciones using al principio del arc,i(o Decryptor.cs.
using System.Security.Cryptography;
using System.IO;
"=. !eemplace el constructor predeterminado por el siguiente constructor.
public Decryptor(EncryptionAlgorithm algId)
{
transformer = new DecryptTransformer(algId);
}
">. *gregue a la clase las siguientes (ariables miembro pri(adas.
private DecryptTransformer transformer;
private byte[] initVec;
"?. *gregue a la clase el siguiente m#todo Decrypt.
public byte[] Decrypt(byte[] bytesData, byte[] bytesKey)
{
//Set up the memory stream for the decrypted data.
MemoryStream memStreamDecryptedData = new MemoryStream();
//Pass in the initialization vector.
transformer.IV = initVec;
ICryptoTransform transform = transformer.GetCryptoServiceProvider(bytesKey);
CryptoStream decStream = new CryptoStream(memStreamDecryptedData,
transform,
CryptoStreamMode.Write);
try
{
decStream.Write(bytesData, 0, bytesData.Length);
}
catch(Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream: \n"
+ ex.Message);
}
decStream.FlushFinalBlock();
decStream.Close();
// Send the data back.
?
return memStreamDecryptedData.ToArray();
} //end Decrypt
"@. *gregue a la clase la siguiente propiedad.
public byte[] IV
{
set{initVec = value;}
}
:2. En el menA enerar, ,aga clic en enerar solucin.

!. Crear una aplicacin de prueba de consola
En este procedimiento se crea una sencilla aplicacin de prueba de consola para probar
la funcionalidad de cifrado y descifrado.
Para crear una aplicacin de prueba de consola
7. *gregue a la solucin actual una nue(a aplicacin de consola de C5 con el
nombre EncryptionTester.
". En el E+plorador de soluciones, ,aga clic con el botn secundario del mouse
&ratn' en el proyecto EncryptionTester y, a continuacin, ,aga clic en
Establecer como proyecto de inicio.
:. 9tilice el E+plorador de soluciones para cambiar el nombre del arc,i(o class7.cs
por Encryptionest.cs.
;. En el arc,i(o Encryptionest.cs, cambie el nombre de Class1 por EncryptionTest.
<. *gregue una referencia de proyecto al proyecto Encryption.
=. *gregue las siguientes instrucciones using al principio del arc,i(o
Encryptionest.cs.
using System.Text;
using Encryption;
>. *gregue el siguiente cdigo al m#todo "ain.
// Set the required algorithm
EncryptionAlgorithm algorithm = EncryptionAlgorithm.Des;
// Init variables.
byte[] IV = null;
byte[] cipherText = null;
byte[] key = null;
try
{ //Try to encrypt.
//Create the encryptor.
Encryptor enc = new Encryptor(EncryptionAlgorithm.Des);
byte[] plainText = Encoding.ASCII.GetBytes("Test String");
@
if ((EncryptionAlgorithm.TripleDes == algorithm) ||
(EncryptionAlgorithm.Rijndael == algorithm))
{ //3Des only work with a 16 or 24 byte key.
key = Encoding.ASCII.GetBytes("password12345678");
if (EncryptionAlgorithm.Rijndael == algorithm)
{ // Must be 16 bytes for Rijndael.
IV = Encoding.ASCII.GetBytes("init vec is big.");
}
else
{
IV = Encoding.ASCII.GetBytes("init vec");
}
}
else
{ //Des only works with an 8 byte key. The others uses variable length keys.
//Set the key to null to have a new one generated.
key = Encoding.ASCII.GetBytes("password");
IV = Encoding.ASCII.GetBytes("init vec");
}
// Uncomment the next lines to have the key or IV generated for you.
// key = null;
// IV = null;
enc.IV = IV;
// Perform the encryption.
cipherText = enc.Encrypt(plainText, key);
// Retrieve the intialization vector and key. You will need it
// for decryption.
IV = enc.IV;
key = enc.Key;
// Look at your cipher text and initialization vector.
Console.WriteLine(" Cipher text: " +
Convert.ToBase64String(cipherText));
Console.WriteLine("Initialization vector: " + Convert.ToBase64String(IV));
Console.WriteLine(" Key: " + Convert.ToBase64String(key));
}
catch(Exception ex)
{
Console.WriteLine("Exception encrypting. " + ex.Message);
return;
}
try
{ //Try to encrypt.
//Set up your decryption, give it the algorithm and initialization vector.
Decryptor dec = new Decryptor(algorithm);
dec.IV = IV;
// Go ahead and decrypt.
72
byte[] plainText = dec.Decrypt(cipherText, key);
// Look at your plain text.
Console.WriteLine(" Plain text: " +
Encoding.ASCII.GetString(plainText));
}
catch(Exception ex)
{
Console.WriteLine("Exception decrypting. " + ex.Message);
return;
}
?. En el menA enerar, ,aga clic en enerar solucin.
@. Ejecute la aplicacin de prueba para comprobar el funcionamiento de las clases
Encryptor y Decryptor.

Referencias
Para obtener ms informacin, consulte )Cmo% *lmacenar cadenas de cone+in
cifradas en el !egistro) en la seccin !eferencia de esta gua.

77

También podría gustarte