[VB2010] Algoritmo encryptacion AES

Iniciado por noxonsoftwares, Septiembre 29, 2012, 06:59:13 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Septiembre 29, 2012, 06:59:13 PM Ultima modificación: Mayo 27, 2014, 09:51:25 PM por Expermicid
Bueno como mi primer aporte les traigo este algoritmo de encriptacion AES el cual funciona de la siguiente manera:

Para encriptar es necesario introducir una contraseña, la cual servira posteriormente para desencriptar lo que esta encriptado (facil no? )

Bueno sin mas vueltas aqui el algoritmo.

Código: vbnet
'Encriptacion en AES con contraseña para poder ver lo encritado
    'Se encripta y se le asigna una contraseña para poder ver el texto desencriptado
    'Para desencriptar pide que se ingrese la contraseña, si esta es incorrecta no muestra nada.

    Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return encrypted
        Catch ex As Exception
        End Try
    End Function

    Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return decrypted
        Catch ex As Exception
        End Try
    End Function



Ejemplo de uso con dos buttons y dos textbox

Código: vbnet
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = AES_Encrypt(TextBox1.Text, InputBox("ingrese la contraseña"))
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = AES_Decrypt(TextBox2.Text, InputBox("ingrese la contraseña"))
    End Sub

Se ve muy interesante, lastima que no trabajo en visual, no se podria usar en C#?? son .net los dos,

gracias por el aporte

El codigo en C# seria el siguiente:


Código: php
//Encriptacion en AES con contraseña para poder ver lo encritado
//Se encripta y se le asigna una contraseña para poder ver el texto desencriptado
//Para desencriptar pide que se ingrese la contraseña, si esta es incorrecta no muestra nada.

public string AES_Encrypt(string input, string pass)
{
System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider();
string encrypted = "";
try {
byte[] hash = new byte[32];
byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass));
Array.Copy(temp, 0, hash, 0, 16);
Array.Copy(temp, 0, hash, 15, 16);
AES.Key = hash;
AES.Mode = System.Security.Cryptography.CipherMode.ECB;
System.Security.Cryptography.ICryptoTransform DESEncrypter = AES.CreateEncryptor();
byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(input);
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length));
return encrypted;
} catch (Exception ex) {
}
}

public string AES_Decrypt(string input, string pass)
{
System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider();
string decrypted = "";
try {
byte[] hash = new byte[32];
byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass));
Array.Copy(temp, 0, hash, 0, 16);
Array.Copy(temp, 0, hash, 15, 16);
AES.Key = hash;
AES.Mode = System.Security.Cryptography.CipherMode.ECB;
System.Security.Cryptography.ICryptoTransform DESDecrypter = AES.CreateDecryptor();
byte[] Buffer = Convert.FromBase64String(input);
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length));
return decrypted;
} catch (Exception ex) {
}
}


Un saludo y mira en esta pagina puedes convertir de VB a C# o viceversa   No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Gracias por responder :), esta interesante la pagina que me pasaste, :D :D

De nada @Zorro dejame que busco tambien existe un programa de escritorio que te permite hacer lo mismo que la pagina pero ahora no recuerdo donde lo tenia ni como se llama  :-[

Hey bro ese programa es excellente ahora me centro ams en esto y me encanta.
Saludos noxon.
:D
System32
XD
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Solamente no acepta caracteres especiales. :( ejemplo: (ÇÁÀÜÑ) e otros! Ayuadame

Me gusto mucho tu aporte +1, recien lo estaba probando y me va bien la encriptacion pero lo que no me va es la desencriptacion.

Saludos.
Hola, soy Kr3L !

@No tienes permitido ver los links. Registrarse o Entrar a mi cuenta - Sabes que estoy probando el sistema que hiciste, y funciona. Me da un hash md5, pero a la hora de probar el mismo hash con otras paginas usando el mismo sistema

AES - ECB del Rijndael. Me dicen todos que la clave esta mal (la que yo mismo ingreso), esta corrupto o el codigo de encriptacion esta rancio.

Estoy haciendo algo mal? O el sistema es para usar por si mismo, en el sentido de tu sistema de encriptacion no es el miso que los demas?

Me podrias orientar?

De todas formas sos un genio!
Pikaa~


HOLA!!!

Aqui la funcion decrypt que uso yo.
Código: vbnet
 
Imports System.Security.Cryptography
Public Function DecryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_decrypt As String)

        Dim sEncryptedString As String = prm_text_to_decrypt

        Dim myRijndael As New RijndaelManaged
        myRijndael.Padding = PaddingMode.Zeros
        myRijndael.Mode = CipherMode.CBC
        myRijndael.KeySize = 256
        myRijndael.BlockSize = 256

        Dim key() As Byte
        Dim IV() As Byte

        key = System.Text.Encoding.ASCII.GetBytes(prm_key)
        IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

        Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

        Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)

        Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}

        Dim msDecrypt As New MemoryStream(sEncrypted)
        Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

        Return (System.Text.Encoding.ASCII.GetString(fromEncrypt))

    End Function


GRACIAS POR LEER!!!
"Algunos creen que soy un bot, puede que tengan razon"
"Como no se puede igualar a Dios, ya he decidido que hacer, ¡SUPERARLO!"
"La peor de las ignorancias es no saber corregirlas"

*Shadow Scouts Team*                                                No tienes permitido ver los links. Registrarse o Entrar a mi cuenta