Problemas con Convert.FromBase64String

12/05/2005 - 18:55 por Ramón Hernández | Informe spam
Hola.

A ver si alguien pudiera ayudarme con este codigo, resulta que converti un
codigo de C# a VB y todo parece funcionar bien, el problema es que al llamar
el metodo Desencriptar las longitudes de los arreglos de Byte para
bytDesEncriptar, tempArray y encrypted son distintas dependiendo si se corre
en VB o C#.


Codigo en VB.
Imports System.Security.Cryptography

Public Class clsCrypto

Public Function Encriptar(ByVal strEncriptar As String, ByVal bytPK() As
Byte) As Byte()

Dim miRijndael As Rijndael = Rijndael.Create()

Dim encrypted As Byte()

Dim returnvalue As Byte()

Try

miRijndael.Key = bytPK

miRijndael.GenerateIV()

Dim toEncrypt As Byte() = System.Text.Encoding.UTF8.GetBytes(strEncriptar)

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt, 0,
toEncrypt.Length)

ReDim returnvalue(miRijndael.IV.Length + encrypted.Length)

miRijndael.IV.CopyTo(returnvalue, 0)

encrypted.CopyTo(returnvalue, miRijndael.IV.Length)

Catch ex As Exception

Finally

miRijndael.Clear()

End Try

Return returnvalue

End Function

Public Function Desencriptar(ByVal bytDesEncriptar() As Byte, ByVal bytPK()
As Byte) As String

Dim miRijndael As Rijndael = Rijndael.Create()

Dim tempArray(miRijndael.IV.Length) As Byte

Dim encrypted(bytDesEncriptar.Length - miRijndael.IV.Length) As Byte

Dim returnValue As String = String.Empty

Try

miRijndael.Key = bytPK

Array.Copy(bytDesEncriptar, tempArray, tempArray.Length)

Array.Copy(bytDesEncriptar, tempArray.Length, encrypted, 0,
encrypted.Length)

miRijndael.IV = tempArray

returnValue System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).Transform
FinalBlock(encrypted, 0, encrypted.Length))

Catch ex As Exception

Finally

miRijndael.Clear()

End Try

Return returnValue

End Function

Public Function Encriptar(ByVal strEncriptar As String, ByVal strPK As
String) As Byte()

Return Encriptar(strEncriptar, (New PasswordDeriveBytes(strPK,
Nothing)).GetBytes(32))

End Function

Public Function Desencriptar(ByVal bytDesEncriptar() As Byte, ByVal strPK As
String) As String

Return Desencriptar(bytDesEncriptar, (New PasswordDeriveBytes(strPK,
Nothing)).GetBytes(32))

End Function

End Class



CODIGO EN C#

using System;

using System.Security.Cryptography;

using System.Text;

namespace TransEncripHash

{

/// <summary>

/// Summary description for Transformacion.

/// </summary>

public class MiRijndael

{

public static byte[] Encriptar(string strEncriptar, byte[] bytPK)

{

Rijndael miRijndael = Rijndael.Create();

byte[] encrypted = null;

byte[] returnValue = null;

try

{

miRijndael.Key = bytPK;

miRijndael.GenerateIV();

byte[] toEncrypt = System.Text.Encoding.UTF8.GetBytes(strEncriptar);

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt, 0,
toEncrypt.Length);

returnValue = new byte[miRijndael.IV.Length + encrypted.Length];

miRijndael.IV.CopyTo(returnValue, 0);

encrypted.CopyTo(returnValue, miRijndael.IV.Length);

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

public static string Desencriptar(byte[] bytDesEncriptar, byte[] bytPK)

{

Rijndael miRijndael = Rijndael.Create();

byte[] tempArray = new byte[miRijndael.IV.Length];

byte[] encrypted = new byte[bytDesEncriptar.Length - miRijndael.IV.Length];

string returnValue = string.Empty;

try

{

miRijndael.Key = bytPK;

Array.Copy(bytDesEncriptar, tempArray, tempArray.Length);

Array.Copy(bytDesEncriptar, tempArray.Length, encrypted, 0,
encrypted.Length);

miRijndael.IV = tempArray;

returnValue System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).Transform
FinalBlock(encrypted, 0, encrypted.Length));

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

public static byte[] Encriptar(string strEncriptar, string strPK)

{

return Encriptar(strEncriptar, (new PasswordDeriveBytes(strPK,
null)).GetBytes(32));

}

public static string Desencriptar(byte[] bytDesEncriptar, string strPK)

{

return Desencriptar(bytDesEncriptar, (new PasswordDeriveBytes(strPK,
null)).GetBytes(32));

}



public static byte[] EncriptarDeImagen(byte[] bytEncriptar, string strPK,
CipherMode cMode)

{

Rijndael miRijndael = Rijndael.Create();

byte[] encrypted = null;

byte[] returnValue = null;

try

{

miRijndael.Key = (new PasswordDeriveBytes(strPK, null)).GetBytes(32);

miRijndael.Mode = cMode;

byte[] toEncrypt = new byte[bytEncriptar.Length-34];

Array.Copy(bytEncriptar, 34, toEncrypt, 0, bytEncriptar.Length-34);

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt, 0,
toEncrypt.Length);

returnValue = new byte[34 + encrypted.Length];

bytEncriptar.CopyTo(returnValue, 0);

encrypted.CopyTo(returnValue, 34);

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

}

}
 

Leer las respuestas

#1 Ramón Hernández
12/05/2005 - 19:01 | Informe spam
Ok, ya vi cual es el problema.

Como le digo a VB que la base sea 1 y no 0?.

Saludos.

"Ramón Hernández" escribió en el mensaje
news:
Hola.

A ver si alguien pudiera ayudarme con este codigo, resulta que converti un
codigo de C# a VB y todo parece funcionar bien, el problema es que al


llamar
el metodo Desencriptar las longitudes de los arreglos de Byte para
bytDesEncriptar, tempArray y encrypted son distintas dependiendo si se


corre
en VB o C#.


Codigo en VB.
Imports System.Security.Cryptography

Public Class clsCrypto

Public Function Encriptar(ByVal strEncriptar As String, ByVal bytPK() As
Byte) As Byte()

Dim miRijndael As Rijndael = Rijndael.Create()

Dim encrypted As Byte()

Dim returnvalue As Byte()

Try

miRijndael.Key = bytPK

miRijndael.GenerateIV()

Dim toEncrypt As Byte() = System.Text.Encoding.UTF8.GetBytes(strEncriptar)

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt,


0,
toEncrypt.Length)

ReDim returnvalue(miRijndael.IV.Length + encrypted.Length)

miRijndael.IV.CopyTo(returnvalue, 0)

encrypted.CopyTo(returnvalue, miRijndael.IV.Length)

Catch ex As Exception

Finally

miRijndael.Clear()

End Try

Return returnvalue

End Function

Public Function Desencriptar(ByVal bytDesEncriptar() As Byte, ByVal


bytPK()
As Byte) As String

Dim miRijndael As Rijndael = Rijndael.Create()

Dim tempArray(miRijndael.IV.Length) As Byte

Dim encrypted(bytDesEncriptar.Length - miRijndael.IV.Length) As Byte

Dim returnValue As String = String.Empty

Try

miRijndael.Key = bytPK

Array.Copy(bytDesEncriptar, tempArray, tempArray.Length)

Array.Copy(bytDesEncriptar, tempArray.Length, encrypted, 0,
encrypted.Length)

miRijndael.IV = tempArray

returnValue >


System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).Transform
FinalBlock(encrypted, 0, encrypted.Length))

Catch ex As Exception

Finally

miRijndael.Clear()

End Try

Return returnValue

End Function

Public Function Encriptar(ByVal strEncriptar As String, ByVal strPK As
String) As Byte()

Return Encriptar(strEncriptar, (New PasswordDeriveBytes(strPK,
Nothing)).GetBytes(32))

End Function

Public Function Desencriptar(ByVal bytDesEncriptar() As Byte, ByVal strPK


As
String) As String

Return Desencriptar(bytDesEncriptar, (New PasswordDeriveBytes(strPK,
Nothing)).GetBytes(32))

End Function

End Class



CODIGO EN C#

using System;

using System.Security.Cryptography;

using System.Text;

namespace TransEncripHash

{

/// <summary>

/// Summary description for Transformacion.

/// </summary>

public class MiRijndael

{

public static byte[] Encriptar(string strEncriptar, byte[] bytPK)

{

Rijndael miRijndael = Rijndael.Create();

byte[] encrypted = null;

byte[] returnValue = null;

try

{

miRijndael.Key = bytPK;

miRijndael.GenerateIV();

byte[] toEncrypt = System.Text.Encoding.UTF8.GetBytes(strEncriptar);

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt,


0,
toEncrypt.Length);

returnValue = new byte[miRijndael.IV.Length + encrypted.Length];

miRijndael.IV.CopyTo(returnValue, 0);

encrypted.CopyTo(returnValue, miRijndael.IV.Length);

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

public static string Desencriptar(byte[] bytDesEncriptar, byte[] bytPK)

{

Rijndael miRijndael = Rijndael.Create();

byte[] tempArray = new byte[miRijndael.IV.Length];

byte[] encrypted = new byte[bytDesEncriptar.Length -


miRijndael.IV.Length];

string returnValue = string.Empty;

try

{

miRijndael.Key = bytPK;

Array.Copy(bytDesEncriptar, tempArray, tempArray.Length);

Array.Copy(bytDesEncriptar, tempArray.Length, encrypted, 0,
encrypted.Length);

miRijndael.IV = tempArray;

returnValue >


System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).Transform
FinalBlock(encrypted, 0, encrypted.Length));

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

public static byte[] Encriptar(string strEncriptar, string strPK)

{

return Encriptar(strEncriptar, (new PasswordDeriveBytes(strPK,
null)).GetBytes(32));

}

public static string Desencriptar(byte[] bytDesEncriptar, string strPK)

{

return Desencriptar(bytDesEncriptar, (new PasswordDeriveBytes(strPK,
null)).GetBytes(32));

}



public static byte[] EncriptarDeImagen(byte[] bytEncriptar, string strPK,
CipherMode cMode)

{

Rijndael miRijndael = Rijndael.Create();

byte[] encrypted = null;

byte[] returnValue = null;

try

{

miRijndael.Key = (new PasswordDeriveBytes(strPK, null)).GetBytes(32);

miRijndael.Mode = cMode;

byte[] toEncrypt = new byte[bytEncriptar.Length-34];

Array.Copy(bytEncriptar, 34, toEncrypt, 0, bytEncriptar.Length-34);

encrypted = (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt,


0,
toEncrypt.Length);

returnValue = new byte[34 + encrypted.Length];

bytEncriptar.CopyTo(returnValue, 0);

encrypted.CopyTo(returnValue, 34);

}

catch { }

finally { miRijndael.Clear(); }

return returnValue;

}

}

}


Preguntas similares