87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace xaskel_coder
|
|
{
|
|
internal class Program
|
|
{
|
|
private const int encodeSize = 3000; // byte count to encode
|
|
private static readonly List<string> extensions = new List<string> { ".col", ".txd", ".dff" };
|
|
|
|
static void Main()
|
|
{
|
|
Console.Write("Enter password: ");
|
|
string password = Console.ReadLine().Trim();
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
return;
|
|
|
|
Console.Write("Getting files list... ");
|
|
List<string> files = GetFiles(Console.ReadLine().Trim());
|
|
if (files.Count == 0)
|
|
return;
|
|
Console.WriteLine("Done.");
|
|
|
|
Console.Write("Encoding files... ");
|
|
foreach (string file in files)
|
|
{
|
|
EncryptFile(file, password);
|
|
}
|
|
Console.WriteLine("Done.");
|
|
|
|
Console.ReadLine();
|
|
}
|
|
|
|
static List<string> GetFiles(string directoryPath)
|
|
{
|
|
List<string> files = Directory.GetFiles(directoryPath, "*.*", SearchOption.TopDirectoryOnly)
|
|
.Where(file => extensions.Contains(Path.GetExtension(file).ToLower()))
|
|
.ToList();
|
|
return files;
|
|
}
|
|
|
|
static void EncryptFile(string filePath, string password)
|
|
{
|
|
string newFilePath = filePath + "rw";
|
|
|
|
using (var md5 = MD5.Create())
|
|
{
|
|
byte[] inputBytes = Encoding.UTF8.GetBytes(password),
|
|
hashBytes = md5.ComputeHash(inputBytes);
|
|
string key = BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 16);
|
|
|
|
byte[] data = File.ReadAllBytes(filePath);
|
|
int bytesToEncode = Math.Min(encodeSize, data.Length);
|
|
|
|
List<string> encoded = new List<string>();
|
|
for (int i = 0; i < bytesToEncode; i++)
|
|
{
|
|
encoded.Add(Tea.Encode(data[i].ToString(), key));
|
|
}
|
|
|
|
if (File.Exists(newFilePath))
|
|
{
|
|
File.Delete(newFilePath);
|
|
}
|
|
|
|
using (var fs = new FileStream(newFilePath, FileMode.CreateNew))
|
|
{
|
|
using (var writer = new BinaryWriter(fs))
|
|
{
|
|
string encodedString = string.Join("", encoded);
|
|
byte[] encodedBytes = Encoding.UTF8.GetBytes(encodedString);
|
|
writer.Write(encodedBytes);
|
|
|
|
if (data.Length > encodeSize)
|
|
{
|
|
writer.Write(data, encodeSize, data.Length - encodeSize);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |