Encriptación con C#

10/11/2004 - 16:32 por Zenkius | Informe spam
Hola grupo!

Quiero usar algún tipo de encriptación en mi aplicación
para validar los usuarios que inician sesión y he oido que
el C# tiene algunas funciones que permiten encriptar.

Pudieran recomendarme alguna manera de hacer esto????

Zenkius.

Preguntas similare

Leer las respuestas

#1 Rodrigo Corral [MVP]
10/11/2004 - 22:26 | Informe spam
Me da que estas confundiendo conceptos. La encriptación no tiene nada que
ver con la autenticación de usuarios, en todo caso con el almacenamiento de
las passwords pero nada más.

¿Que quieres hacer? ¿Comprobar si un usuario te pasa su contraseña de
windows correcta? ¿Almacenar usuarios y contraseñas en una base de datos?


Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org
Respuesta Responder a este mensaje
#2 Zenkius
10/11/2004 - 22:52 | Informe spam
Lo que quiero hacer es almacenar usuarios y contraseñas en
una base de datos SQL.

Podrías ayudarme?

Me da que estas confundiendo conceptos. La encriptación


no tiene nada que
ver con la autenticación de usuarios, en todo caso con el


almacenamiento de
las passwords pero nada más.

¿Que quieres hacer? ¿Comprobar si un usuario te pasa su


contraseña de
windows correcta? ¿Almacenar usuarios y contraseñas en


una base de datos?


Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


.

Respuesta Responder a este mensaje
#3 Santi
11/11/2004 - 10:06 | Informe spam
Hola,

si tu aplicación es web puedes usar:

string encriptada= FormsAuthentication.HashPasswordForStoringInConfigFile(
sinEncriptar, "SHA1" );

Así lo guardas en la base de datos y para comparar encriptas tambien la que
te dan y la comparas con la encriptada.



Si es una app de escritorio aquí tienes un ejemplo:

using System.IO;
using System.Text;
using System.Security.Cryptography;


string original = "This is a much longer string of data than a
public/private key algorithm will accept.";
string roundtrip;
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV;

//Create a new key and initialization vector.
myRijndael.GenerateKey();
myRijndael.GenerateIV();

//Get the key and IV.
key = myRijndael.Key;
IV = myRijndael.IV;

//Get an encryptor.
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);

//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);

//Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original);

//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();

//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();


this.Response.Write("Round Trip: " + textConverter.GetString(encrypted) +
"<P>");

//This is where the message would be transmitted to a recipient
// who already knows your secret key. Optionally, you can
// also encrypt your secret key using a public key algorithm
// and pass it to the mesage recipient along with the RijnDael
// encrypted message.

//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

//Now decrypt the previously encrypted message using the decryptor
// obtained in the above step.
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);

fromEncrypt = new byte[encrypted.Length];

//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt);

//Display the original data and the decrypted data.
this.Response.Write("Original: " + original);
this.Response.Write("Round Trip: " + roundtrip);
Respuesta Responder a este mensaje
#4 Rodrigo Corral [MVP]
11/11/2004 - 10:57 | Informe spam
Lo que debes guardar es un Hash de la Password y no la password en sí.
Puedes usar la función HashPasswordForStoringInConfigFile para obtener este
hash. Cuando el usuario te proporciona la contraseña simplemente obten su
hash y comparale con el hash almacenado en la base de datos. Para alguien
que vea el hash en la base de datos sera imposible deducir la password.

Puedes saber mas sobre este tema en los ariculos siguientes:

Security Briefs: Hashing Passwords, The AllowPartiallyTrustedCallers
Attribute -- MSDN Magazine, August 2003
http://msdn.microsoft.com/msdnmag/i...rityBriefs

Protect It: Safeguard Database Connection Strings and Other Sensitive
Settings in Your Code -- MSDN Magazine, November 2003
http://msdn.microsoft.com/msdnmag/i...ctYourData


Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida