Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
sasikala sRank #1,091 · 1.7K reputationPosted Jan 3, 2025, 12:29 AM EST
file encryption in C# .NET, there are several options available, each with its own strengths. Here are some of the most commonly used methods:
1. AES (Advanced Encryption Standard)
Example Code:
using System.Security.Cryptography; using System.IO; \/\/ Encrypt public static void EncryptFile(string inputFile, string outputFile, byte[] key, byte[] iv) { using (var aes = Aes.Create()) { aes.Key = key; aes.IV = iv; using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV)) using (var fileStream = new FileStream(outputFile, FileMode.Create)) using (var cryptoStream = new CryptoStream(fileStream, encryptor, CryptoStreamMode.Write)) { using (var inputStream = new FileStream(inputFile, FileMode.Open)) { inputStream.CopyTo(cryptoStream); } } } }2. RSA (Rivest-Shamir-Adleman)
Use Case: Typically used to encrypt the AES key, which can then be used to encrypt the actual file.
3. Triple DES (3DES)
4. File System Encryption
Conclusion
For most applications, AES is generally the best choice due to its balance of security and performance. If you need to securely exchange keys, consider combining RSA with AES.
Jaish MathewsRank #225 · 11K reputationPosted Jan 3, 2025, 2:30 AM EST
using System; using System.IO; using System.Security.Cryptography; public class FileEncryption { public static void EncryptFile(string inputFile, string outputFile, byte[] key, byte[] iv) { using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; using (FileStream fileStream = new FileStream(outputFile, FileMode.Create)) using (CryptoStream cryptoStream = new CryptoStream(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open)) { inputFileStream.CopyTo(cryptoStream); } } } public static void DecryptFile(string inputFile, string outputFile, byte[] key, byte[] iv) { using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; using (FileStream fileStream = new FileStream(outputFile, FileMode.Create)) using (CryptoStream cryptoStream = new CryptoStream(fileStream, aes.CreateDecryptor(), CryptoStreamMode.Write)) using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open)) { cryptoStream.CopyTo(fileStream); } } } }using System; using System.IO; using System.Security.Cryptography; public class FileEncryptionRSA { public static void EncryptFile(string inputFile, string outputFile, RSAParameters publicKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(publicKey); byte[] fileBytes = File.ReadAllBytes(inputFile); byte[] encryptedData = rsa.Encrypt(fileBytes, false); File.WriteAllBytes(outputFile, encryptedData); } } public static void DecryptFile(string inputFile, string outputFile, RSAParameters privateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(privateKey); byte[] encryptedData = File.ReadAllBytes(inputFile); byte[] decryptedData = rsa.Decrypt(encryptedData, false); File.WriteAllBytes(outputFile, decryptedData); } } }