none
encryption not working:is it antivirus preventing it? RRS feed

  • Question

  • Hi  I tried the code. I can see my source text file selected and read and encryption function called etc..But the output file is zero size...I have McAfee on windows 7. I am wondering if this is reason why encryption is not performed.

    class Program
        {
    
            static void Main(string[] args)
            {
                
                EncryptFile("c:\\mvc\\Aes\\testing.txt", "c:\\mvc\\Aes\\testoutput",
                    "hassankeyissecretveryStrongkeythatisveryDangerous");
                
                
            }
            private static void EncryptFile(string inputFile, string outputFile, string skey)
            {
                try
                {
                    using (RijndaelManaged aes = new RijndaelManaged())
                    {
                        byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
    
                        /* This is for demostrating purposes only. 
                         * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                        byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
    
                        using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                        {
                            using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                            {
                                using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                                {
                                    using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                                    {
                                        int data;
                                        while ((data = fsIn.ReadByte()) != -1)
                                        {
                                            cs.WriteByte((byte)data);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // failed to encrypt file
                }
            }
           
    
        }
    }

     
    Thursday, June 6, 2019 12:49 AM

Answers

  • Okay, there's actually some exception throwing in the code.

    [quote]

    System.Security.Cryptography.CryptographicException: Specified initialization vector (IV) does not match the block size for this algorithm.
       at System.Security.Cryptography.RijndaelManagedTransform..ctor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 blockSize, Int32 feedbackSize, PaddingMode PaddingValue, RijndaelManagedTransformMode transformMode)
       at System.Security.Cryptography.RijndaelManaged.NewEncryptor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 feedbackSize, RijndaelManagedTransformMode encryptMode)
       at System.Security.Cryptography.RijndaelManaged.CreateEncryptor(Byte[] rgbKey, Byte[] rgbIV)
       at EncryptFile.Program.EncryptFile(String inputFile, String outputFile, String skey) in C:\GitHub\Repos\MSDNDemos\EncryptFile\EncryptFile\Program.cs:line 34

    [/quote]

    The IV you provided don't match the required block size and the Key you provided don't match the required KeySize.

    So here's simple dirty fix the make it work:

            private static void EncryptFile(string inputFile, string outputFile, string skey)
            {
                try
                {
                    using (RijndaelManaged aes = new RijndaelManaged())
                    {
                        byte[] key = ASCIIEncoding.UTF8.GetBytes(skey).Take(aes.KeySize / 8).ToArray();
    
                        /* This is for demostrating purposes only. 
                         * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                        byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey).Take(aes.BlockSize / 8).ToArray();
    
                        using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                        {
                            using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                            {
                                using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                                {
                                    using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                                    {
                                        int data;
                                        while ((data = fsIn.ReadByte()) != -1)
                                        {
                                            cs.WriteByte((byte)data);
                                        }
                                    }
                                    cs.FlushFinalBlock();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // failed to encrypt file
                    Console.WriteLine(ex.ToString());
                }
            }
    This is not good because I'm throwing away part of the key user provides. In reality you'll probably want to pre-process it with MD5CryptoServiceProvider.ComputeHash() before throwing away excessive bytes.

    • Marked as answer by kobosh3 Thursday, June 6, 2019 1:52 PM
    Thursday, June 6, 2019 12:58 PM
    Answerer

All replies

  • You have to call cs.FlushFinalBlock() or cs.Close() to write the data. Calling cs.Dispose() (when you exit the using block) will not call it for you.

    If your application is blocked by the antivirus, you should see some exception throwing there.

    Thursday, June 6, 2019 1:12 AM
    Answerer
  • thanks cheong00

    it did not work I tried both close() and flushfinalblock.

     using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                            {
                                CryptoStream cs=null;
                                using (cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                                {
                                    FileStream fsIn = null;
                                    using ( fsIn = new FileStream(inputFile, FileMode.Open))
                                    {
                                        int data;
                                        while ((data = fsIn.ReadByte()) != -1)
                                        {
                                            cs.WriteByte((byte)data);
                                        }
                                    }
                                    //cs.FlushFinalBlock();
                                }
                                cs.FlushFinalBlock();
                           

    Thursday, June 6, 2019 12:35 PM
  • Okay, there's actually some exception throwing in the code.

    [quote]

    System.Security.Cryptography.CryptographicException: Specified initialization vector (IV) does not match the block size for this algorithm.
       at System.Security.Cryptography.RijndaelManagedTransform..ctor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 blockSize, Int32 feedbackSize, PaddingMode PaddingValue, RijndaelManagedTransformMode transformMode)
       at System.Security.Cryptography.RijndaelManaged.NewEncryptor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 feedbackSize, RijndaelManagedTransformMode encryptMode)
       at System.Security.Cryptography.RijndaelManaged.CreateEncryptor(Byte[] rgbKey, Byte[] rgbIV)
       at EncryptFile.Program.EncryptFile(String inputFile, String outputFile, String skey) in C:\GitHub\Repos\MSDNDemos\EncryptFile\EncryptFile\Program.cs:line 34

    [/quote]

    The IV you provided don't match the required block size and the Key you provided don't match the required KeySize.

    So here's simple dirty fix the make it work:

            private static void EncryptFile(string inputFile, string outputFile, string skey)
            {
                try
                {
                    using (RijndaelManaged aes = new RijndaelManaged())
                    {
                        byte[] key = ASCIIEncoding.UTF8.GetBytes(skey).Take(aes.KeySize / 8).ToArray();
    
                        /* This is for demostrating purposes only. 
                         * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                        byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey).Take(aes.BlockSize / 8).ToArray();
    
                        using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                        {
                            using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                            {
                                using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                                {
                                    using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                                    {
                                        int data;
                                        while ((data = fsIn.ReadByte()) != -1)
                                        {
                                            cs.WriteByte((byte)data);
                                        }
                                    }
                                    cs.FlushFinalBlock();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // failed to encrypt file
                    Console.WriteLine(ex.ToString());
                }
            }
    This is not good because I'm throwing away part of the key user provides. In reality you'll probably want to pre-process it with MD5CryptoServiceProvider.ComputeHash() before throwing away excessive bytes.

    • Marked as answer by kobosh3 Thursday, June 6, 2019 1:52 PM
    Thursday, June 6, 2019 12:58 PM
    Answerer