Lecture à partir d'un cryptostream à la fin du flux

Je vais avoir quelques problèmes avec le code ci-dessous. J'ai un fichier dans un emplacement temporaire qui est dans le besoin de chiffrement, cette fonction crypte les données qui sont ensuite stockées à la "pathToSave" emplacement.

Sur l'inspection de l'est ne semble pas être de la manipulation de l'ensemble du fichier correctement - Il y a des morceaux manquants de ma sortie et je soupçonne qu'il a quelque chose à voir avec la boucle while pas en cours d'exécution à travers l'ensemble du flux.

En aparté, si j'essaie de l'appeler CryptStrm.Close() après la boucle while-je recevoir une exception. Cela signifie que si je tente de décrypter le fichier, j'obtiens un fichier en cours d'utilisation d'erreur!

Essayé tous l'habitude et j'ai regardé ici à des problèmes similaires, toute aide serait super.

Grâce

public void EncryptFile(String tempPath, String pathToSave)
    {
        try
        {
            FileStream InputFile = new FileStream(tempPath, FileMode.Open, FileAccess.Read);
            FileStream OutputFile = new FileStream(pathToSave, FileMode.Create, FileAccess.Write);

            RijndaelManaged RijCrypto = new RijndaelManaged();

            //Key
            byte[] Key = new byte[32] { ... };

            //Initialisation Vector
            byte[] IV = new byte[32] { ... };

            RijCrypto.Padding = PaddingMode.None;
            RijCrypto.KeySize = 256;
            RijCrypto.BlockSize = 256;
            RijCrypto.Key = Key;
            RijCrypto.IV = IV;

            ICryptoTransform Encryptor = RijCrypto.CreateEncryptor(Key, IV);

            CryptoStream CryptStrm = new CryptoStream(OutputFile, Encryptor, CryptoStreamMode.Write);

            int data;
            while (-1 != (data = InputFile.ReadByte()))
            {
                CryptStrm.WriteByte((byte)data);
            }
        }
        catch (Exception EncEx)
        {
            throw new Exception("Encoding Error: " + EncEx.Message);
        }
    }

EDIT:

J'ai fait l'hypothèse que mon problème est avec le Cryptage. Mon Décrypter pourrait être le coupable

        public String DecryptFile(String encryptedFilePath)
    {
        FileStream InputFile = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read);

        RijndaelManaged RijCrypto = new RijndaelManaged();

        //Key
        byte[] Key = new byte[32] { ... };

        //Initialisation Vector
        byte[] IV = new byte[32] { ... };

        RijCrypto.Padding = PaddingMode.None;
        RijCrypto.KeySize = 256;
        RijCrypto.BlockSize = 256;
        RijCrypto.Key = Key;
        RijCrypto.IV = IV;

        ICryptoTransform Decryptor = RijCrypto.CreateDecryptor(Key, IV);

        CryptoStream CryptStrm = new CryptoStream(InputFile, Decryptor, CryptoStreamMode.Read);

        String OutputFilePath = Path.GetTempPath() + "myfile.name";
        StreamWriter OutputFile = new StreamWriter(OutputFilePath);

        OutputFile.Write(new StreamReader(CryptStrm).ReadToEnd());

        CryptStrm.Close();
        OutputFile.Close();

        return OutputFilePath;
    }

OriginalL'auteur David | 2012-03-15