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

Résolu/Fermé
Anonyme209 Messages postés 678 Date d'inscription samedi 6 octobre 2012 Statut Membre Dernière intervention 22 décembre 2020 - 17 déc. 2014 à 09:34
Anonyme209 Messages postés 678 Date d'inscription samedi 6 octobre 2012 Statut Membre Dernière intervention 22 décembre 2020 - 17 déc. 2014 à 20:39
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.
A voir également:

4 réponses

NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 331
17 déc. 2014 à 12:06
Quelle version de VB ?
En VB.NET, au lieu de prendre System.Test.Encoding.ASCII, utilises UTF8
0
Anonyme209 Messages postés 678 Date d'inscription samedi 6 octobre 2012 Statut Membre Dernière intervention 22 décembre 2020 15
17 déc. 2014 à 13:39
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
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 331
17 déc. 2014 à 18:35
System.Text.ASCIIEncoding.ASCII c'est la même chose que System.Text.Encoding.ASCII
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
17 déc. 2014 à 18:19
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
Anonyme209 Messages postés 678 Date d'inscription samedi 6 octobre 2012 Statut Membre Dernière intervention 22 décembre 2020 15
17 déc. 2014 à 20:39
Merci,

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