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 :

Problème de cryptage/décryptage DES


Sujet :

C#

  1. #1
    Membre du Club
    Homme Profil pro
    Responsable de rayon
    Inscrit en
    Juin 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de rayon
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 86
    Points : 60
    Points
    60
    Par défaut Problème de cryptage/décryptage DES
    Bonjour
    Je cherche à crypter et décrypter un flux en binaire. Le cryptage se passe visiblement sans problème mais lorsque je cherche à décrypter j'ai une erreur "Données incorrectes". 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
    public static Stream EncryptStream(Stream streamIn)
            {
                MemoryStream ms = new MemoryStream();
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                CryptoStream cryptostream = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); 
                byte[] bytearrayinput = new byte[streamIn.Length - 1];
                streamIn.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                return ms;
            }
            public static Stream DecryptStream(Stream streamIn)
            {
                MemoryStream ms = new MemoryStream();
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desdecrypt = DES.CreateDecryptor();
                CryptoStream cryptostreamDecr = new CryptoStream(streamIn, desdecrypt, CryptoStreamMode.Read);
                StreamWriter fsDecrypted = new StreamWriter(ms);
                fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                fsDecrypted.Flush();
                fsDecrypted.Close();
                return ms;
            }
    Si quelqu'un a une idée car là je sèche :p
    Merci

  2. #2
    Expert confirmé Avatar de DonQuiche
    Inscrit en
    Septembre 2010
    Messages
    2 741
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 2 741
    Points : 5 485
    Points
    5 485
    Par défaut
    Bonjour, la position courante de streamIn est-elle bien au début du flux ?

  3. #3
    Membre du Club
    Homme Profil pro
    Responsable de rayon
    Inscrit en
    Juin 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de rayon
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 86
    Points : 60
    Points
    60
    Par défaut
    Oui streamIn est bien en position 0 au début de la fonction.

  4. #4
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 700
    Points : 488
    Points
    488
    Par défaut
    j'ai eu ce genre de probleme avec un autre algoritme. la taille de mon vecteur n'etait pas supporté par celui ci. Regarde donc dans les specs, la taille de la clé, et du vecteur supporté, on ne sait jamais

  5. #5
    Membre du Club
    Homme Profil pro
    Responsable de rayon
    Inscrit en
    Juin 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de rayon
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 86
    Points : 60
    Points
    60
    Par défaut
    Bon alors j'ai effectivement détecté quelques erreurs : la clé et le vecteur d'initialisation était en 16 bytes alors qu'il en fallait 8 mais le pire : j'ai oublié de les intégrer dans ma fonction mdr
    Alors je les ai ajouté mais... même erreur

    Voici le code actuellement avec la fonction de création de la clé et du vecteur :
    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
            public static Stream EncryptStream(Stream streamIn)
            {
                MemoryStream ms = new MemoryStream();
                streamIn.Position = 0;
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desencrypt = DES.CreateEncryptor(key, iv);
                CryptoStream cryptostream = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); 
                byte[] bytearrayinput = new byte[streamIn.Length - 1];
                streamIn.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                return ms;
            }
            public static Stream DecryptStream(Stream streamIn)
            {
                MemoryStream ms = new MemoryStream();
                streamIn.Position = 0;
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desdecrypt = DES.CreateDecryptor(key, iv);
                CryptoStream cryptostreamDecr = new CryptoStream(streamIn, desdecrypt, CryptoStreamMode.Read);
                StreamWriter fsDecrypted = new StreamWriter(ms);
                fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                fsDecrypted.Flush();
                fsDecrypted.Close();
                return ms;
            }
            private static List<byte[]> GenerateAlgotihmInputs()
            {
                byte[] Key;
                byte[] iv;
                List<byte[]> result = new List<byte[]>();
                Rfc2898DeriveBytes rfcDb = new Rfc2898DeriveBytes(key, System.Text.Encoding.UTF8.GetBytes(key));
                Key = rfcDb.GetBytes(8);
                iv = rfcDb.GetBytes(8);
                result.Add(Key);
                result.Add(iv);
                return result;
            }
    Le codage se fait à priori bien (en tous les cas pas d'erreur)
    Et l'erreur se produit sur le décodage à la ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());

  6. #6
    Membre du Club
    Homme Profil pro
    Responsable de rayon
    Inscrit en
    Juin 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de rayon
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 86
    Points : 60
    Points
    60
    Par défaut
    J'ai résolu le problème ! Pour ceux que ça intéresse voici la correction :
    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 Stream EncryptStream(Stream streamIn)
            {
                streamIn.Position = 0;
                MemoryStream ms = new MemoryStream();
                BinaryReader br = new BinaryReader(streamIn);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desencrypt = DES.CreateEncryptor(key, iv);
                CryptoStream cryptostream = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write);
                byte[] b = br.ReadBytes(1000);
                while (b.Length > 0)
                {
                    cryptostream.Write(b, 0, b.Length);
                    b = br.ReadBytes(1000);
                }
                cryptostream.FlushFinalBlock();
                return ms;
            }
            public static Stream DecryptStream(Stream streamIn)
            {
                streamIn.Position = 0;
                MemoryStream ms = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(ms);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                List<byte[]> secure = GenerateAlgotihmInputs();
                byte[] key = secure[0];
                byte[] iv = secure[1];
                ICryptoTransform desdecrypt = DES.CreateDecryptor(key, iv);
                CryptoStream cryptostreamDecr = new CryptoStream(streamIn, desdecrypt, CryptoStreamMode.Read);
                int bytes = 1000;
                byte[] b = new byte[bytes];
                do
                {
                    bytes = cryptostreamDecr.Read(b, 0, b.Length);
                    bw.Write(b, 0, bytes);
     
                } while (bytes > 0);
                bw.Flush();
                return ms;
            }
    Merci pour votre aide !!!

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 28/02/2011, 20h58
  2. Problème cryptage décryptage BlowFish
    Par xokami35x dans le forum Sécurité
    Réponses: 3
    Dernier message: 31/03/2010, 13h15
  3. Problème de cryptage/décryptage RSA en Java
    Par Reeter dans le forum Sécurité
    Réponses: 2
    Dernier message: 29/03/2009, 22h41
  4. RSA Cryptage/Décryptage des String
    Par khaledUSTHB dans le forum VB.NET
    Réponses: 3
    Dernier message: 18/09/2008, 10h49
  5. [VB.NET]Problème lors du décryptage DES
    Par Nixar dans le forum Windows Forms
    Réponses: 2
    Dernier message: 21/03/2007, 09h27

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