Ver Mensaje Individual
  #100 (permalink)  
Antiguo 25/02/2011, 12:55
danfred
 
Fecha de Ingreso: febrero-2011
Mensajes: 1
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: Facturas Digitales SAT - Mexico C#

A mi tambien me esta funcionando el código que mencionas, pero ahora ocupo el 'validador' o sea leer del XML el certificado, leer el sello digital y leer la cadena y validar con esos tres datos, he podido leer los datos pero la funcion en c# rsa.VerifyData(); no me funciona, te anexo el codigo para que le eches un ojo, en teoria deberia de funcionar:

public string ValidaSelloDigital2(string selloDigital, string cert, string cadena)
{
try
{
byte[] sello_byte = Convert.FromBase64String( selloDigital);
byte[] cert_byte = Convert.FromBase64String( cert);

X509Certificate2 certificado = new X509Certificate2( cert_byte);
//initialze the byte arrays to the public key information.
byte[] pk = certificado.GetPublicKey();

//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.PersistKeyInCsp = false;
//Get an instance of RSAParameters from ExportParameters function.
RSAParameters RSAKeyInfo = rsa.ExportParameters(false);

//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = pk;
//Import key parameters into RSA.
rsa.ImportParameters(RSAKeyInfo);

byte[] cadena_byte = System.Text.Encoding.UTF8.GetBytes(cadena);
System.Security.Cryptography.SHA1CryptoServiceProv ider x =
new System.Security.Cryptography.SHA1CryptoServiceProv ider();
byte[] data = x.ComputeHash(cadena_byte);

System.Security.Cryptography.MD5CryptoServiceProvi der x2 =
new System.Security.Cryptography.MD5CryptoServiceProvi der();
byte[] data2 = x2.ComputeHash(cadena_byte);

bool result = rsa.VerifyHash(data2, CryptoConfig.MapNameToOID("MD5"), sello_byte);
bool result2 = rsa.VerifyHash(data, CryptoConfig.MapNameToOID("SHA1"), sello_byte);

string ret = "Sello inválido.";

if (result)
{
ret = "Sello válido con digestion MD5 " + hash_md5(cadena);
}
else
{
if (result2)
{
ret = "Sello válido con digestion SHA1 " + hash_sha1(cadena);
}
}
return ret;
}
catch (Exception ex)
{
return "Error al desencriptar el sello digital " + ex.ToString();
}
}


Cita:
Iniciado por javalos532 Ver Mensaje
Hola a todos, alguien ya tiene este codigo en C#.... con VB me sale perfecto el sello pero en C# no me da igual ...ese es el codigo que estoy utilizando...

X509Certificate2 _MiCertificado = new X509Certificate2(@"....cert.p12", "12345");
RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)_MiCertificado.PrivateKe y;
MD5 hasher = MD5CryptoServiceProvider.Create();
byte[] bytesFirmados = RSA.SignData(System.Text.Encoding.UTF8.GetBytes(pD ato), hasher);
return Convert.ToBase64String(bytesFirmados);

Gracias.

Última edición por danfred; 25/02/2011 a las 13:02