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;

}

}

}

Preguntas similare

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;

}

}

}


Respuesta Responder a este mensaje
#2 José Manuel Agüero
12/05/2005 - 21:01 | Informe spam
Hola, Ramón:

En C# la declaración de una variable en array indica el número de elementos, pero en Visual Basic indica el índice máximo:
C#:
int miarray[25] // 25 elementos: 0 a 24.
VB:
dim miarray(25) as integer '26 elementos: 0 a 25.

Solución: Revisa tu código y cuando indiques los índices en un Dim o ReDim, resta uno. Por ejemplo:
ReDim returnvalue(miRijndael.IV.Length + encrypted.Length - 1)
Dim tempArray(miRijndael.IV.Length - 1) As Byte
Dim encrypted(bytDesEncriptar.Length - miRijndael.IV.Length - 1) As Byte

Saludos.


"Ramón Hernández" escribió en el mensaje news:
| 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;
| >
| > }
| >
| > }
| >
| > }
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida