private static byte[] CryptoTransform( string fileName, CryptoTransformOperationsEnum operation, SymmetricAlgorithm algo) { byte[] orignalFile = File.ReadAllBytes(fileName); byte[] processedFile; ICryptoTransform cryptoTransform; switch (operation) { case CryptoTransformOperationsEnum.Encrypt: cryptoTransform = algo.CreateEncryptor(); break; case CryptoTransformOperationsEnum.Decrypt: cryptoTransform = algo.CreateDecryptor(); break; default: throw new Exception(); } using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)) { cryptoStream.Write(orignalFile, 0, orignalFile.Length); } algo.Dispose(); processedFile = memoryStream.ToArray(); } return(processedFile); }
private static void StartCryptoProcessing( string fileName, CryptoTransformOperationsEnum operation, SymetricAlgorithmsEnum algorithm) { SymmetricAlgorithm algo; switch (algorithm) { case SymetricAlgorithmsEnum.AES: algo = new AesManaged(); break; case SymetricAlgorithmsEnum.DES: algo = new DESCryptoServiceProvider(); break; case SymetricAlgorithmsEnum.RC2: algo = new RC2CryptoServiceProvider(); break; default: throw new Exception(); } byte[] key = GetCredencial("Key", algo.BlockSize); byte[] iv = GetCredencial("IV", algo.BlockSize); algo.IV = iv; algo.Key = key; byte[] transformedFile = CryptoTransform(fileName, operation, algo); string newFileName = ""; if (operation == CryptoTransformOperationsEnum.Decrypt) { newFileName = Path.Combine( Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)); newFileName = GetNextFileName(newFileName); } else if (operation == CryptoTransformOperationsEnum.Encrypt) { newFileName = fileName + ".enc"; } File.WriteAllBytes(newFileName, transformedFile); }