Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Mace Windu en 11 Enero 2009, 15:26 PM

Título: [C#] cifrar/descifrar en AES
Publicado por: Mace Windu 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:


Argumentos:


Esta función cifra:

Código (csharp) [Seleccionar]
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 (csharp) [Seleccionar]
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
Título: Re: [C#] cifrar/descifrar en AES
Publicado por: WODZAROD 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
Título: Re: [C#] cifrar/descifrar en AES
Publicado por: [D4N93R] 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!
Título: Re: [C#] cifrar/descifrar en AES
Publicado por: Shell Root 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?
Título: Re: [C#] cifrar/descifrar en AES
Publicado por: [D4N93R] 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.! :)