Files
2026-02-23 10:28:26 +08:00

124 lines
5.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAppTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
if (args.Length == 0) return; // 无参数则静默退出
try
{
string[] revitExts = { ".rvt", ".rfa", ".rft", ".rte" };
foreach (string filePath in args)
{
if (!File.Exists(filePath)) continue;
string ext = Path.GetExtension(filePath).ToLowerInvariant();
if (revitExts.Contains(ext))
{
Decrypt(filePath);
}
}
}
catch (Exception ex)
{
// 可选:记录错误(调试用)
// File.WriteAllText(Path.Combine(Path.GetTempPath(), "rvt_clean_error.txt"), ex.ToString());
}
//Encrypt("E:\\Users\\Zhanggg\\Downloads\\AC35kV 1×240mm2电力电缆.rfa");
return;
//FileHashRsaHelper.GenerateEncFile(@"D:\Users\Zhanggg\Desktop\");
//FileHashRsaHelper.VerifyEncFile(@"D:\Users\Zhanggg\Desktop\");
//string file = "E:\\Users\\Zhanggg\\Desktop\\新建文件夹\\1.ifc";
//string subjectName = "CN=MyApp Code Signing";
//string pfxPassword = "YourSecurePassword123!";
//string pfxPath = "E:\\Users\\Zhanggg\\Desktop\\新建文件夹\\MySelfSignedCert.pfx";
//var cert = DigitalSigner.CreateSelfSignedCertificate(subjectName);
//File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pfx, pfxPassword));
// 签名
//DigitalSigner.SignFileAuthenticode(file, pfxPath, pfxPassword, "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.22621.0\\x64\\signtool.exe");
//DigitalSigner.SignFile(file, pfxPath, pfxPassword);
// 验证
//bool valid = DigitalSigner.VerifySignature(file, file + ".sig", pfx);
//Console.WriteLine("签名验证 " + (valid ? "成功" : "失败"));
}
private static void Decrypt(string filePath, string tag = "VmVyc2lvbiAxLjAgU1pNZWRpRGF0YQ==")
{
// 关键:用与写入时相同的编码(通常 UTF-8 或 ASCII
byte[] tagBytes = Encoding.UTF8.GetBytes(tag); // 或 Encoding.ASCII
byte[] data = File.ReadAllBytes(filePath);
// 检查开头和结尾是否匹配
bool startMatch = data.Length >= tagBytes.Length &&
ArrayEquals(data, 0, tagBytes, 0, tagBytes.Length);
bool endMatch = data.Length >= tagBytes.Length * 2 &&
ArrayEquals(data, data.Length - tagBytes.Length, tagBytes, 0, tagBytes.Length);
if (startMatch && endMatch)
{
// 提取中间部分:去掉开头和结尾的 tag
int start = tagBytes.Length;
int length = data.Length - 2 * tagBytes.Length;
byte[] original = new byte[length];
Array.Copy(data, start, original, 0, length);
File.WriteAllBytes(filePath, original);
Console.WriteLine("✅ 文件已成功还原!");
}
else
{
Console.WriteLine("❌ 文件开头或结尾不匹配标记字符串。");
}
}
private static void Encrypt(string markedRvtPath, string tag = "VmVyc2lvbiAxLjAgU1pNZWRpRGF0YQ==")
{
// 选择编码:通常用 UTF-8 或 ASCII必须与后续读取时一致
Encoding encoding = Encoding.UTF8; // 或 Encoding.ASCII
byte[] tagBytes = encoding.GetBytes(tag);
byte[] originalData = File.ReadAllBytes(markedRvtPath);
// 构造新文件内容:[tag] + [原始内容] + [tag]
long newLength = (long)tagBytes.Length * 2 + originalData.Length;
byte[] markedData = new byte[newLength];
// 写入头部 tag
//Array.Copy(tagBytes, 0, markedData, 0, tagBytes.Length);
// 写入原始内容
Array.Copy(originalData, 0, markedData, tagBytes.Length, originalData.Length);
// 写入尾部 tag
Array.Copy(tagBytes, 0, markedData, tagBytes.Length + originalData.Length, tagBytes.Length);
// 保存新文件
File.WriteAllBytes(markedRvtPath, markedData);
Console.WriteLine("✅ 文件已成功加密!");
}
static bool ArrayEquals(byte[] a, int aStart, byte[] b, int bStart, int length)
{
for (int i = 0; i < length; i++)
{
if (a[aStart + i] != b[bStart + i])
return false;
}
return true;
}
}
}