elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [C#] cifrar/descifrar en AES
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [C#] cifrar/descifrar en AES  (Leído 18,194 veces)
Mace Windu

Desconectado Desconectado

Mensajes: 29


Flaming Our Skills Team


Ver Perfil WWW
[C#] cifrar/descifrar en AES
« en: 11 Enero 2009, 15:26 pm »

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
  1. public static string Encrypt(string PlainText, string Password, string SaltValue, string hashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
  2.        {
  3.            try
  4.            {
  5.                byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
  6.                byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);
  7.                byte[] plainTextBytes = Encoding.UTF8.GetBytes(PlainText);
  8.  
  9.                PasswordDeriveBytes password = new PasswordDeriveBytes(Password, saltValueBytes, hashAlgorithm, PasswordIterations);
  10.  
  11.                byte[] keyBytes = password.GetBytes(KeySize / 8);
  12.  
  13.                RijndaelManaged symmetricKey = new RijndaelManaged();
  14.  
  15.                symmetricKey.Mode = CipherMode.CBC;
  16.  
  17.                ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitialVectorBytes);
  18.  
  19.                MemoryStream memoryStream = new MemoryStream();
  20.  
  21.                CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
  22.  
  23.                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  24.  
  25.                cryptoStream.FlushFinalBlock();
  26.  
  27.                byte[] cipherTextBytes = memoryStream.ToArray();
  28.  
  29.                memoryStream.Close();
  30.                cryptoStream.Close();
  31.  
  32.                string cipherText = Convert.ToBase64String(cipherTextBytes);
  33.  
  34.                return cipherText;
  35.            }
  36.            catch
  37.            {
  38.                MessageBox.Show("The typed information is wrong. Please, check it.", "FoS TeaM", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  39.                return null;
  40.            }
  41.        }

Y esta descifra:

Código
  1. public static string Decrypt(string PlainText, string Password, string SaltValue, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
  2.        {
  3.            try
  4.            {
  5.                byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
  6.                byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);
  7.  
  8.                byte[] cipherTextBytes = Convert.FromBase64String(PlainText);
  9.  
  10.                PasswordDeriveBytes password = new PasswordDeriveBytes(Password, saltValueBytes, HashAlgorithm, PasswordIterations);
  11.  
  12.                byte[] keyBytes = password.GetBytes(KeySize / 8);
  13.  
  14.                RijndaelManaged symmetricKey = new RijndaelManaged();
  15.  
  16.                symmetricKey.Mode = CipherMode.CBC;
  17.  
  18.                ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, InitialVectorBytes);
  19.  
  20.                MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
  21.  
  22.                CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
  23.  
  24.                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  25.  
  26.                int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  27.  
  28.                memoryStream.Close();
  29.                cryptoStream.Close();
  30.  
  31.                string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  32.  
  33.                return plainText;
  34.            }
  35.            catch
  36.            {
  37.                MessageBox.Show("The typed information is wrong. Please, check it.", "FoS TeaM", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  38.                return null;
  39.            }
  40.        }

Salu2


En línea

WODZAROD

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Re: [C#] cifrar/descifrar en AES
« Respuesta #1 en: 20 Diciembre 2010, 03:44 am »

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


En línea

[D4N93R]
Wiki

Desconectado Desconectado

Mensajes: 1.646


My software never has bugs. Its just features!


Ver Perfil WWW
Re: [C#] cifrar/descifrar en AES
« Respuesta #2 en: 20 Diciembre 2010, 04:10 am »

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!
En línea

Shell Root
Moderador Global
***
Desconectado Desconectado

Mensajes: 3.723


<3


Ver Perfil WWW
Re: [C#] cifrar/descifrar en AES
« Respuesta #3 en: 20 Diciembre 2010, 04:24 am »

No creo que mi amigo Javier sea un copión. ES EL MISMO USUARIO. :D

PD: @[D4N93R], lo viste en WinJaNet?
En línea

Por eso no duermo, por si tras mi ventana hay un cuervo. Cuelgo de hilos sueltos sabiendo que hay veneno en el aire.
[D4N93R]
Wiki

Desconectado Desconectado

Mensajes: 1.646


My software never has bugs. Its just features!


Ver Perfil WWW
Re: [C#] cifrar/descifrar en AES
« Respuesta #4 en: 20 Diciembre 2010, 04:56 am »

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.! :)
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[c++] Cifrar y descifrar archivos
Programación C/C++
Beakman 2 17,475 Último mensaje 5 Diciembre 2016, 17:26 pm
por engel lex
cifrar/descifrar archivos
.NET (C#, VB.NET, ASP)
rob1104 3 6,886 Último mensaje 1 Julio 2012, 19:41 pm
por Keyen Night
cifrar/descifrar carpera en win y linux
Criptografía
maxmag 2 3,532 Último mensaje 27 Octubre 2012, 04:41 am
por APOKLIPTICO
[aporte]c++ cifrar/descifrar archivos comando
Programación C/C++
daryo 4 3,811 Último mensaje 12 Junio 2013, 17:57 pm
por daryo
software cifrar o cifrar
Criptografía
ambrayas 7 7,383 Último mensaje 21 Septiembre 2017, 04:41 am
por AlbertoBSD
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines