IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Le remplissage n'est pas valide et ne peut pas être supprimé (AES)


Sujet :

VB.NET

  1. #1
    Membre du Club Avatar de Cybercope
    Homme Profil pro
    Programmeur amateur
    Inscrit en
    Mai 2014
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Programmeur amateur

    Informations forums :
    Inscription : Mai 2014
    Messages : 78
    Points : 59
    Points
    59
    Par défaut Le remplissage n'est pas valide et ne peut pas être supprimé (AES)
    Bonjours à tous !

    Je rencontre un problème assez pénible. Je développe un logiciel qui utilise la cryptographie. Celui ci crypte une string avant d'écrire la string cryptée dans un fichier .txt . Cependant, je rencontre un problème à la lecture.

    L'algorithme utilisé est AES.

    Voici mon code :

    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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
     
    Imports System.IO
    Imports System.Security.Cryptography
     
    Public Class Client
     
    #Region "Variables"
     
        Dim Counter1 As Integer = 0
        Dim path As String = "J:\test.txt"
        Dim myAes As Aes = Aes.Create()
     
    #End Region
     
    #Region "Procédures"
     
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            crypt()
        End Sub
     
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            decrypt()
        End Sub
     
    #End Region
     
    #Region "CRYPT"
     
        Private Sub crypt()
     
     
            Dim sw As StreamWriter = New StreamWriter(path, False)
     
     
            For i = 0 To ListBox1.Items.Count - 1 Step 1
     
                    Dim original As String = AssemblyTextForCrypt(i)
                    Dim encrypted As Byte() = EncrypText(original, myAes.Key, myAes.IV)
                    Dim Final As String = System.Text.Encoding.Unicode.GetString(encrypted)
                    sw.WriteLine(Final)
                Next
                sw.Flush()
            sw.Close()
        End Sub
     
        Function AssemblyTextForCrypt(ByVal index As Integer)
     
            Dim assembly As String = ""
            assembly = ListBox1.Items.Item(index) + "|" + ListBox2.Items.Item(index) + "|" + ListBox3.Items.Item(index) + "|" + ListBox4.Items.Item(index)
            Return assembly
     
        End Function
     
        Function EncrypText(ByVal Text As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
     
            Dim encrypted() As Byte
            ' Create an Aes object
            ' with the specified key and IV.
     
            ' Create a decrytor to perform the stream transform.
            Dim encryptor As ICryptoTransform = myAes.CreateEncryptor(myAes.Key, myAes.IV)
            ' Create the streams used for encryption.
            Using msEncrypt As New MemoryStream()
                Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)
     
                        'Write all data to the stream.
                        swEncrypt.Write(Text)
                    End Using
                    encrypted = msEncrypt.ToArray()
                End Using
            End Using
     
            ' Return the encrypted bytes from the memory stream.
            Return encrypted
     
        End Function
     
    #End Region
     
    #Region "DECRYPT"
     
        Sub decrypt()
     
            Dim sr As StreamReader = New StreamReader(path)
            Dim ligne() As Byte
     
            Do
                ligne = System.Text.Encoding.Unicode.GetBytes(sr.ReadLine)
                Dim decrypted As String = DecrypText(ligne, myAes.Key, myAes.IV)
                MsgBox(ligne.ToString)
                Dim List() As String = DesassemblyForRead(decrypted)
                Place(List)
            Loop Until ligne Is Nothing
            sr.Close()
     
        End Sub
     
        Function DecrypText(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
            ' Declare the string used to hold
            ' the decrypted text.
            Dim plaintext As String = Nothing
     
            ' Create an Aes object
            ' with the specified key and IV.
     
            ' Create a decrytor to perform the stream transform.
            Dim decryptor As ICryptoTransform = myAes.CreateDecryptor(myAes.Key, myAes.IV)
     
            ' Create the streams used for decryption.
            Using msDecrypt As New MemoryStream(cipherText)
     
                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
     
                    Using srDecrypt As New StreamReader(csDecrypt)
     
                        ' Read the decrypted bytes from the decrypting stream
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd
     
                    End Using
                End Using
            End Using
     
            Return plaintext
     
        End Function 'DecryptStringFromBytes_Aes 
     
     
        Function DesassemblyForRead(ByVal line As String)
     
            Dim List() As String = Nothing
            List = line.Split("|")
            Return List
     
        End Function
     
        Sub Place(ByVal Arr() As String)
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            ListBox3.Items.Clear()
            ListBox4.Items.Clear()
            ListBox1.Items.Add(Arr(0))
            ListBox2.Items.Add(Arr(1))
            ListBox3.Items.Add(Arr(2))
            ListBox4.Items.Add(Arr(3))
        End Sub
     
    #End Region

    Le cryptage et l'écriture se déroule sans problème.

    J'ai une erreur du type : System.Security.Cryptography.CryptographicException: Le remplissage n'est pas valide et ne peut pas être supprimé.
    sur la ligne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     plaintext = srDecrypt.ReadToEnd
    J'ai essayer de retourner un tableau de bytes au lieu d'une string mais le problème persiste.

    N'étant pas expert en la matière, j'ai du mal a voir d'ou vient cette erreur.

    NB: J'ai déjà cherché sur google, mais je n'ai rien trouvé qui fonctionne ou qui puisse m'aider.

    Merci de votre aide !

    Ind6x

  2. #2
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Points : 0
    Points
    0
    Par défaut
    Bonjour,
    voici la solution a ton probléme :
    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
    Private Sub Decrypt(entrer As String, sortie As String, key As String)
            Dim DES As New DESCryptoServiceProvider()
            DES.Key() = ASCIIEncoding.ASCII.GetBytes(key)
            DES.IV = ASCIIEncoding.ASCII.GetBytes(key)
            Dim fsread As New FileStream(entrer, FileMode.Open, FileAccess.Read)
            Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
            Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
            Dim fsDecrypted As New StreamWriter(sortie)
            fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
            fsDecrypted.Flush()
            fsDecrypted.Close()
        End Sub
     
        Private Sub Encrypt(entrer As String, sortie As String, key As String)
            Dim fsInput As New FileStream(entrer, FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sortie, FileMode.Create, FileAccess.Write)
            Dim DES As New DESCryptoServiceProvider()
            DES.Key = ASCIIEncoding.ASCII.GetBytes(key)
            DES.IV = ASCIIEncoding.ASCII.GetBytes(key)
            Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
            Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
            Dim bytearrayinput(fsInput.Length - 1) As Byte
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
        End Sub
     
        Function CreateKey() As String
            Dim DesC As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
            Return ASCIIEncoding.ASCII.GetString(DesC.Key)
        End Function
    exemple : encrypt(C:\Users\priver\Desktop\teste.txt,C:\Users\priver\Desktop\teste2.txt,CreateKey)

    cordialement,

  3. #3
    Membre du Club Avatar de Cybercope
    Homme Profil pro
    Programmeur amateur
    Inscrit en
    Mai 2014
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Programmeur amateur

    Informations forums :
    Inscription : Mai 2014
    Messages : 78
    Points : 59
    Points
    59
    Par défaut
    Bonjour,

    Merci de votre réponse. Cependant, je ne souhaite pas utiliser DES mais AES. De plus, il ne s'agit pas de fichier que je souhaite crypter mais d'un flux.

    J'ai retirer les using et end using dans le code source et j'ai fait des déclaration simple avec Dim. Parfois cela fonctionne, mais après le problème resurgit.

    Merci

    Ind6x

  4. #4
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Points : 0
    Points
    0
    Par défaut
    si vous voulez plus information vous avez la réponde dans ce lien : https://nciphers.com/aes/
    Décrypter et encrypter AES.
    Cordialement,

  5. #5
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Points : 0
    Points
    0
    Par défaut
    si tu veux un exemple , voici la solution :
    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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    Imports System.IO
    Imports System.Security.Cryptography
    Imports System.Text
     
    Public Class Form1
        Private SecretKey(15) As Byte
        Private SecretIV(15) As Byte
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim open As New OpenFileDialog
            open.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
            open.Filter = "Text Files (.txt)|*.txt"
            If open.ShowDialog = Windows.Forms.DialogResult.OK Then
                TextBox1.Text = open.FileName
            Else
                Exit Sub
            End If
        End Sub
     
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim save As New SaveFileDialog
            save.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
            save.Filter = "Text Files (.txt)|*.txt"
            If save.ShowDialog = Windows.Forms.DialogResult.OK Then
                TextBox2.Text = save.FileName
            Else
                Exit Sub
            End If
        End Sub
     
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            If TextBox1.Text.Length > 0 OrElse TextBox2.Text.Length > 0 Then
                EncryptFile(TextBox1.Text, TextBox2.Text)
                MessageBox.Show("fichier crypter!")
            Else
                MessageBox.Show("Veuillez séléctionner un fichier entrer et sortie !", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub
     
        Private Sub EncryptFile(ByVal InputFilename As String, ByVal OutputFilename As String)
            Dim AES As New AesCryptoServiceProvider()
            Dim AesDecrypt As ICryptoTransform = AES.CreateEncryptor(SecretKey, SecretIV)
            Using fsInput As New FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                Using fsOutput As New FileStream(OutputFilename, FileMode.Create, FileAccess.Write, FileShare.None)
                    Using cryptostream As New CryptoStream(fsOutput, AesDecrypt, CryptoStreamMode.Write)
                        Dim bytearrayinput(fsInput.Length - 1) As Byte
                        Dim bytesRead As Long = 0
                        bytesRead = fsInput.Read(bytearrayinput, 0, fsInput.Length)
                        If Not (bytesRead = 0) Then
                            cryptostream.Write(bytearrayinput, 0, bytesRead)
                            cryptostream.Flush()
                        End If
                    End Using
                End Using
            End Using
        End Sub
     
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            If TextBox1.Text.Length > 0 OrElse TextBox2.Text.Length > 0 Then
                DécryptFile(TextBox1.Text, TextBox2.Text)
                MessageBox.Show("Fichier Décrypter!")
            Else
                MessageBox.Show("Veuillez séléctionner un fichier entrer et sortie !", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub
     
        Private Sub DécryptFile(ByVal InputFilename As String, ByVal OutputFilename As String)
            Dim AES As New AesCryptoServiceProvider()
            Dim AesDecrypt As ICryptoTransform = AES.CreateDecryptor(SecretKey, SecretIV)
            Using fsread As New FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                Using cryptostreamDecr As New CryptoStream(fsread, AesDecrypt, CryptoStreamMode.Read)
                    Using fsDecrypted As New FileStream(OutputFilename, FileMode.Create, FileAccess.Write, FileShare.None)
                        Dim bytearrayinput(fsread.Length) As Byte
                        Dim bytesRead As Long = 0
                        bytesRead = cryptostreamDecr.Read(bytearrayinput, 0, fsread.Length)
                        If (bytesRead > 0) Then
                            fsDecrypted.Write(bytearrayinput, 0, bytesRead)
                            fsDecrypted.Flush()
                        End If
                    End Using
                End Using
            End Using
        End Sub
     
    End Class

Discussions similaires

  1. [AC-2003] Le pas d'une minute peut-il être variable
    Par KonTiKI dans le forum Access
    Réponses: 3
    Dernier message: 17/05/2016, 14h37
  2. Réponses: 2
    Dernier message: 05/12/2014, 14h44
  3. Réponses: 11
    Dernier message: 08/05/2014, 18h10
  4. Ne peut pas valider customVAlidator
    Par phfle1 dans le forum ASP.NET
    Réponses: 1
    Dernier message: 30/06/2008, 21h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo