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

C# Discussion :

Erreur cryptage AES(Rijndael)


Sujet :

C#

  1. #1
    Membre régulier Avatar de Klivor
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 143
    Points : 100
    Points
    100
    Par défaut Erreur cryptage AES(Rijndael)
    Bonjour cher amis,

    j'ai un souci pour le dechiffrement dans l'algorithme AES.

    Voici le code de la procédure
    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
     public static string DecryptString(string cipherText, string strKey)
            {
    
                // Place le texte à déchiffrer dans un tableau d'octets
                byte[] cipheredData = Convert.FromBase64String(cipherText);
    
                // Place la clé de déchiffrement dans un tableau d'octets
                byte[] key = Encoding.UTF8.GetBytes(strKey);
    
              
    
                RijndaelManaged rijndael = new RijndaelManaged();
                rijndael.Mode = CipherMode.CBC;
    
    
                // Ecris les données déchiffrées dans le MemoryStream
                ICryptoTransform decryptor = rijndael.CreateDecryptor();
                MemoryStream ms = new MemoryStream(cipheredData);
                CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
    
                // Place les données déchiffrées dans un tableau d'octet
                byte[] plainTextData = new byte[cipheredData.Length];
    Sa plante sur cette ligne
                int decryptedByteCount = cs.Read(plainTextData, 0, plainTextData.Length);
    
                ms.Close();
                cs.Close();
    
                return Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount);
    
            }
    l'erreur est :
    Padding is invalid and cannot be removed.
    je cherche sur le net depuis quelques heures déjà sans résultat.

    quelqu'un à t-il une idée ?

    Merci pour tous

  2. #2
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    Bonjour,

    l'algo de Rijndael n'est pas très compliqué à utiliser, voici un exemple :

    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
    static void Main(string[] args)
    {
        string secret = "mon code secret";
        string crypte = Crypter("ma phrase à encoder", secret);
        Console.WriteLine(Decrypter(crypte, secret));
    }
     
    public static string Decrypter(string encryptedString, string keyPhrase)
    {
        byte[] Key = new byte[24];
        byte[] IV = new byte[16];
        GenerateKey(keyPhrase, ref Key, ref IV);
     
        byte[] encrypted = Convert.FromBase64String(encryptedString);
        UnicodeEncoding textConverter = new UnicodeEncoding(); // ou ASCIIEncoding si besoin
        RijndaelManaged myRijndael = new RijndaelManaged { Key = Key, IV = IV };
     
        ICryptoTransform decryptor = myRijndael.CreateDecryptor(Key, IV);
        MemoryStream msDecrypt = new MemoryStream(encrypted);
        CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
        byte[] fromEncrypt = new byte[encrypted.Length];
     
        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
     
        return textConverter.GetString(fromEncrypt);
    }
     
    public static string Crypter(string original, string keyPhrase)
    {
        byte[] Key = new byte[24];
        byte[] IV = new byte[16];
     
        GenerateKey(keyPhrase, ref Key, ref IV);
     
        UnicodeEncoding textConverter = new UnicodeEncoding(); // ou ASCIIEncoding si besoin
        RijndaelManaged myRijndael = new RijndaelManaged { Key = Key, IV = IV };
     
        ICryptoTransform encryptor = myRijndael.CreateEncryptor(Key, IV);
        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
     
        byte[] toEncrypt = textConverter.GetBytes(original);
     
        csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
        csEncrypt.FlushFinalBlock();
     
        byte[] encrypted = msEncrypt.ToArray();
        return Convert.ToBase64String(encrypted);
    }
     
    public static void GenerateKey(string SecretPhrase, ref byte[] Key, ref byte[] IV)
    {
        byte[] bytePhrase = Encoding.ASCII.GetBytes(SecretPhrase);
        SHA384Managed sha384 = new SHA384Managed();
        sha384.ComputeHash(bytePhrase);
        byte[] result = sha384.Hash;
        for (int loop = 0; loop < 24; loop++)
            Key[loop] = result[loop];
        for (int loop = 24; loop < 40; loop++)
            IV[loop - 24] = result[loop];
    }
    par contre, ce n'est pas de l'AES

  3. #3
    Membre régulier Avatar de Klivor
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 143
    Points : 100
    Points
    100
    Par défaut
    Merci de t'as réponse, je vais faire le test de suite.
    Mais par contre AES est bien du Rinjdael

    regarde ce lien : http://fr.wikipedia.org/wiki/Advance...ption_Standard

  4. #4
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    ah, je viens d'apprendre un truc, merci

  5. #5
    Membre régulier Avatar de Klivor
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 143
    Points : 100
    Points
    100
    Par défaut
    Citation Envoyé par nico-pyright(c) Voir le message
    ah, je viens d'apprendre un truc, merci
    Derien, pour revenir au code que tu m'as donner regarde ce qu'il fait :



    c'est presque sa, je ne comprend pas pourquoi il rajoute les symbols :/

  6. #6
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    peut-être que cela vient de ton code secret.
    Essaie de changer ca dans le GenerateKey

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    byte[] bytePhrase = Encoding.Unicode.GetBytes(SecretPhrase);
    il y a des caractères bizarres dans ta clé ?

  7. #7
    Membre régulier
    Homme Profil pro
    Developpeur
    Inscrit en
    Décembre 2011
    Messages
    111
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Developpeur
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2011
    Messages : 111
    Points : 79
    Points
    79
    Par défaut crypto
    Excuse_moi Mais la cryptographie Symetrique,n'est-elle pas basée sur le Z=26;
    cad elle est basée sur le lettre alphabetique?
    Si oui pourquoi n'est créer tes propres modules?

  8. #8
    Membre régulier Avatar de Klivor
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 143
    Points : 100
    Points
    100
    Par défaut
    Citation Envoyé par nico-pyright(c) Voir le message
    peut-être que cela vient de ton code secret.
    Essaie de changer ca dans le GenerateKey

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    byte[] bytePhrase = Encoding.Unicode.GetBytes(SecretPhrase);
    il y a des caractères bizarres dans ta clé ?
    Non, sa ne change rien et mon code secret est une chaîne simple.
    Donc je suis obligé de faire se traitement pour enlever ces caractères.

    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
     ArrayList chainedecrypte = new ArrayList();
               foreach (char unchar in textDechiffre.Text)
               {
                   if (unchar.ToString() != "\0")
                   {
                       chainedecrypte.Add(unchar);
                   }
               }
               textDechiffre.Text = "";
     
               foreach (char unchar in chainedecrypte)
               {
                   textDechiffre.Text = textDechiffre.Text + unchar.ToString();
               }
               chainedecrypte.Clear();

  9. #9
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Points : 16 075
    Points
    16 075
    Par défaut
    après avoir cherché vite fait, j'ai l'impression que c'est normal car c'est un algo par bloc.

    Tu peux retirer les \0 à la fin , par ex :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string res = Decrypter(res, "test").Replace("\0", string.Empty);

  10. #10
    Membre régulier Avatar de Klivor
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 143
    Points : 100
    Points
    100
    Par défaut
    Ok,Ok
    Merci à vous pour votre aide précieuse.
    à bientôt et bonne prog.

    Klivor

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Cryptage AES clé 256bits
    Par marion782 dans le forum Sécurité
    Réponses: 15
    Dernier message: 16/07/2013, 16h52
  2. Cryptage AES (alias Rijndael)
    Par teol91 dans le forum Langage
    Réponses: 6
    Dernier message: 14/10/2011, 09h53
  3. Erreur OpenSSL Decryption Rijndael 256
    Par messier79 dans le forum Langage
    Réponses: 2
    Dernier message: 22/11/2010, 02h21
  4. code cryptage AES
    Par youp_db dans le forum Windows
    Réponses: 2
    Dernier message: 04/03/2008, 09h10
  5. Bibliothèque zip avec cryptage AES
    Par guilig dans le forum Bibliothèques
    Réponses: 5
    Dernier message: 01/02/2007, 18h30

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