C sharp - AYUDA, Encriptar y DesEncriptar en SQL desde Visual C#

 
Vista:
sin imagen de perfil

AYUDA, Encriptar y DesEncriptar en SQL desde Visual C#

Publicado por Josue (1 intervención) el 25/07/2016 01:09:12
Buenas, tengo un proyecto donde todos los datos del sistema deben estar encriptados en la base de datos, pero me he encontrado un problema y es que a la hora de ejecutar la sentencia SQL me da un error de que dice :

"An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: Failed to convert parameter value from a String to a Byte[]."


El error se genera en el .ExecuteNonQuery();

Los codigos que estoy utilizando para la ejecucion son los siguientes:

-------------------------------------------------- Recive los datos desde la parte visual--- Los datos van de tipo String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public bool guardar_Consecutivo()
{
	conexion = cls_DAL.trae_conexion("PROYECTO", ref mensaje_error, ref numero_error);
	if (conexion == null)
	{
		MessageBox.Show(mensaje_error, "Error al obtener cadena de conexion", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return false;
	}
	else
	{
		sql = "Insert into CONSECUTIVOS values( ENCRYPTBYPASSPHRASE('password', @tipo ), ENCRYPTBYPASSPHRASE('password', @descripcion), ENCRYPTBYPASSPHRASE('password', @valor), ENCRYPTBYPASSPHRASE('password', @prefijo))";
 
		ParamStruct[] parametros = new ParamStruct[4];
		cls_DAL.agregar_datos_estructura_parametros(ref parametros, 0, "@tipo", SqlDbType.VarBinary, _Tipo);
		cls_DAL.agregar_datos_estructura_parametros(ref parametros, 1, "@descripcion", SqlDbType.VarBinary, _Descripcion);
		cls_DAL.agregar_datos_estructura_parametros(ref parametros, 2, "@valor", SqlDbType.VarBinary,  _Valor);
		cls_DAL.agregar_datos_estructura_parametros(ref parametros, 3, "@prefijo", SqlDbType.VarBinary, _Prefijo);
 
		cls_DAL.conectar(conexion, ref mensaje_error, ref numero_error);
		cls_DAL.ejecuta_sqlcommand(conexion, sql, false, parametros, ref mensaje_error, ref numero_error);
		if (numero_error != 0)
		{
			MessageBox.Show(mensaje_error, "Error al guardar el Consecutivo", MessageBoxButtons.OK, MessageBoxIcon.Error);
			cls_DAL.desconectar(conexion, ref mensaje_error, ref numero_error);
			return false;
		}
		else
		{
			cls_DAL.desconectar(conexion, ref mensaje_error, ref numero_error);
			return true;
		}
	}
}

--------------------------------------------------------------------------- Este es el codigo para ejecutar los parametros

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void agregar_datos_estructura_parametros(ref ParamStruct[] parametros, int posicion, string nombre_parametro, SqlDbType tipo_dato_parametro, object valor_parametro)
{
	parametros[posicion].Nombre_Parametro = nombre_parametro.ToString();
	parametros[posicion].Tipo_Dato = tipo_dato_parametro;
	parametros[posicion].Valor_Parametro = valor_parametro;
}
 
-------------------------------------------------------------------- Y este el codigo que ejecuta la sentencia SQL
 
public static void ejecuta_sqlcommand(SqlConnection conexion, string sql, bool es_procedimiento_almacenado, ParamStruct[] parametros, ref string mensaje_error, ref int numero_error)
{
	SqlCommand sql_command;
	try
	{
		int resultado = 0;
		sql_command = new SqlCommand(string.Format (sql), conexion);
		if (es_procedimiento_almacenado)
		{
			sql_command.CommandType = CommandType.StoredProcedure;
		}
		foreach (ParamStruct var in parametros)
		{
			Agrega_parametro(ref sql_command, var.Nombre_Parametro, var.Valor_Parametro.ToString(), var.Tipo_Dato);
		}
		resultado = sql_command.ExecuteNonQuery();
		mensaje_error = "";
		numero_error = 0;
	}
	catch (SqlException ex)
	{
		mensaje_error = "Error al ejecutar la sentencia sql, informacion adicional: " + ex.Message;
		numero_error = ex.Number;
	}
}


Les agradeceria mucho la ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder