Ver Mensaje Individual
  #226 (permalink)  
Antiguo 05/10/2010, 10:36
AntonioMatias
 
Fecha de Ingreso: octubre-2005
Mensajes: 47
Antigüedad: 18 años, 6 meses
Puntos: 0
Información Respuesta: Factura electroncia sat mexico

Public Shared Function DecryptPBDK2(ByVal edata As Byte(), ByVal salt As Byte(), ByVal IV As Byte(), ByVal secpswd As SecureString, ByVal iterations As Integer) As Byte()
Dim decrypt As CryptoStream = Nothing
'CryptoStream decrypt = null;

Dim unmanagedPswd As IntPtr = IntPtr.Zero
Dim psbytes As Byte() = New Byte(secpswd.Length) {}
unmanagedPswd = Marshal.SecureStringToGlobalAllocAnsi(secpswd)
Marshal.Copy(unmanagedPswd, psbytes, 0, psbytes.Length)
Marshal.ZeroFreeGlobalAllocAnsi(unmanagedPswd)

Try
Dim kd As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(psbytes, salt, iterations)
Dim decAlg As TripleDES = TripleDES.Create()
decAlg.Key = kd.GetBytes(24)
decAlg.IV = IV
Dim memstr As MemoryStream = New MemoryStream()
decrypt = New CryptoStream(memstr, decAlg.CreateDecryptor(), CryptoStreamMode.Write)
decrypt.Write(edata, 0, edata.Length)
decrypt.Flush()
decrypt.Close() ' this is REQUIRED.
Dim cleartext As Byte() = memstr.ToArray()
Return cleartext

Catch ex As Exception
Console.WriteLine("Problem decrypting: {0}", ex.Message)
Return Nothing
End Try

End Function

Public Shared Function DecodePrivateKeyInfo(ByVal pkcs8 As Byte()) As RSACryptoServiceProvider
'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
Dim SeqOID As Byte() = {&H30, &HD, &H6, &H9, &H2A, &H86, &H48, &H86, &HF7, &HD, &H1, &H1, &H1, &H5, &H0}
Dim seq As Byte() = New Byte(15) {}

'--------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
Dim mem As MemoryStream = New MemoryStream(pkcs8)
Dim lenstream As Integer = CInt(mem.Length)
Dim binr As BinaryReader = New BinaryReader(mem) 'wrap Memory Stream with BinaryReader for easy reading
Dim bt As Byte = 0
Dim twobytes As UShort = 0

Try
twobytes = binr.ReadUInt16()
If (twobytes = &H8130) Then 'data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte() 'advance 1 byte
ElseIf (twobytes = &H8230) Then
binr.ReadInt16() 'advance 2 bytes
Else
Return Nothing
End If

bt = binr.ReadByte()
If (bt <> &H2) Then
Return Nothing
End If

twobytes = binr.ReadUInt16()
If (bt <> &H1) Then
Return Nothing
End If

seq = binr.ReadBytes(15) 'read the Sequence OID
If (Not CompareBytearrays(seq, SeqOID)) Then 'make sure Sequence for OID is correct
Return Nothing
End If

bt = binr.ReadByte()
If (bt <> &H4) Then
Return Nothing
End If

bt = binr.ReadByte() 'read next byte, or next 2 bytes is 0x81 or 0x82; otherwise bt is the byte count
If (bt = &H81) Then
binr.ReadByte()
ElseIf (bt = &H82) Then
binr.ReadUInt16()
End If

'------ at this stage, the remaining sequence should be the RSA private key
Dim rsaprivkey As Byte() = binr.ReadBytes(CInt(lenstream - mem.Position))
Dim rsacsp As RSACryptoServiceProvider = DecodeRSAPrivateKey(rsaprivkey)
Return rsacsp

Catch ex As Exception
Return Nothing
' ''return null;
Finally
binr.Close()
End Try
End Function

Public Shared Function DecodeRSAPrivateKey(ByVal privkey As Byte()) As RSACryptoServiceProvider
Dim MODULUS, E, D, P, Q, DP, DQ, IQ As Byte()

'--------- Set up stream to decode the asn.1 encoded RSA private key ------
Dim mem As MemoryStream = New MemoryStream(privkey)
Dim binr As BinaryReader = New BinaryReader(mem) 'wrap Memory Stream with BinaryReader for easy reading
Dim bt As Byte = 0
Dim twobytes As UShort = 0
Dim elems As Integer = 0

Try
twobytes = binr.ReadUInt16()
If (twobytes = &H8130) Then 'data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte() 'advance 1 byte
ElseIf (twobytes = &H8230) Then
binr.ReadInt16() 'advance 2 bytes
Else
Return Nothing
End If

twobytes = binr.ReadUInt16()
If (twobytes <> &H102) Then 'version number
Return Nothing
End If
bt = binr.ReadByte()
If (bt <> &H0) Then
Return Nothing
End If

'------ 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) Then
showBytes("\nModulus", MODULUS)
showBytes("\nExponent", E)
showBytes("\nD", D)
showBytes("\nP", P)
showBytes("\nQ", Q)
showBytes("\nDP", DP)
showBytes("\nDQ", DQ)
showBytes("\nIQ", IQ)
End If

'------- create RSACryptoServiceProvider instance and initialize with public key -----
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider()
Dim RSAparams As RSAParameters = 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 ex As Exception
Return Nothing
End Try
binr.Close()
End Function

Public Shared Function GetIntegerSize(ByVal binr As BinaryReader) As Integer

Dim bt As Byte = 0
Dim lowbyte As Byte = &H0
Dim highbyte As Byte = &H0
Dim count As Integer = 0
bt = binr.ReadByte()
If (bt <> &H2) Then 'expect integer
Return 0
End If
bt = binr.ReadByte()

If (bt = &H81) Then
count = binr.ReadByte() 'data size in next byte
ElseIf (bt = &H82) Then
highbyte = binr.ReadByte() 'data size in next 2 bytes
lowbyte = binr.ReadByte()
Dim modint As Byte() = {lowbyte, highbyte, &H0, &H0}
count = BitConverter.ToInt32(modint, 0)
Else
count = bt 'we already have the data size
End If

While (binr.ReadByte() = &H0) 'remove high order zeros in data
count -= 1
End While
binr.BaseStream.Seek(-1, SeekOrigin.Current) 'last ReadByte wasn't a removed zero, so back up a byte
Return count

End Function