i have 2 function with below code. i need c# ver 5.0 that compile with csc.exe compiler .netframwork 3.5 that:
ask from user to encrypt or decrypt file, and path of file to decrypt or encrypte file.
c#:
// RegionHelper.Encryptor
using System;
using System.IO;
using System.Security.Cryptography;
public class Encryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
Below is the complete C# program that allows users to either encrypt or decrypt a file using the provided Encryptor and Decryptor classes. This program is compatible with C# version 5.0 and can be compiled using the csc.exe compiler targeting the .NET Framework 3.5:
csharp
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do you want to encrypt or decrypt a file? (e/d)");
string choice = Console.ReadLine();
Console.Write("Enter the path of the file: ");
string filePath = Console.ReadLine();
try
{
if (choice.Equals("e", StringComparison.OrdinalIgnoreCase))
{
Encryptor encryptor = new Encryptor(filePath);
Console.WriteLine("Enter text to encrypt (type 'exit' to finish):");
string line;
while ((line = Console.ReadLine()) != "exit")
{
encryptor.WriteLine(line);
}
encryptor.Close();
Console.WriteLine("File encrypted successfully!");
}
else if (choice.Equals("d", StringComparison.OrdinalIgnoreCase))
{
Decryptor decryptor = new Decryptor(filePath);
Console.WriteLine("Decrypted content:");
string line;
while ((line = decryptor.ReadLine()) != null)
{
Console.WriteLine(line);
}
decryptor.Close();
}
else
{
Console.WriteLine("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.");
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
// RegionHelper.Encryptor
using System;
using System.IO;
using System.Security.Cryptography;
public class Encryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
private readonly byte[] _cryptKey = new byte[32]
{
1, 139, 186, 155, 65, 100, 58, 33, 81, 231,
17, 149, 80, 130, 47, 202, 28, 161, 83, 155,
221, 28, 73, 125, 247, 213, 70, 114, 161, 26,
75, 252
};
private readonly CryptoStream _cStream;
private readonly FileStream _fStream;
private readonly StreamWriter _sWriter;
public Encryptor(string fileName)
{
_fStream = File.Open(fileName, FileMode.Create);
Rijndael rijndael = Rijndael.Create();
rijndael.Key = _cryptKey;
rijndael.IV = _cryptIv;
_cStream = new CryptoStream(_fStream, rijndael.CreateEncryptor(_cryptKey, _cryptIv), CryptoStreamMode.Write);
_sWriter = new StreamWriter(_cStream);
}
public void WriteLine(string s)
{
try
{
_sWriter.WriteLine(s);
}
catch (Exception)
{
Close();
throw;
}
}
public void Close()
{
_sWriter.Close();
_cStream.Close();
_fStream.Close();
}
}
// RegionHelper.Decryptor
using System;
using System.IO;
using System.Security.Cryptography;
public class Decryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
private readonly byte[] _cryptKey = new byte[32]
{
1, 139, 186, 155, 65, 100, 58, 33, 81, 231,
17, 149, 80, 130, 47, 202, 28, 161, 83, 155,
221, 28, 73, 125, 247, 213, 70, 114, 161, 26,
75, 252
};
private readonly CryptoStream _cStream;
private readonly FileStream _fStream;
private readonly StreamReader _sReader;
public Decryptor(string fileName)
{
_fStream = File.Open(fileName, FileMode.OpenOrCreate);
Rijndael rijndael = Rijndael.Create();
rijndael.Key = _cryptKey;
rijndael.IV = _cryptIv;
_cStream = new CryptoStream(_fStream, rijndael.CreateDecryptor(_cryptKey, _cryptIv), CryptoStreamMode.Read);
_sReader = new StreamReader(_cStream);
}
public string ReadLine()
{
try
{
return _sReader.ReadLine();
}
catch (Exception)
{
Close();
throw;
}
}
public void Close()
{
_sReader.Close();
_cStream.Close();
_fStream.Close();
}
}
How to Compile and Run
Save the above code into a C# file, for example, FileEncryptorDecryptor.cs.
Open the command prompt.
Navigate to the directory where you've saved the file.
When prompted, enter e to encrypt a file or d to decrypt a file.
For encryption, input the text to encrypt and type exit when you're done.
For decryption, the program will display the decrypted text from the file.
Make sure you have read/write permissions for the specified file paths.
using System;
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do you want to encrypt or decrypt a file? (e/d)");
string choice = Console.ReadLine();
code
Console.Write("Enter the path of the file: ");
string filePath = Console.ReadLine();
try
{
if (choice.Equals("e", StringComparison.OrdinalIgnoreCase))
{
Encryptor encryptor = new Encryptor(filePath);
Console.WriteLine("Enter text to encrypt (type 'exit' to finish):");
string line;
while ((line = Console.ReadLine()) != "exit")
{
encryptor.WriteLine(line);
}
encryptor.Close();
Console.WriteLine("File encrypted successfully!");
}
else if (choice.Equals("d", StringComparison.OrdinalIgnoreCase))
{
Decryptor decryptor = new Decryptor(filePath);
Console.WriteLine("Decrypted content:");
string line;
while ((line = decryptor.ReadLine()) != null)
{
Console.WriteLine(line);
}
decryptor.Close();
}
else
{
Console.WriteLine("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.");
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
// RegionHelper.Encryptor
public class Encryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
update code to save enc/dec to file with: filename+_enc orfilename+_dec + extension
To modify the provided C# code so that it saves the encrypted and decrypted files with appropriate filenames (i.e., appending _enc for the encrypted file and _dec for the decrypted file), you'll need to adjust the paths where the files are being created.
Here’s how you can achieve that. I've updated the Encryptor and Decryptor classes' constructors, as well as the file-handling logic in the Main method, to reflect this change.
Updated Code
csharp
using System;
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do you want to encrypt or decrypt a file? (e/d)");
string choice = Console.ReadLine();
Console.Write("Enter the path of the file: ");
string filePath = Console.ReadLine();
try
{
if (choice.Equals("e", StringComparison.OrdinalIgnoreCase))
{
string encryptedFilePath = Path.GetFileNameWithoutExtension(filePath) + "_enc" + Path.GetExtension(filePath);
Encryptor encryptor = new Encryptor(encryptedFilePath);
Console.WriteLine("Enter text to encrypt (type 'exit' to finish):");
string line;
while ((line = Console.ReadLine()) != "exit")
{
encryptor.WriteLine(line);
}
encryptor.Close();
Console.WriteLine($"File encrypted successfully! Saved as: {encryptedFilePath}");
}
else if (choice.Equals("d", StringComparison.OrdinalIgnoreCase))
{
string decryptedFilePath = Path.GetFileNameWithoutExtension(filePath) + "_dec" + Path.GetExtension(filePath);
Decryptor decryptor = new Decryptor(filePath);
using (StreamWriter writer = new StreamWriter(decryptedFilePath))
{
string line;
while ((line = decryptor.ReadLine()) != null)
{
writer.WriteLine(line);
}
}
decryptor.Close();
Console.WriteLine($"File decrypted successfully! Saved as: {decryptedFilePath}");
}
else
{
Console.WriteLine("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.");
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
// RegionHelper.Encryptor
public class Encryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
private readonly byte[] _cryptKey = new byte[32]
{
1, 139, 186, 155, 65, 100, 58, 33, 81, 231,
17, 149, 80, 130, 47, 202, 28, 161, 83, 155,
221, 28, 73, 125, 247, 213, 70, 114, 161, 26,
75, 252
};
private readonly CryptoStream _cStream;
private readonly FileStream _fStream;
private readonly StreamWriter _sWriter;
public Encryptor(string fileName)
{
_fStream = File.Open(fileName, FileMode.Create);
Rijndael rijndael = Rijndael.Create();
rijndael.Key = _cryptKey;
rijndael.IV = _cryptIv;
_cStream = new CryptoStream(_fStream, rijndael.CreateEncryptor(_cryptKey, _cryptIv), CryptoStreamMode.Write);
_sWriter = new StreamWriter(_cStream);
}
public void WriteLine(string s)
{
try
{
_sWriter.WriteLine(s);
}
catch (Exception)
{
Close();
throw;
}
}
public void Close()
{
_sWriter.Close();
_cStream.Close();
_fStream.Close();
}
}
// RegionHelper.Decryptor
public class Decryptor
{
private readonly byte[] _cryptIv = new byte[16]
{
133, 85, 59, 2, 52, 62, 190, 38, 169, 72,
48, 15, 89, 55, 6, 4
};
private readonly byte[] _cryptKey = new byte[32]
{
1, 139, 186, 155, 65, 100, 58, 33, 81, 231,
17, 149, 80, 130, 47, 202, 28, 161, 83, 155,
221, 28, 73, 125, 247, 213, 70, 114, 161, 26,
75, 252
};
private readonly CryptoStream _cStream;
private readonly FileStream _fStream;
private readonly StreamReader _sReader;
public Decryptor(string fileName)
{
_fStream = File.Open(fileName, FileMode.Open);
Rijndael rijndael = Rijndael.Create();
rijndael.Key = _cryptKey;
rijndael.IV = _cryptIv;
_cStream = new CryptoStream(_fStream, rijndael.CreateDecryptor(_cryptKey, _cryptIv), CryptoStreamMode.Read);
_sReader = new StreamReader(_cStream);
}
public string ReadLine()
{
try
{
return _sReader.ReadLine();
}
catch (Exception)
{
Close();
throw;
}
}
public void Close()
{
_sReader.Close();
_cStream.Close();
_fStream.Close();
}
}
Changes Made:
File Naming:
For encryption, the encrypted file is saved with the suffix _enc followed by the original file's extension.
For decryption, the decrypted contents are saved with the suffix _dec followed by the original file's extension.
Error Handling and Output:
The program prints the name of the saved files upon successful encryption or decryption.
How to Compile and Run
You can use the same steps mentioned earlier for compiling and running this code. Just replace the previous code with this updated version. The flow through the console remains the same, allowing the user to encrypt or decrypt a file and specifying the appropriate input/output file paths automatically.