Bonjour,

Je sais que lors du chiffrement avec AES, on utilise ConvertFromString and ConvertToString mais en utilisant les sockets, je dois rester en byte et lors du déchiffrement par le serveur, le serveur transmet une erreur : "The padding is invalid and cannot be removed."
Partie serveur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
Friend Function serverconnexion(ByVal sender As System.Object, e As System.EventArgs)
        Try
            Dim c As New TcpListener(Net.IPAddress.Parse("127.0.0.1"), "7008")
            c.Start()
            Console.WriteLine("starting...")
            Dim a As TcpClient = c.AcceptTcpClient()
            Dim b As New NetworkStream(a.Client)
            Dim rsachiffrement As New System.Security.Cryptography.RSACryptoServiceProvider
            b.Write(System.Text.Encoding.UTF8.GetBytes(rsachiffrement.ToXmlString(False)), 0, System.Text.Encoding.UTF8.GetBytes(rsachiffrement.ToXmlString(False)).Count)
            Dim rsa1(a.ReceiveBufferSize) As Byte
            Dim index As Integer = b.Read(rsa1, 0, a.ReceiveBufferSize)
            Dim rsa2(index - 1) As Byte
            For i = 0 To index - 1
                rsa2(i) = rsa1(i)
            Next
            Dim rsa() As Byte = rsachiffrement.Decrypt(rsa2, False)
            Dim aeschiffrement As New System.Security.Cryptography.AesCryptoServiceProvider
            aeschiffrement.Mode = Security.Cryptography.CipherMode.CFB
            Buffer.BlockCopy(rsa, 0, aeschiffrement.Key, 0, 32)
            Buffer.BlockCopy(rsa, 32, aeschiffrement.IV, 0, 16)
            Dim aesencryptor = aeschiffrement.CreateEncryptor
            Dim aesdecryptor = aeschiffrement.CreateDecryptor
            Dim aes1(a.ReceiveBufferSize) As Byte
            Dim index1 As Integer = b.Read(aes1, 0, a.ReceiveBufferSize)
            Dim aes2(index1 - 1) As Byte
            For i = 0 To index1 - 1
                aes2(i) = aes1(i)
            Next
            Dim aes As New MemoryStream
            Using s As New System.Security.Cryptography.CryptoStream(aes, aesdecryptor, Security.Cryptography.CryptoStreamMode.Write)
                s.Write(aes2, 0, aes2.Count)
            End Using
            Console.WriteLine("Longueur serveur : " & aes.Length)
            Console.WriteLine("Server : " & aes.ToArray.Count)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Function
Partie cliente :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
Dim a As New TcpClient
        Console.WriteLine("starting2...")
        a.Connect("127.0.0.1", "7008")
        Console.WriteLine("Ok")
        Dim b As New NetworkStream(a.Client)
        Dim rsa(a.ReceiveBufferSize) As Byte
        b.Read(rsa, 0, a.ReceiveBufferSize)
        'Fabrication de la clé aléatoire
        Dim key As String = ""
        Dim characters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwyz0123456789"
        Randomize()
        For i = 0 To 16
            key += characters(Math.Floor(Rnd() * characters.Length))
        Next
        Dim process As New System.Security.Cryptography.Rfc2898DeriveBytes(key, 8, 10000)
        Dim keytoencrypt() As Byte = process.GetBytes(48)
        Dim rsachiffrement As New System.Security.Cryptography.RSACryptoServiceProvider
        rsachiffrement.FromXmlString(System.Text.Encoding.UTF8.GetString(rsa))
        b.Write(rsachiffrement.Encrypt(keytoencrypt, False), 0, rsachiffrement.Encrypt(keytoencrypt, False).Count)
        Dim aeschiffrement As New System.Security.Cryptography.AesCryptoServiceProvider
        aeschiffrement.Mode = System.Security.Cryptography.CipherMode.CFB
        Buffer.BlockCopy(keytoencrypt, 0, aeschiffrement.Key, 0, 32)
        Buffer.BlockCopy(keytoencrypt, 32, aeschiffrement.IV, 0, 16)
        Dim aesencryptor = aeschiffrement.CreateEncryptor
        Dim aesdecryptor = aeschiffrement.CreateDecryptor
        Dim aes As New MemoryStream
        Using s As New System.Security.Cryptography.CryptoStream(aes, aesencryptor, System.Security.Cryptography.CryptoStreamMode.Write)
            s.Write(System.Text.Encoding.UTF8.GetBytes(getinfos), 0, System.Text.Encoding.UTF8.GetBytes(getinfos).Count)
        End Using
        b.Write(aes.ToArray, 0, aes.ToArray.Count)
        Console.WriteLine("Longueur : " & aes.Length)
        Console.WriteLine("Client : " & getinfos)
La longueur reçue des deux côtés est correct, les clés sont transmises correctement (je l'ai testé), le seul problème est donc le déchiffrement par AES via la clé et le IV envoyé qui ne fonctionne pas. getinfos() renvoie des données au format String séparés par des ":" tels que "A:B:C:D:10/10/1976:E". Quelqu'un a-t-il une idée? Merci.