[C#] cifrar/descifrar en AES
(1/1)
Mace Windu:
Bueno, el código está hecho en C# y corre perfectamente bajo VS 2008. Utiliza el famoso algoritmo de cifrado AES.
Namespaces utilizados:
System;System.Security.Cryptography;
Argumentos:
PlainText: Texto a cifrar.Password: Nuestra contraseña.hashAlgorithm: El algoritmo para generar el hash, puede ser MD5 o SHA1.SaltValue: Puede ser cualquier cadena.InitialVector: Debe ser una cadena de 16 bytes, es decir, 16 caracteres.PasswordIterations: Con uno o dos será suficiente.KeySize: Puede ser cualquiera de estos tres valores: 128, 192 o 256.
Esta función cifra:
Código
public static string Encrypt(string PlainText, string Password, string SaltValue, string hashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
{
try
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(PlainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(Password, saltValueBytes, hashAlgorithm, PasswordIterations);
byte[] keyBytes = password.GetBytes(KeySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitialVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
catch
{
MessageBox.Show("The typed information is wrong. Please, check it.", "FoS TeaM", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return null;
}
}
Y esta descifra:
Código
public static string Decrypt(string PlainText, string Password, string SaltValue, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
{
try
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);
byte[] cipherTextBytes = Convert.FromBase64String(PlainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(Password, saltValueBytes, HashAlgorithm, PasswordIterations);
byte[] keyBytes = password.GetBytes(KeySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, InitialVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
return plainText;
}
catch
{
MessageBox.Show("The typed information is wrong. Please, check it.", "FoS TeaM", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return null;
}
}
Salu2
WODZAROD:
Hola, tienes alguna aplicación en Visual C# o en otro software de cifrado AES. Haber si me puedes facilitar una aplicacòn con tu ejemplo mostrado, te lo agradecería mucho.
Rómulo
[D4N93R]:
Ese código lo posteó otra persona, en otro foro. Por favor editar el post y colocar la fuente original, sino será borrado.
Un saludo!
Shell Root:
No creo que mi amigo Javier sea un copión. ES EL MISMO USUARIO. :D
PD: @[D4N93R], lo viste en WinJaNet?
[D4N93R]:
Por casualidad alguien me había mandado eso hace tiempo para que lo revisara y me acordé, por eso llo busqué y encontré el mismo en varios foros, por lo que si es el autor todo lo que tiene es que ponerse como tal, eso es todo :) Tu sabes, para evitar problemas.
Un saludo!
PD: Pase lo que pase es un buen código, por lo que apenas él edite el post, lo pongo en Temas interesantes.! :)
Navegación