Encrypt and decrypt a file using tripleDES

Can someone give me some code or valuable sugestion to encrypt and decrypt a large file in C #? I am trying to do this with TripleDES.it should be safe, fast and reliable here encryption and decryption should be done based on my key.

+2


source to share


4 answers


I found a good article.



TripleDES

+1


source


Here is the link Encrypt / decrypt text files with Rijndael or TripleDES



+1


source


0


source


Below is the code snippet in case anyone is looking for it.

    public static byte[] Encrypt(byte[] plain, String key)
    {
        byte[] keyArray = SoapHexBinary.Parse(key).Value;

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.None;
        tdes.IV = new byte[8];

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(plain, 0, plain.Length);
        tdes.Clear();

        return resultArray;
    }


    public static byte[] Decrypt(byte[] cipher, String key)
    {
        byte[] keyArray = SoapHexBinary.Parse(key).Value;

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.None;
        tdes.IV = new byte[8];

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(cipher, 0, cipher.Length);
        tdes.Clear();

        return resultArray;
    }

      

Keep in mind that I was passing the key as in HexBinary format. The above code uses CBC encryption mode with the start vector {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} and the padding is None, since the data I passed was always a multiple of 64 bits. Therefore, if your data size is not fixed, you may need to install an add-on.

0


source







All Articles