VB/VB.Net RSA petit probleme

A

Anonymous

Invité
#1
Bonjour à tous,
Je voudrais savoir si vous pouviez m'aidez pour la MAJ car j'ai un petit souci.

Actuellement je prends la clé qui est envoyée par le serveur.
* Je la décrypte avec la verifyKey (j'ai lu dans les sources qu'il faisait comme ça).
Ensuite j'encrypte le mot de passe avec la clé decrypt et j’envoie.

* Mon problème se situe ici. J'utilise cette fonction :
Code:
 Private Shared Function DecodeX509PublicKey(ByVal x509Key As Byte()) As RSACryptoServiceProvider

        Dim seqID As Byte() = {&H30, &HD, &H6, &H9, &H2A, &H86, _
         &H48, &H86, &HF7, &HD, &H1, &H1, _
         &H1, &H5, &H0}
        Dim seq As Byte()

        Dim mem As New MemoryStream(x509Key)
        Dim binr As New BinaryReader(mem)

        Dim bt As Byte
        Dim twobytes As UShort

        Try

            twobytes = binr.ReadUInt16()
            If twobytes = &H8130 Then
                binr.ReadByte()
            ElseIf twobytes = &H8230 Then
                binr.ReadInt16()
            Else
                Return Nothing
            End If

            seq = binr.ReadBytes(15)
            If Not CompareByteArrays(seq, seqID) Then
                Return Nothing
            End If

            twobytes = binr.ReadUInt16()
            If twobytes = &H8103 Then
                binr.ReadByte()
            ElseIf twobytes = &H8203 Then
                binr.ReadInt16()
            Else
                Return Nothing
            End If

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

            twobytes = binr.ReadUInt16()
            If twobytes = &H8130 Then
                binr.ReadByte()
            ElseIf twobytes = &H8230 Then
                binr.ReadInt16()
            Else
                Return Nothing
            End If

            twobytes = binr.ReadUInt16()
            Dim lowbyte As Byte
            Dim highbyte As Byte = &H0

            If twobytes = &H8102 Then
                lowbyte = binr.ReadByte()
            ElseIf twobytes = &H8202 Then
                highbyte = binr.ReadByte()
                lowbyte = binr.ReadByte()
            Else
                Return Nothing
            End If

            Dim modint As Byte() = {lowbyte, highbyte, &H0, &H0}
            Dim modsize As Integer = BitConverter.ToInt32(modint, 0)

            Dim firstbyte As Byte = binr.ReadByte()
            binr.BaseStream.Seek(-1, SeekOrigin.Current)

            If firstbyte = &H0 Then
                binr.ReadByte()
                modsize -= 1
            End If

            Dim modulus As Byte() = binr.ReadBytes(modsize)

            If binr.ReadByte() <> &H2 Then
                Return Nothing
            End If
            Dim expbytes As Integer = CInt(binr.ReadByte())
            Dim exponent As Byte() = binr.ReadBytes(expbytes)

            Dim RSA As New RSACryptoServiceProvider()

            Dim RSAKeyInfo As New RSAParameters() With {
             .Modulus = modulus,
             .Exponent = exponent
            }

            RSA.ImportParameters(RSAKeyInfo)

            Return RSA

        Catch e As Exception

            Return Nothing

        Finally

            binr.Close()

        End Try

    End Function

Et ensuite je fais :

Code:
Dim rsa As RSACryptoServiceProvider = DecodeX509PublicKey(Convert.FromBase64String(verifyKey))
If rsa Is Nothing Then Return Nothing
Dim loc4 = rsa.Decrypt(text, False)
J'ai l'impression que je me plante grave car il me met des fois que la clé n'existe pas ou que ya un truc trop long. En réalité je sais pas comment décrypter en RSA avec une clé PEM .

Merci de votre aide.
 
Haut Bas