Encrypt and decrypt a file using tripleDES
4 answers
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 to share