Secure File Sharing System C#
👤 Sharing: AI
```csharp
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.IO.Compression;
namespace SecureFileSharing
{
public class SecureFileSharingSystem
{
// Configuration (replace with your desired values)
private const string EncryptionKey = "YourSecretEncryptionKey1234567890"; // Keep this VERY secure and DO NOT hardcode in real application. Store in secure configuration.
private const string Salt = "YourSecureSaltValue"; // Use a unique, randomly generated salt per file for better security in real application.
private const int KeySize = 256; // AES-256 is generally recommended
private const int BlockSize = 128; // AES block size
// Encryption Method: AES
public static void EncryptFile(string inputFile, string outputFile)
{
try
{
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] keyBytes = new Rfc2898DeriveBytes(EncryptionKey, saltBytes, 1000).GetBytes(KeySize / 8); // Key derivation function
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = KeySize;
aesAlg.BlockSize = BlockSize;
aesAlg.Key = keyBytes;
// Create Initialization Vector (IV). Use a unique IV per file for better security
aesAlg.GenerateIV();
byte[] iv = aesAlg.IV;
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
// Write the IV to the beginning of the output file (important for decryption)
fsOut.Write(iv, 0, iv.Length);
using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV))
{
using (CryptoStream csEncrypt = new CryptoStream(fsOut, encryptor, CryptoStreamMode.Write))
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
byte[] buffer = new byte[4096]; // Buffer size can be adjusted
int bytesRead;
while ((bytesRead = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
csEncrypt.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
Console.WriteLine($"File encrypted successfully: {outputFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Encryption error: {ex.Message}");
}
}
public static void DecryptFile(string inputFile, string outputFile)
{
try
{
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] keyBytes = new Rfc2898DeriveBytes(EncryptionKey, saltBytes, 1000).GetBytes(KeySize / 8);
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = KeySize;
aesAlg.BlockSize = BlockSize;
aesAlg.Key = keyBytes;
// Read the IV from the beginning of the input file
byte[] iv = new byte[aesAlg.BlockSize / 8];
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
fsIn.Read(iv, 0, iv.Length);
aesAlg.IV = iv;
using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
{
using (CryptoStream csDecrypt = new CryptoStream(fsIn, decryptor, CryptoStreamMode.Read))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
{
fsOut.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
Console.WriteLine($"File decrypted successfully: {outputFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Decryption error: {ex.Message}");
}
}
// Optional: Compress File (GZip)
public static void CompressFile(string inputFile, string outputFile)
{
try
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
using (GZipStream gzipStream = new GZipStream(fsOut, CompressionMode.Compress))
{
fsIn.CopyTo(gzipStream);
}
}
}
Console.WriteLine($"File compressed successfully: {outputFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Compression error: {ex.Message}");
}
}
public static void DecompressFile(string inputFile, string outputFile)
{
try
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
using (GZipStream gzipStream = new GZipStream(fsIn, CompressionMode.Decompress))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
gzipStream.CopyTo(fsOut);
}
}
}
Console.WriteLine($"File decompressed successfully: {outputFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Decompression error: {ex.Message}");
}
}
public static void Main(string[] args)
{
// Example Usage
string originalFile = "test.txt";
string encryptedFile = "test.enc";
string decryptedFile = "test_decrypted.txt";
string compressedFile = "test.gz";
string decompressedFile = "test_decompressed.txt";
// Create a test file
File.WriteAllText(originalFile, "This is a test file for secure file sharing.");
Console.WriteLine("Starting Secure File Sharing Example:");
// Compression (Optional)
CompressFile(originalFile, compressedFile);
DecompressFile(compressedFile, decompressedFile);
// Encryption
EncryptFile(originalFile, encryptedFile);
// Decryption
DecryptFile(encryptedFile, decryptedFile);
Console.WriteLine("File Sharing Example Complete. Check the generated files.");
//Cleanup (optional)
//File.Delete(encryptedFile);
//File.Delete(decryptedFile);
//File.Delete(compressedFile);
//File.Delete(decompressedFile);
Console.ReadKey();
}
}
}
```
👁️ Viewed: 9
Comments