tbf/Encrypt/Program.cs
Michal Buzik 0daefb93bb Add CJMS11 enhancements, logging, and resolution support
- Extended CJMS11 camera handling with new functionalities, including image grabbing, resolution configuration, and improved ROI support.
- Added new `ToString` method implementations for enhanced debugging and logging in key classes like `JmsMessage`, `JmsPacket`, and `RoiCfg`.
- Introduced additional commands (`start_stream_images`, `stop_stream_images`) in `CommandM` and its enum.
- Implemented new resolution handling in `RoiCfg` with support for configurable image frames.
- Refactored resolution-related UI elements in `RoiCfgCtrl` to add a dropdown for selecting resolution.
- Made minor UX improvements by replacing inconsistent string formats with verbatim strings in console messages.
- Updated project dependencies with new resolution-related utilities (`Frame` and `ResolutionFrames`).
- Resolved camera-specific symbolic issues by transitioning to `CJMS11.Camera` over prior naming inconsistencies.
2025-07-18 08:50:22 +02:00

94 lines
3.3 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Encrypt
{
/// <summary>
/// See also:
/// https://www.fluxbytes.com/csharp/encrypt-and-decrypt-files-in-c/
/// </summary>
class Program
{
static byte[] key = new byte[] { 83, 254, 105, 64, 184, 201, 195, 127, 52, 99, 77, 45, 252, 132, 163, 156 };
static byte[] IV = new byte[] { 189, 23, 137, 56, 204, 241, 242, 118, 28, 89, 9, 198, 224, 111, 186, 125 };
static void Main(string[] args)
{
if ((args.Length < 1) || !File.Exists(args[0]))
{
Console.WriteLine(@"Usage: Encode <file>");
Console.ReadKey();
return;
}
string fileName = args[0];
string intermediateFile = fileName + ".encrypted"; /// Intermediate file, output of cryptography encoding
string backupFile = fileName + ".bak";
try
{
using (StreamReader reader = new StreamReader(fileName))
{
if (!reader.ReadLine().StartsWith("<?xml version="))
{
Console.WriteLine(string.Format("File {0} is already encrypted.", fileName));
Console.ReadKey();
return;
}
}
bool successful = false;
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
{
using (FileStream fsCrypt = new FileStream(intermediateFile, FileMode.Create))
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
{
using (FileStream fsIn = new FileStream(fileName, FileMode.Open))
{
int data;
while ((data = fsIn.ReadByte()) != -1) cs.WriteByte((byte)data);
successful = true;
}
}
}
}
}
if (successful)
{
/// File encryption successfuly completed => rotate files
if (File.Exists(backupFile)) File.Delete(backupFile);
File.Move(fileName, backupFile);
File.Move(intermediateFile, fileName);
return;
}
else
{
Failed(fileName, intermediateFile);
return;
}
}
catch (Exception)
{
Failed(fileName, intermediateFile);
return;
}
}
static void Failed(string fileName, string toBeDeleted)
{
if (File.Exists(toBeDeleted))
{
File.Delete(toBeDeleted);
}
Console.WriteLine(string.Format("Encryption of file {0} failed.", fileName));
Console.ReadKey();
}
}
}