|      Respuesta: Factura electroncia sat mexico        aqui les dejo una clase q utilize   
using System;   
using System.IO;   
using System.Text;   
using System.Security.Cryptography;   
using System.Security.Cryptography.X509Certificates;   
using System.Runtime.InteropServices;   
using System.Security;   
using System.Diagnostics;   
using System.ComponentModel;       
/// <summary>   
/// Realiza la    
/// </summary>   
public class clsSeguridad 
{   
    static bool verbose = false;   
    private void SAT() 
    { 
        string aa = ""; 
        //byte[] pLlavePrivadaBytes;   
        //string CadenaOriginal = "";   
        //string lPassword = "122233";   
        //SecureString lSecStr = new SecureString();   
        //lSecStr.Clear();   
        //foreach (char c in lPassword.ToCharArray())   
        //    lSecStr.AppendChar(c);       
        //RSACryptoServiceProvider lrsa = DecodeEncryptedPrivateKeyInfo(pLlavePrivadaBytes, lSecStr);   
        ////RSACryptoServiceProvider lrsa = DecodeEncryptedPrivateKeyInfo(pLlavePrivadaBytes, lSecurePaswordString);   
        //MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();   
        //Byte[] bytesFirmados = lrsa.SignData(System.Text.Encoding.UTF8.GetBytes(C  adenaOriginal), hasher);   
        //string sellodigital = Convert.ToBase64String(bytesFirmados);   
    }   
    public static string CodificarMD5(string input) 
    { 
        byte[] CadenaUTF8; 
        byte[] tmpHash; 
        //convierte en UTF8 
        CadenaUTF8 = Encoding.UTF8.GetBytes(input); 
        //crea el hash 
        tmpHash = new MD5CryptoServiceProvider().ComputeHash(CadenaUTF8)  ; 
        // lo pasa a una variable string mediante la funcion ByteArrayToString 
        int i; 
        StringBuilder sOutput = new StringBuilder(tmpHash.Length); 
        for (i = 0; i < tmpHash.Length; i++) 
        { 
            sOutput.Append(tmpHash[i].ToString("x2")); 
        } 
        return sOutput.ToString(); 
    }    
    public static RSACryptoServiceProvider DecodeEncryptedPrivateKeyInfo(byte[] encpkcs8, SecureString lSecStr) 
    {   
        // encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"   
        // this byte[] includes the sequence byte and terminal encoded null    
        byte[] OIDpkcs5PBES2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D };   
        byte[] OIDpkcs5PBKDF2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0C };   
        byte[] OIDdesEDE3CBC = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07 };   
        byte[] seqdes = new byte[10];   
        byte[] seq = new byte[11];   
        byte[] salt;   
        byte[] IV;   
        byte[] encryptedpkcs8;   
        byte[] pkcs8;       
        int saltsize, ivsize, encblobsize;   
        int iterations;       
        // ---------  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ------   
        MemoryStream mem = new MemoryStream(encpkcs8);   
        int lenstream = (int)mem.Length;   
        BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading   
        byte bt = 0;   
        ushort twobytes = 0;       
        try 
        {       
            twobytes = binr.ReadUInt16();   
            if (twobytes == 0x8130)   //data read as little endian order (actual data order for Sequence is 30 81)   
                binr.ReadByte();      //advance 1 byte   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();     //advance 2 bytes   
            else   
                return null;       
            twobytes = binr.ReadUInt16();   //inner sequence   
            if (twobytes == 0x8130)   
                binr.ReadByte();   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();           
            seq = binr.ReadBytes(11);       //read the Sequence OID   
            if (!CompareBytearrays(seq, OIDpkcs5PBES2)) //is it a OIDpkcs5PBES2 ?   
                return null;       
            twobytes = binr.ReadUInt16();   //inner sequence for pswd salt   
            if (twobytes == 0x8130)   
                binr.ReadByte();   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();       
            twobytes = binr.ReadUInt16();   //inner sequence for pswd salt   
            if (twobytes == 0x8130)   
                binr.ReadByte();   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();       
            seq = binr.ReadBytes(11);       //read the Sequence OID   
            if (!CompareBytearrays(seq, OIDpkcs5PBKDF2))      //is it a OIDpkcs5PBKDF2 ?   
                return null;       
            twobytes = binr.ReadUInt16();   
            if (twobytes == 0x8130)   
                binr.ReadByte();   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();       
            bt = binr.ReadByte();   
            if (bt != 0x04)           //expect octet string for salt   
                return null;   
            saltsize = binr.ReadByte();   
            salt = binr.ReadBytes(saltsize);       
            if (verbose)   
                showBytes("Salt for pbkd", salt);   
            bt = binr.ReadByte();   
            if (bt != 0x02)     //expect an integer for PBKF2 interation count   
                return null;       
            int itbytes = binr.ReadByte();  //PBKD2 iterations should fit in 2 bytes.   
            if (itbytes == 1)   
                iterations = binr.ReadByte();   
            else if (itbytes == 2)   
                iterations = 256 * binr.ReadByte() + binr.ReadByte();   
            else   
                return null;   
            if (verbose)   
                Console.WriteLine("PBKD2 iterations {0}", iterations);       
            twobytes = binr.ReadUInt16();   
            if (twobytes == 0x8130)   
                binr.ReadByte();   
            else if (twobytes == 0x8230)   
                binr.ReadInt16();           
            seqdes = binr.ReadBytes(10);          //read the Sequence OID   
            if (!CompareBytearrays(seqdes, OIDdesEDE3CBC))    //is it a OIDdes-EDE3-CBC ?   
                return null;       
            bt = binr.ReadByte();   
            if (bt != 0x04)           //expect octet string for IV   
                return null;   
            ivsize = binr.ReadByte(); // IV byte size should fit in one byte (24 expected for 3DES)   
            IV = binr.ReadBytes(ivsize);   
            if (verbose)   
                showBytes("IV for des-EDE3-CBC", IV);       
            bt = binr.ReadByte();   
            if (bt != 0x04)           // expect octet string for encrypted PKCS8 data   
                return null;           
            bt = binr.ReadByte();       
            if (bt == 0x81)   
                encblobsize = binr.ReadByte();    // data size in next byte   
            else if (bt == 0x82)   
                encblobsize = 256 * binr.ReadByte() + binr.ReadByte();   
            else   
                encblobsize = bt;           // we already have the data size           
            encryptedpkcs8 = binr.ReadBytes(encblobsize);   
            //if(verbose)   
            //      showBytes("Encrypted PKCS8 blob", encryptedpkcs8) ;           
            SecureString secpswd = lSecStr;//GetSecPswd(lSecStr.ToString());   
            pkcs8 = DecryptPBDK2(encryptedpkcs8, salt, IV, secpswd, iterations);   
            if (pkcs8 == null)  // probably a bad pswd entered.   
                return null;       
            //if(verbose)   
            //      showBytes("Decrypted PKCS #8", pkcs8) ;   
            //----- With a decrypted pkcs #8 PrivateKeyInfo blob, decode it to an RSA ---   
            RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8);   
            return rsa;   
        }       
        catch (Exception) 
        {   
            return null;   
        }       
        finally { binr.Close(); }           
    }   
    private static bool CompareBytearrays(byte[] a, byte[] b) 
    {   
        if (a.Length != b.Length)   
            return false;   
        int i = 0;   
        foreach (byte c in a) 
        {   
            if (c != b[i])   
                return false;   
            i++;   
        }   
        return true;   
    }   
    public static byte[] DecryptPBDK2(byte[] edata, byte[] salt, byte[] IV, SecureString secpswd, int iterations) 
    {   
        CryptoStream decrypt = null;       
        IntPtr unmanagedPswd = IntPtr.Zero;   
        byte[] psbytes = new byte[secpswd.Length];   
        unmanagedPswd = Marshal.SecureStringToGlobalAllocAnsi(secpswd);   
        Marshal.Copy(unmanagedPswd, psbytes, 0, psbytes.Length);   
        Marshal.ZeroFreeGlobalAllocAnsi(unmanagedPswd);       
        try 
        {   
            Rfc2898DeriveBytes kd = new Rfc2898DeriveBytes(psbytes, salt, iterations);   
            TripleDES decAlg = TripleDES.Create();   
            decAlg.Key = kd.GetBytes(24);   
            decAlg.IV = IV;   
            MemoryStream memstr = new MemoryStream();   
            decrypt = new CryptoStream(memstr, decAlg.CreateDecryptor(), CryptoStreamMode.Write);   
            decrypt.Write(edata, 0, edata.Length);   
            decrypt.Flush();   
            decrypt.Close();  // this is REQUIRED.   
            byte[] cleartext = memstr.ToArray();   
            return cleartext;   
        }   
        catch (Exception e) 
        {   
            Console.WriteLine("Problem decrypting: {0}", e.Message);   
            return null;   
        }   
    }             |