Ver Mensaje Individual
  #3 (permalink)  
Antiguo 07/11/2006, 15:00
Avatar de shumito
shumito
 
Fecha de Ingreso: mayo-2006
Mensajes: 248
Antigüedad: 18 años
Puntos: 0
esto encontre...saludos !

public string EncriptaTexto(string Cadena,byte[] Key, byte[] IV)
{
string TextoEncriptado = string.Empty;

// Create or open the specified file.
MemoryStream mStream = new MemoryStream();

// Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream, RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream);

try
{
// Write the data to the stream
// to encrypt it.
sWriter.Write(Cadena);
TextoEncriptado = Convert.ToBase64String(mStream.ToArray());

}
catch (Exception ex)
{
TextoEncriptado = ex.Message;
}
finally
{
sWriter.Close();
cStream.Close();
mStream.Close();
}
return TextoEncriptado;
}

public string DesencriptaTexto(string Cadena, byte[] Key, byte[] IV)
{
string TextoDesencriptado = string.Empty;

// Create or open the specified file.
MemoryStream mStream = new MemoryStream(Convert.FromBase64String(Cadena));

// Crea un nuevo Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream, RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);

// Create a StreamWriter using the CryptoStream.
StreamReader sReader = new StreamReader(cStream);
try
{
// escribo el texto a desencriptar
TextoDesencriptado = sReader.ReadToEnd();
}
catch (Exception ex)
{
TextoDesencriptado = ex.Message;
}
finally
{
// cierro todos los streams
sReader.Close();
cStream.Close();
mStream.Close();
}
return TextoDesencriptado;
}
}