2019-07-17 04:47:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Decrypt
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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]))
|
|
|
|
|
|
{
|
2025-07-18 06:50:22 +00:00
|
|
|
|
Console.WriteLine(@"Usage: Decode <file>");
|
2019-07-17 04:47:08 +00:00
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string fileName = args[0];
|
|
|
|
|
|
string intermediateFile = fileName + ".plain"; /// Intermediate file, output of cryptography decoding
|
|
|
|
|
|
string backupFile = fileName + ".bak";
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (reader.ReadLine().StartsWith("<?xml version="))
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(string.Format("File {0} is not encrypted.", fileName));
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool successful = false;
|
|
|
|
|
|
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
|
|
|
|
|
|
{
|
|
|
|
|
|
using (FileStream fsCrypt = new FileStream(fileName, FileMode.Open))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (FileStream fsOut = new FileStream(intermediateFile, FileMode.Create))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
|
|
|
|
|
|
{
|
|
|
|
|
|
int data;
|
|
|
|
|
|
while ((data = cs.ReadByte()) != -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (data != 0) fsOut.WriteByte((byte)data);
|
|
|
|
|
|
}
|
|
|
|
|
|
successful = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (successful)
|
|
|
|
|
|
{
|
|
|
|
|
|
/// File decryption 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 e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Failed(fileName, intermediateFile, e.Message);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Failed(string fileName, string toBeDeleted, string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(toBeDeleted))
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(toBeDeleted);
|
|
|
|
|
|
}
|
|
|
|
|
|
Console.WriteLine(string.Format("Decryption of file {0} failed: {1}", fileName, message));
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|