VB.NET: Problème avec les accents AES-128

Résolu
Anonyme209 Messages postés 761 Statut Membre -  
Anonyme209 Messages postés 761 Statut Membre -
Bonjour,

Pour crypter des données sur Visual Basic, j'utilise souvent l'algorithme AES (128 bits).
Mais j'ai remarqué que les accents s'affichaient "?" lors du décryptage.
J'aimerais savoir comment faire pour éviter cela.

Merci de votre aide.

4 réponses

  1. NHenry Messages postés 15235 Date d'inscription   Statut Modérateur Dernière intervention   387
     
    Quelle version de VB ?
    En VB.NET, au lieu de prendre System.Test.Encoding.ASCII, utilises UTF8
    0
  2. Anonyme209 Messages postés 761 Statut Membre 19
     
    C'est du VB.NET
    Je n'ai pas trouvé "System.Text.Encoding.ASCII" ou "System.Text.Encoding.ASCII" dans les fonctions AES_Encrypt ou AES_Decrypt.
    Peut-être que ma fonction n'est pas correcte.
    Dans ce cas laquelle dois-je utiliser?

     Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = Security.Cryptography.CipherMode.ECB
                Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
                encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return encrypted
            Catch ex As Exception
            End Try
        End Function
    
        Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim decrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = Security.Cryptography.CipherMode.ECB
                Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
                Dim Buffer As Byte() = Convert.FromBase64String(input)
                decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return decrypted
            Catch ex As Exception
            End Try
        End Function
    0
    1. NHenry Messages postés 15235 Date d'inscription   Statut Modérateur Dernière intervention   387
       
      System.Text.ASCIIEncoding.ASCII c'est la même chose que System.Text.Encoding.ASCII
      0
  3. cs_Le Pivert Messages postés 8437 Statut Contributeur 730
     
    Bonjour,

    Voici avec les caractères accentués:

    Imports System.IO
    Imports System.Text
    Public Class Form1
        Dim strContent As String
        Dim path As String = "C:\Users\Daniel\Documents\MyTest.txt" 'a adapter
    
        Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.Encoding.Default.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = Security.Cryptography.CipherMode.ECB
                Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Dim Buffer As Byte() = System.Text.Encoding.Default.GetBytes(input)
                encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return encrypted
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
    
        Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim decrypted As String = ""
            Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.Encoding.Default.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = System.Text.Encoding.Default.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return decrypted
             Catch ex As Exception
            Return Nothing
            End Try
        End Function
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            strContent = "éèàaztocéà"
            strContent = AES_Encrypt(strContent, "somepassword")
            File.WriteAllText(path, strContent)
        End Sub
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            strContent = "DeSIW7QBVFGZYY/PFjOvpA=="
            strContent = AES_Decrypt(strContent, "somepassword")
            File.WriteAllText(path, strContent)
        End Sub
    End Class
    

    0
  4. Anonyme209 Messages postés 761 Statut Membre 19
     
    Merci,

    avec ce code les accents sont affichés correctement même après le décryptage.
    0