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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
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 prersa(a.ReceiveBufferSize) As Byte
Dim index = b.Read(prersa, 0, a.ReceiveBufferSize)
b.Flush()
Dim rsa(index - 1) As Byte
For i = 0 To index - 1
rsa(i) = prersa(i)
Next
Console.WriteLine("Public key reçue:" & System.Text.Encoding.UTF8.GetString(rsa))
'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
b.Write(rsachiffrement.Encrypt(keytoencrypt, False), 0, rsachiffrement.Encrypt(keytoencrypt, False).Count)
b.Flush()
Dim text As String = ""
For i = 0 To rsachiffrement.Encrypt(keytoencrypt, False).Count - 1
text += rsachiffrement.Encrypt(keytoencrypt, False)(i).ToString("x2")
Next
Console.WriteLine("Key : " & text)
Dim aeschiffrement As New System.Security.Cryptography.AesCryptoServiceProvider
aeschiffrement.Mode = System.Security.Cryptography.CipherMode.CBC
Buffer.BlockCopy(keytoencrypt, 0, aeschiffrement.Key, 0, 32)
Buffer.BlockCopy(keytoencrypt, 32, aeschiffrement.IV, 0, 16)
Dim key1 As String = ""
Dim IV1 As String = ""
For i = 0 To aeschiffrement.Key.Count - 1
key1 += aeschiffrement.Key(i).ToString("x2")
Next
For i = 0 To aeschiffrement.IV.Count - 1
IV1 += aeschiffrement.IV(i).ToString("x2")
Next
Console.WriteLine(key1 & ":" & IV1)
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
IO.File.WriteAllText("crypted.txt", Convert.ToBase64String(aes.ToArray))
b.Write(IO.File.ReadAllBytes("crypted.txt"), 0, IO.File.ReadAllBytes("crypted.txt").Count)
b.Flush()
IO.File.Delete("crypted.txt")
Console.WriteLine("Longueur : " & aes.ToArray.Count)
Console.WriteLine("Client : " & getinfos) |
Partager