mercoledì 13 novembre 2013

Crypt And Decrypt rar files with c#

Ciao a tutti,

un link interessante per cryptare e decriptare file rar in c#!!!

http://www.fluxbytes.com/csharp/encrypt-and-decrypt-files-in-c/



Riporto il codice qualora la pagina linkata dovesse essere dismessa.



CRYPT
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
    }
}





Decrypt

private static void DecryptFile(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(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
    }
}
Usage
                           File Input                     File Output                          Key
EncryptFile("C:\\myfile.rar", "c:\\myfileEncrypted.rar", "1234512345678976");
DecryptFile("C:\\myfileEncrypted.rar", "c:\\myfileDecrypted.rar", "1234512345678976");


Ciao e alla prossima!!!