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 57
| Dim KeyAES As String = "Bonjour"
Private Function Crypter(ByVal pData As String, ByVal pKey As String) As String
'Création des objets de crypto
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
'Données clair string vers données clair byte
Dim DataClairByte As Byte() = System.Text.Encoding.UTF8.GetBytes(pData)
'Key string vers key byte
Dim KeyByte As Byte() = System.Text.Encoding.UTF8.GetBytes(pKey)
'Hashage de la key byte
Dim KeyHashByte As Byte() = Md5.ComputeHash(KeyByte)
'Key hash byte vers key hash hex
Dim StringBuilder As New System.Text.StringBuilder
For i As Integer = 0 To KeyHashByte.Length - 1
StringBuilder.AppendFormat("{0:X2}", KeyHashByte(i))
Next
Dim KeyHashHex As String = StringBuilder.ToString.ToLower
'Key / IV
Dim KeyUseHashHex As String = ""
Dim IVHex As String = ""
For i As Integer = 0 To KeyHashHex.Length - 1
If i Mod 2 = 0 Then
KeyUseHashHex = KeyUseHashHex + KeyHashHex(i)
Else
IVHex = IVHex + KeyHashHex(i)
End If
Next
Dim Key As Byte() = System.Text.Encoding.UTF8.GetBytes(KeyUseHashHex)
Dim IV As Byte() = System.Text.Encoding.UTF8.GetBytes(IVHex)
'Cryptage
AES.KeySize = 128
AES.Mode = System.Security.Cryptography.CipherMode.CBC
AES.Key = Key
AES.IV = IV
Dim Encrypteur As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor()
Dim MemoryStream As New System.IO.MemoryStream
Dim CryptoStream As New System.Security.Cryptography.CryptoStream(MemoryStream, Encrypteur, System.Security.Cryptography.CryptoStreamMode.Write)
CryptoStream.Write(DataClairByte, 0, DataClairByte.Length)
CryptoStream.FlushFinalBlock()
Dim DataCryptByte As Byte() = MemoryStream.ToArray
'Liveration des ressources
MemoryStream.Close()
CryptoStream.Close()
'Data Crypt Byte ver Data Crypt String
Dim DataCryptString As String = System.Convert.ToBase64String(DataCryptByte)
Return DataCryptString
End Function |
Partager