无法解密c#中的文件,生成的文件已损坏

时间:2015-05-14 18:29:32

标签: c# encryption

我在visual studio中创建了一些代码,使用ftp下载方法下载文件然后我尝试创建代码来解密它,当我意识到加密的文件(我有密钥和IV)所以我试图制作代码来解密文件,但结果已损坏)

private static void DecryptFile(string path, string newpath, string skey)
    {
        try
        {
            using (RijndaelManaged aes = new RijndaelManaged() { 
                KeySize = 128,
                Mode = CipherMode.CBC

            })
            {
                byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);


                byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);

                using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                {
                    using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                    {
                        using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
                        {
                            using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                            {
                                int data;
                                while ((data = cs.ReadByte()) != -1)
                                {
                                    fsOut.WriteByte((byte)data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // failed to decrypt file
        }
    }

0 个答案:

没有答案