Aes Problem

Résolu
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012 - 4 mai 2012 à 18:31
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012 - 11 mai 2012 à 00:26
Bonjour a tous, voila j'ai un soucis sur le décryptage d un fichier, jutilise donc la classe RijndaelManaged pour executer mon decryptage avec ce code:

public bool DecryptFile(string inputFile, string outputFile, string sKEY, string sIV)
        {
            CryptoStream cryptoStream = null;
            try
            {
                FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
                FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write);

                string hexKey = "0x" + sKEY;
                string hexIV = "0x" + sIV;
                byte[] KEY = ParseHex(sKEY);
                byte[] KEYIV = ParseHex(sIV);

                RijndaelManaged aes new RijndaelManaged() { IV KEYIV, Key = KEY,Mode = CipherMode.CFB,Padding = PaddingMode.None};

                ICryptoTransform encryptor = aes.CreateDecryptor();
                cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write);

                byte[] buffer = new byte[fsInput.Length];
                long bytesProcessed = 0;
                long fileLength = fsInput.Length;
                int bytesInCurrentBlock;

                do
                {
                    bytesInCurrentBlock = fsInput.Read(buffer, 0, buffer.Length);
                    cryptoStream.Write(buffer, 0, bytesInCurrentBlock);
                    bytesProcessed = bytesProcessed + bytesInCurrentBlock;
                }
                while (bytesProcessed < fileLength);

                cryptoStream.Clear();
                cryptoStream.Dispose();
                cryptoStream.Close();
            }
            catch (Exception ex)
            {
                string message3 = Common.Translator.GetString("ERRORUNCRYPT");
                AddToConsole(message3 + ex.Message, LEVEL.ERROR);
                return false;
            }
            return true;
        }

public byte[] ParseHex(string hex)
        {
            int offset = hex.StartsWith("0x") ? 2 : 0;
            if ((hex.Length % 2) != 0)
            {
                //AddToConsole("Invalid length: " + hex.Length, LEVEL.ERROR);
            }
            byte[] ret = new byte[(hex.Length - offset) / 2];

            for (int i = 0; i < ret.Length; i++)
            {
                ret[i] = (byte)((ParseNybble(hex[offset]) << 4)
                                 | ParseNybble(hex[offset + 1]));
                offset += 2;
            }
            return ret;
        }

        static int ParseNybble(char c)
        {
            if (c >= '0' && c <= '9')
            {
                return c - '0';
            }
            if (c >= 'A' && c <= 'F')
            {
                return c - 'A' + 10;
            }
            if (c >= 'a' && c <= 'f')
            {
                return c - 'a' + 10;
            }
            throw new ArgumentException("Invalide hex: " + c);
        }


Le problème est que je pers 2 bytes sur mon décryptage
avec cette exception : Longueur des données à déchiffrer non valide.
la version c++ de mon app ou en ligne de commande sous linux avec openssl tout ce déroule bien, le problème viens en c#

bon


pas bon


Quelqu'un aurais une idée ?

7 réponses

sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012
11 mai 2012 à 00:26
problème réglé, j'ai re-écrit la méthode comme ceci:

public bool DecryptFile(string inputFile, string outputFile, string key, string iv)
        {
            FileStream fsInput = new FileStream(inputFile, FileMode.Open);
            FileStream fsOutput = new FileStream(outputFile, FileMode.Create);
            try
            {
                RijndaelManaged aes = new RijndaelManaged
                {
                    IV = Crypto.ParseHexString(iv),
                    Key = Crypto.ParseHexString(key),
                    Mode = CipherMode.CFB,
                    Padding = PaddingMode.None,
                };

                ICryptoTransform encryptor = aes.CreateDecryptor();

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    int cut;
                    using (CryptoStream output = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    {
                        cut = (int)(aes.BlockSize - fsInput.Length % aes.BlockSize);
                        byte[] buffer = new byte[fsInput.Length + cut];

                        fsInput.Read(buffer, 0, buffer.Length);
                        output.Write(buffer, 0, buffer.Length);
                    }

                    byte[] data = memoryStream.ToArray();
                    fsOutput.Write(data, 0, data.Length - cut);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
                return false;
            }
            fsOutput.Flush();
            fsOutput.Close();
            return true;
        }
3
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012
7 mai 2012 à 03:07
personnes d’intéressé par mon problème ?
0
cs_jopop Messages postés 1540 Date d'inscription lundi 26 mai 2003 Statut Membre Dernière intervention 1 août 2013 12
9 mai 2012 à 14:57
Salut,

le souci pourrait provenir du byte 0x00, premier byte à sauter dans ton appli, mais également le byte réservé pour signifier une fin de chaîne de caractères. ça fait pas bien avancer le schmilblick, mais ça peut se creuser.

PS : je connais pas cette librairie
0
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012
9 mai 2012 à 17:04
Ce qui est bizarre, c'est qu'un fois mon fichier décompressé avec zlib il reste identique malgré l'exception et les 2 bytes manquant
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_jopop Messages postés 1540 Date d'inscription lundi 26 mai 2003 Statut Membre Dernière intervention 1 août 2013 12
9 mai 2012 à 17:12
Perso j'ai eu un souci un peu similaire en pondant encodeur RSA en Java.
Sous Windows il persistait 2 bytes en début de fichier qui pourrissait tous mes calculs.
C'est pour ça que j'ai pensé à un problème de "byte mal-aimé".

Sinon là je vois pas bien ... t'as déjà tenté le mode debug pas-à-pas pour voir quand est-ce que tu perdais ces deux maudits bytes ?
0
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012
9 mai 2012 à 17:35
Le problème est qu'en debug pas-a-pas j ai mon visual studio qui plante -_-
0
sheppertizer Messages postés 22 Date d'inscription jeudi 9 octobre 2008 Statut Membre Dernière intervention 23 octobre 2012
9 mai 2012 à 17:56
Petit édit, après avoir fixé le problème de crash de visual studio ^^

je perd mes bytes dans ma boucle read/write de mes 2 fichiers
byte[] buffer = new byte[fsInput.Length];
long bytesProcessed = 0;
long fileLength = fsInput.Length;
int bytesInCurrentBlock;

do
{
     bytesInCurrentBlock = fsInput.Read(buffer, 0, buffer.Length);
     cryptoStream.Write(buffer, 0, bytesInCurrentBlock);
     bytesProcessed = bytesProcessed + bytesInCurrentBlock;
}
while (bytesProcessed < fileLength);


en changeant par

int data;

   while ((data = fsInput.ReadByte()) != -1)
      cryptoStream.WriteByte((byte)data);


j'en perd bien +
0
Rejoignez-nous