Files
HLib/Encryption/FIleEncryptUxer.cs
T
2026-06-01 13:23:34 +09:00

272 lines
12 KiB
C#

using KssSmaPlaLib.Commons;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Encryption
{
public static class FIleEncryptUxer
{
public static bool TuckIntoMain(string[] args)
{
if (args.Length < 2) return false;
if (args[0].ToLower() == "/encfile")
{
var srcPath = args[1];
var descPath = args.Length >= 3 ? args[2] : args[1] + ".enc";
var password= args.Length >= 4 ? args[3] : string.Empty;
if (TryEncrypt(srcPath, descPath, out var messageForMe, password))
{
LoggerUxer.Info($"ファイル暗号化が成功しました。");
}
else
{
LoggerUxer.Error($"ファイル暗号化が失敗しました。[エラー内容:{messageForMe}]");
}
return true;
}
else if (args[0].ToLower() == "/decfile")
{
var srcPath = args[1];
var descPath = args.Length >= 3 ? args[2] : (srcPath.EndsWith(".enc") ? srcPath.TrimEnd(".enc".ToArray()) : srcPath + ".src");
var password = args.Length >= 4 ? args[3] : string.Empty;
if (TryDecrypt(srcPath, descPath, out var messageForMe, password))
{
Console.WriteLine($"ファイル複合化が成功しました。");
}
else
{
Console.WriteLine($"ファイル複合化が失敗しました。[エラー内容:{messageForMe}]");
}
return true;
}
else if (args[0].ToLower() == "/encbigfile")
{
var srcPath = args[1];
var descPath = args.Length >= 3 ? args[2] : args[1] + ".enc";
var password = args.Length >= 4 ? args[3] : string.Empty;
// テキストのみ方式
if (TryEncryptLargeFileToBase64(srcPath, descPath, out var messageForMe, true, password))
{
LoggerUxer.Info($"ファイル暗号化が成功しました。");
}
else
{
LoggerUxer.Error($"ファイル暗号化が失敗しました。[エラー内容:{messageForMe}]");
}
return true;
}
else if (args[0].ToLower() == "/decbigfile")
{
var srcPath = args[1];
var descPath = args.Length >= 3 ? args[2] : (srcPath.EndsWith(".enc") ? srcPath.TrimEnd(".enc".ToArray()) : srcPath + ".src");
var password = args.Length >= 4 ? args[3] : string.Empty;
if (TryDecryptLargeFileFromBase64(srcPath, descPath, out var messageForMe, true, password))
{
Console.WriteLine($"ファイル複合化が成功しました。");
}
else
{
Console.WriteLine($"ファイル複合化が失敗しました。[エラー内容:{messageForMe}]");
}
return true;
}
return false;
}
public static bool TryEncryptLargeFileToBase64(string inputFilePath, string outputTextFilePath, out string resultMessage, bool isAllText = false, string password = "")
{
if (string.IsNullOrEmpty(password)) password = SimpleEncryptUxer.FixedPassword;
try
{
byte[] key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
using (var aes = Aes.Create())
{
aes.Key = key;
aes.GenerateIV();
using (var inputStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (var outputStream = new FileStream(outputTextFilePath, FileMode.Create, FileAccess.Write))
{
if (isAllText)
{
using (var memoryStream = new MemoryStream())
using (var encryptor = aes.CreateEncryptor())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, bytesRead);
}
cryptoStream.FlushFinalBlock();
string ivBase64 = Convert.ToBase64String(aes.IV);
string cipherBase64 = Convert.ToBase64String(memoryStream.ToArray());
string outputText = $"{ivBase64}::{cipherBase64}";
byte[] outputBytes = Encoding.UTF8.GetBytes(outputText);
outputStream.Write(outputBytes, 0, outputBytes.Length);
}
}
else
{
// IVはバイナリで先頭に書き込む
outputStream.Write(aes.IV, 0, aes.IV.Length);
using (var base64Transform = new ToBase64Transform())
using (var cryptoStream = new CryptoStream(outputStream, base64Transform, CryptoStreamMode.Write))
using (var encryptor = aes.CreateEncryptor())
using (var aesStream = new CryptoStream(cryptoStream, encryptor, CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
aesStream.Write(buffer, 0, bytesRead);
}
aesStream.FlushFinalBlock();
}
}
}
}
resultMessage = "暗号化成功";
return true;
}
catch (Exception ex)
{
resultMessage = $"暗号化失敗: {ex.Message}";
return false;
}
}
public static bool TryDecryptLargeFileFromBase64(string inputTextFilePath, string outputFilePath, out string resultMessage, bool isAllText = false, string password = "")
{
resultMessage = string.Empty;
if (string.IsNullOrEmpty(password)) password = SimpleEncryptUxer.FixedPassword;
try
{
byte[] key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
using (var aes = Aes.Create())
{
aes.Key = key;
if (isAllText)
{
string inputText = File.ReadAllText(inputTextFilePath, Encoding.UTF8);
string[] parts = inputText.Split(new[] { "::" }, StringSplitOptions.None);
if (parts.Length != 2)
{
resultMessage = $"暗号ファイルの形式が不正です(::区切りが見つかりません)";
return false;
}
byte[] iv = Convert.FromBase64String(parts[0]);
byte[] cipherBytes = Convert.FromBase64String(parts[1]);
aes.IV = iv;
using (var decryptor = aes.CreateDecryptor())
using (var memoryStream = new MemoryStream(cipherBytes))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var outputStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = cryptoStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
}
}
}
else
{
using (var inputStream = new FileStream(inputTextFilePath, FileMode.Open, FileAccess.Read))
{
byte[] iv = new byte[16];
inputStream.Read(iv, 0, iv.Length);
aes.IV = iv;
using (var base64Transform = new FromBase64Transform())
using (var cryptoStream = new CryptoStream(inputStream, base64Transform, CryptoStreamMode.Read))
using (var decryptor = aes.CreateDecryptor())
using (var aesStream = new CryptoStream(cryptoStream, decryptor, CryptoStreamMode.Read))
using (var outputStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = aesStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
return true;
}
catch (Exception ex)
{
resultMessage = $"ファイルの復号化に失敗しました。[エラー内容:{ex.Message}]";
return false;
}
}
public static bool TryEncrypt(string inputFilePath, string outputTextFilePath, out string resultMessage, string password = "")
{
try
{
string base64Data = Convert.ToBase64String(File.ReadAllBytes(inputFilePath));
bool success = SimpleEncryptUxer.Encrypt(base64Data, out var resultText, out var messageForMe, password);
resultMessage = messageForMe;
if (success)
{
File.WriteAllText(outputTextFilePath, resultText, Encoding.UTF8);
return true;
}
return false;
}
catch (Exception ex)
{
resultMessage = $"ファイル暗号化失敗: {ex.Message}";
return false;
}
}
public static bool TryDecrypt(string inputTextFilePath, string outputBinaryFilePath, out string resultMessage, string password = "")
{
try
{
string encryptedText = File.ReadAllText(inputTextFilePath, Encoding.UTF8);
bool success = SimpleEncryptUxer.Decrypt(encryptedText, out var resultText, out var messageForMe, password);
resultMessage = messageForMe;
if (success)
{
byte[] binaryData = Convert.FromBase64String(resultText);
File.WriteAllBytes(outputBinaryFilePath, binaryData);
return true;
}
return false;
}
catch (Exception ex)
{
resultMessage = $"ファイル復号化失敗: {ex.Message}";
return false;
}
}
}
}