Cita:  
					Iniciado por jcgalle  
  Hola cres que pudieras compartir con nosotros el codigo: clsSeguridad.DecodeEncryptedPrivateKeyInfo
    Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Diagnostics
Imports System.ComponentModel  
Public Class clsSeguridad_2
    Shared verbose As Boolean = False 
    Private Sub SAT()
        Dim aa As String = ""
    End Sub 
    Public Shared Function CodificarMD5(ByVal input As String) As String
        Dim CadenaUTF8 As Byte()
        Dim tmpHash As Byte()
        'convierte en UTF8
        CadenaUTF8 = Encoding.UTF8.GetBytes(input)
        'crea el hash
        tmpHash = New MD5CryptoServiceProvider().ComputeHash(CadenaUTF8)
        Dim i As Integer
        Dim sOutput As StringBuilder = New StringBuilder(tmpHash.Length) 
        For i = 0 To tmpHash.Length - 1 Step 1
            sOutput.Append(tmpHash(i).ToString("x2"))
        Next 
        Return sOutput.ToString() 
    End Function 
    Public Shared Function DecodeEncryptedPrivateKeyInfo(ByVal encpkcs8 As Byte(), ByVal lSecStr As SecureString) As RSACryptoServiceProvider 
        Dim OIDpkcs5PBES2 As Byte() = {&H6, &H9, &H2A, &H86, &H48, &H86, &HF7, &HD, &H1, &H5, &HD}
        Dim OIDpkcs5PBKDF2 As Byte() = {&H6, &H9, &H2A, &H86, &H48, &H86, &HF7, &HD, &H1, &H5, &HC}
        Dim OIDdesEDE3CBC As Byte() = {&H6, &H8, &H2A, &H86, &H48, &H86, &HF7, &HD, &H3, &H7} 
        Dim seqdes As Byte() = New Byte(10) {}
        Dim seq As Byte() = New Byte(10) {}
        Dim salt As Byte()
        Dim IV As Byte()
        Dim encryptedpkcs8 As Byte()
        Dim pkcs8 As Byte()
        Dim saltsize, ivsize, encblobsize As Integer
        Dim iterations As Integer 
        ' '' --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
        Dim mem As MemoryStream = New MemoryStream(encpkcs8)
        Dim lenstream As Integer = Int(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 
            twobytes = binr.ReadUInt16() 'inner sequence
            If (twobytes = &H8130) Then
                binr.ReadByte()
            ElseIf (twobytes = &H8230) Then
                binr.ReadInt16()
            End If 
            seq = binr.ReadBytes(11) 'read the Sequence OID
            If (Not CompareBytearrays(seq, OIDpkcs5PBES2)) Then 'is it a OIDpkcs5PBES2 ?
                Return Nothing
            End If  
            twobytes = binr.ReadUInt16() 'inner sequence for pswd salt
            If (twobytes = &H8130) Then
                binr.ReadByte()
            ElseIf (twobytes = &H8230) Then
                binr.ReadInt16()
            End If 
            twobytes = binr.ReadUInt16() 'inner sequence for pswd salt
            If (twobytes = &H8130) Then
                binr.ReadByte()
            ElseIf (twobytes = &H8230) Then
                binr.ReadInt16()
            End If 
            seq = binr.ReadBytes(11) 'read the Sequence OID
            If (Not CompareBytearrays(seq, OIDpkcs5PBES2)) Then 'is it a OIDpkcs5PBES2 ?
                Return Nothing
            End If 
            twobytes = binr.ReadUInt16() 'inner sequence for pswd salt
            If (twobytes = &H8130) Then
                binr.ReadByte()
            ElseIf (twobytes = &H8230) Then
                binr.ReadInt16()
            End If 
            bt = binr.ReadByte()
            If (bt <> &H4) Then
                Return Nothing
            End If
            saltsize = binr.ReadByte()
            salt = binr.ReadBytes(saltsize) 
            If (verbose) Then
                'showBytes("Salt for pbkd", salt)
            End If
            bt = binr.ReadByte() 
            If (bt <> &H2) Then 'expect an integer for PBKF2 interation count
                Return Nothing
            End If 
            Dim itbytes As Integer = binr.ReadByte() 'PBKD2 iterations should fit in 2 bytes.
            If (itbytes = 1) Then
                iterations = 256 * binr.ReadByte() + binr.ReadByte()
            Else
                Return Nothing
            End If 
            If (verbose) Then
                Console.WriteLine("PBKD2 iterations {0}", iterations)
            End If 
            twobytes = binr.ReadUInt16()
            If (twobytes = &H8130) Then
                binr.ReadByte()
            ElseIf (twobytes = &H8230) Then
                binr.ReadInt16()
            End If 
            seqdes = binr.ReadBytes(10) 'read the Sequence OID
            If (Not CompareBytearrays(seqdes, OIDdesEDE3CBC)) Then 'is it a OIDdes-EDE3-CBC ?
                Return Nothing
            End If 
            bt = binr.ReadByte()
            If (bt <> &H4) Then 'expect octet string for IV
                Return Nothing
            End If
            ivsize = binr.ReadByte() 'IV byte size should fit in one byte (24 expected for 3DES)
            IV = binr.ReadBytes(ivsize)
            If (verbose) Then
                'showBytes("IV for des-EDE3-CBC", IV)
            End If 
            bt = binr.ReadByte()
            If (bt <> &H4) Then 'expect octet string for encrypted PKCS8 data
                Return Nothing
            End If 
            bt = binr.ReadByte()
            'bt = binr.ReadByte();  
            If (bt = &H81) Then
                encblobsize = binr.ReadByte() ' data size in next byte
            ElseIf (bt = &H82) Then
                encblobsize = 256 * binr.ReadByte() + binr.ReadByte()
            Else
                encblobsize = bt ' we already have the data size
            End If 
            encryptedpkcs8 = binr.ReadBytes(encblobsize) 
            Dim secpswd As SecureString = lSecStr 'GetSecPswd(lSecStr.ToString());
            pkcs8 = DecryptPBDK2(encryptedpkcs8, salt, IV, secpswd, iterations)
            If (pkcs8 Is Nothing) Then ' probably a bad pswd entered.
                Return Nothing
            End If 
            ' ----- With a decrypted pkcs #8 PrivateKeyInfo blob, decode it to an RSA ---
            Dim rsa As RSACryptoServiceProvider = DecodePrivateKeyInfo(pkcs8)
            Return rsa 
        Catch ex As Exception
            Return Nothing
        Finally
            binr.Close()
        End Try 
    End Function 
    Private Shared Function CompareBytearrays(ByVal a As Byte(), ByVal b As Byte()) As Boolean
        If (a.Length <> b.Length) Then
            Return False
        End If 
        Dim i As Integer = 0
        For Each c As Byte In a
            If (c <> b(i)) Then
                Return False
                i += 1
            End If
        Next
        Return True
    End Function