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

Dotnet Discussion :

pb de déchiffrement -AES


Sujet :

Dotnet

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    988
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 988
    Par défaut pb de déchiffrement -AES
    Bonjour,

    J'ai crée une fonction de déchiffrement du mot de passe avec la fonction suivante qui utilise l'algorithme de cryptographie AES

    dont voici le 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
     
     
     public  static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
                {
                    // Check arguments.
                    if (cipherText == null || cipherText.Length <= 0)
                        throw new ArgumentNullException("cipherText");
                    if (Key == null || Key.Length <= 0)
                        throw new ArgumentNullException("Key");
                    if (IV == null || IV.Length <= 0)
                        throw new ArgumentNullException("IV");
     
                    // Declare the string used to hold
                    // the decrypted text.
                    string password_decrypte = null;
     
                    // Create an AesCryptoServiceProvider object
                    // with the specified key and IV.
                    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                    {
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;
     
                        // Create a decrytor to perform the stream transform.
                        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
     
                        // Create the streams used for decryption.
                        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                        {
                            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                                {
     
                                // Read the decrypted bytes from the decrypting stream
                                // and place them in a string.
                                password_decrypte = srDecrypt.ReadToEnd();
                                }
                            }
                        }
     
                    }
     
                    return password_decrypte;
     
                }
    Voici que j'obtiens une erreur au niveau de cette instruction :
    srDecrypt.ReadToEnd();

    dont voici le texte


    Une erreur est survenue Les données d'entrée ne constituent pas un bloc complet.Son détail est à System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) à System.Security.Cryptography.CryptoStream.FlushFinalBlock()

    J'ai lu le post suivant http://www.developpez.net/forums/d13...aremment-null/ qui parle de mode du cipher

    J'ai ajouté cette instruction à la fonction DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) au niveau du using , comme conseillé par la personne qui a résolu son pb dans le post , mais sans resultat, le msg d'erreur étant le même.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                    {
                       aesAlg.Mode = CipherMode.CFB;
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;
    Si vous avez une suggestion à me faire, je vous en remercie beaucoup car je tourne un peu en rond avec cette situation.

    Bien cordialement.

    new_wave

  2. #2
    Expert confirmé

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 761
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    Peut-on aussi avoir le code de la méthode de chiffrage ? Souvent, ce genre d'erreur résulte souvent d'une configuration différente entre le chiffrement / déchiffrement, ou d'une corruption des données.

  3. #3
    Membre éprouvé
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    988
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 988
    Par défaut problème de chiffrement/dechiffrement AES
    Bonjour et merci de votre intérêt pour ma question.
    Voici le code de chiffrement.
    En espérant que cela vous permettra de trouver d'ou provient l'erreur!
    Cordialement.

    new_wave
    Dans la classe Forum_Registration
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     AesCryptoServiceProvider myAes = new AesCryptoServiceProvider();
     
     byte[] hashedMDP;   hashedMDP=cryptage_decryptage_AES.EncryptStringToBytes_Aes(txtpassword.Text, myAes.Key, myAes.IV);
    Dans la classe cryptage_decryptage_AES, la méthode statique EncryptStringToBytes_Aes
    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
     
      public static byte[] EncryptStringToBytes_Aes(string password_non_crypte, byte[] Key, byte[] IV)
                {
                    // Check arguments.
                    if (password_non_crypte == null || password_non_crypte.Length <= 0)
                        throw new ArgumentNullException("password non crypté ");
                    if (Key == null || Key.Length <= 0)
                        throw new ArgumentNullException("Key");
                    if (IV == null || IV.Length <= 0)
                        throw new ArgumentNullException("IV");
                    byte[] encrypted;
                    // Create an AesCryptoServiceProvider object
                    // with the specified key and IV.
                    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                    {
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;
     
                        // Create a decrytor to perform the stream transform.
                        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
     
                        // Create the streams used for encryption.
                        using (MemoryStream msEncrypt = new MemoryStream())
                        {
                            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                            {
                                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                                {
     
                                    //Write all data to the stream.
                                    swEncrypt.Write(password_non_crypte);
                                }
                                encrypted = msEncrypt.ToArray();
                            }
                        }
                    }
     
     
                    // Return the encrypted bytes from the memory stream.
                    return encrypted;
     
                }

  4. #4
    Expert confirmé

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 761
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    Sans tester, je dirais qu'il manque un FlushFinalBlock juste après le swEncrypt.Write.

  5. #5
    Membre éprouvé
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    988
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 988
    Par défaut pb de chiffrement/ dechiffrement-AES
    Bonjour,

    Voici le code de la fonction de chiffrement

    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
     
     
    public static byte[] EncryptStringToBytes_Aes(string password_non_crypte, byte[] Key, byte[] IV)
                {
                    // Check arguments.
                    if (password_non_crypte == null || password_non_crypte.Length <= 0)
                        throw new ArgumentNullException("password non crypté ");
                    if (Key == null || Key.Length <= 0)
                        throw new ArgumentNullException("Key");
                    if (IV == null || IV.Length <= 0)
                        throw new ArgumentNullException("IV");
                    byte[] encrypted;
                    // Create an AesCryptoServiceProvider object
                    // with the specified key and IV.
                    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                    {
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;
     
                        // Create a decrytor to perform the stream transform.
                        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
     
                        // Create the streams used for encryption.
                        using (MemoryStream msEncrypt = new MemoryStream())
                        {
                            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                            {
                                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                                {
     
                                    //Write all data to the stream.
                                    swEncrypt.Write(password_non_crypte);
                                    csEncrypt.FlushFinalBlock(); 
                                }
                                encrypted = msEncrypt.ToArray();
                            }
                        }
                    }
     
     
                    // Return the encrypted bytes from the memory stream.
                    return encrypted;
     
                }
    Cela ne fonctionne toujours pas .
    J'ai exactement le même message d'erreur.

    Si tu as une autre idée, je veux bien...

    Merci à toi.

    Cordialement.

    new_wave

  6. #6
    Expert confirmé

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 761
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    Je viens de tester, et ta fonction initiale fonctionne bien.

    Voici mon programme d'exemple :
    Code C# : 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
     
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace ChiffrementDechiffrement
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte[] key = {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 };
                byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
                string phrase = "Test";
     
                byte[] encrypted = EncryptStringToBytes_Aes(phrase, key, IV);
                Console.WriteLine(DecryptStringFromBytes_Aes(encrypted, key, IV));
                Console.ReadLine();
            }
     
            public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV");
     
                // Declare the string used to hold
                // the decrypted text.
                string password_decrypte = null;
     
                // Create an AesCryptoServiceProvider object
                // with the specified key and IV.
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
     
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
     
                    // Create the streams used for decryption.
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
     
                                // Read the decrypted bytes from the decrypting stream
                                // and place them in a string.
                                password_decrypte = srDecrypt.ReadToEnd();
                            }
                        }
                    }
     
                }
     
                return password_decrypte;
     
            }
     
            public static byte[] EncryptStringToBytes_Aes(string password_non_crypte, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (password_non_crypte == null || password_non_crypte.Length <= 0)
                    throw new ArgumentNullException("password non crypté ");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV");
                byte[] encrypted;
                // Create an AesCryptoServiceProvider object
                // with the specified key and IV.
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
     
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
     
                    // Create the streams used for encryption.
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
     
                                //Write all data to the stream.
                                swEncrypt.Write(password_non_crypte);
     
                            }
                            //csEncrypt.FlushFinalBlock();
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
     
     
                // Return the encrypted bytes from the memory stream.
                return encrypted;
     
            }
        }
    }

    J'ai juste repris ton code, rien fait de plus, hormis supprimer le FlushFinalBlock qui est en fait inutile et même erroné (le StreamWriter lorsqu'il est disposé, ferme le flux dans lequel il écrit, ce qui appel automatiquement FlushFinalBlock pour un CryptoStream. Pire ! En faisant explicitement cet appel avant de sortir du using définissant swEncrypt, les données mises en cache par swEncrypt pouvait ne pas être envoyées vers le flux sous-jacent, résultant en une absence de données).

Discussions similaires

  1. Réponses: 3
    Dernier message: 20/04/2016, 14h12
  2. Réponses: 0
    Dernier message: 26/10/2015, 15h51
  3. Déchiffrement erroné - AES
    Par khaled_prg dans le forum Général Java
    Réponses: 1
    Dernier message: 06/09/2014, 15h46
  4. Déchiffrement AES 128 bits
    Par Happpy dans le forum Algorithmes et structures de données
    Réponses: 2
    Dernier message: 05/03/2014, 00h27
  5. Standard cryptographique AES et licence d'utilisation
    Par KORTA dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 24/09/2003, 13h33

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