Ver Mensaje Individual
  #176 (permalink)  
Antiguo 21/09/2010, 15:47
scorpionsdd
 
Fecha de Ingreso: septiembre-2010
Mensajes: 18
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: Factura electroncia sat mexico

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;

}

}

public static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)
{

// 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[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };

byte[] seq = new byte[15];

// --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------

MemoryStream mem = new MemoryStream(pkcs8);

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;





bt = binr.ReadByte();

if (bt != 0x02)

return null;



twobytes = binr.ReadUInt16();



if (twobytes != 0x0001)

return null;



seq = binr.ReadBytes(15); //read the Sequence OID

if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct

return null;



bt = binr.ReadByte();

if (bt != 0x04) //expect an Octet string

return null;



bt = binr.ReadByte(); //read next byte, or next 2 bytes is 0x81 or 0x82; otherwise bt is the byte count

if (bt == 0x81)

binr.ReadByte();

else

if (bt == 0x82)

binr.ReadUInt16();

//------ at this stage, the remaining sequence should be the RSA private key



byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position));

RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);

return rsacsp;

}



catch (Exception)
{

return null;

}



finally { binr.Close(); }



}

public static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{

byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;



// --------- Set up stream to decode the asn.1 encoded RSA private key ------

MemoryStream mem = new MemoryStream(privkey);

BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading

byte bt = 0;

ushort twobytes = 0;

int elems = 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();

if (twobytes != 0x0102) //version number

return null;

bt = binr.ReadByte();

if (bt != 0x00)

return null;





//------ all private key components are Integer sequences ----

elems = GetIntegerSize(binr);

MODULUS = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

E = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

D = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

P = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

Q = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

DP = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

DQ = binr.ReadBytes(elems);



elems = GetIntegerSize(binr);

IQ = binr.ReadBytes(elems);



Console.WriteLine("showing components ..");

if (verbose)
{

showBytes("\nModulus", MODULUS);

showBytes("\nExponent", E);

showBytes("\nD", D);

showBytes("\nP", P);

showBytes("\nQ", Q);

showBytes("\nDP", DP);

showBytes("\nDQ", DQ);

showBytes("\nIQ", IQ);

}



// ------- create RSACryptoServiceProvider instance and initialize with public key -----

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

RSAParameters RSAparams = new RSAParameters();

RSAparams.Modulus = MODULUS;

RSAparams.Exponent = E;

RSAparams.D = D;

RSAparams.P = P;

RSAparams.Q = Q;

RSAparams.DP = DP;

RSAparams.DQ = DQ;

RSAparams.InverseQ = IQ;

RSA.ImportParameters(RSAparams);

return RSA;

}

catch (Exception)
{

return null;

}

finally { binr.Close(); }

}