本帖最后由 qq465881818 于 2026-9-15 21:05 编辑
[C#] 纯文本查看 复制代码 using System;
using System.Numerics;
using System.Runtime.CompilerServices;
internal class Program
{
//--------------------------------------------------------------------------
// Stage1 的目标数组: 内嵌于 <PrivateImplementationDetails> 的 FieldRVA 常量
// 字段 token 0x04000001, RVA 0x2048 (文件偏移 0x248), 长度 8 字节
//--------------------------------------------------------------------------
private static readonly byte[] Expected =
{
0x35, 0x33, 0x74, 0x78, 0x7A, 0x02, 0x09, 0x71
};
private static void Main(string[] args)
{
Console.Title = "CrackMe - Algorithm Challenge";
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(
"\r\n" +
" ╔══════════════════════════════════════════╗\r\n" +
" ║ CRACKME 算法挑战 - 五星难度 ║\r\n" +
" ║ 输入正确的密码字符串即可破解 ║\r\n" +
" ╚══════════════════════════════════════════╝\r\n" +
" ");
Console.ResetColor();
Console.Write("请输入密码: ");
string input = Console.ReadLine();
bool ok = VerifyPassword(input);
if (ok)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\n恭喜!密码正确!你成功破解了这道题!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n密码错误!继续努力!");
Console.ResetColor();
}
Console.WriteLine("\n按任意键退出...");
Console.ReadKey();
}
//--------------------------------------------------------------------------
// 总校验: 任意一个阶段失败即返回 false
//--------------------------------------------------------------------------
private static bool VerifyPassword(string pwd)
{
if (string.IsNullOrEmpty(pwd))
return false;
if (!Stage1_CharShift(pwd))
return false;
if (!Stage2_Polynomial(pwd))
return false;
if (!Stage3_CRC(pwd))
return false;
if (!Stage4_Matrix(pwd))
return false;
if (!Stage5_Hash(pwd))
return false;
return true;
}
//--------------------------------------------------------------------------
// 阶段 1 - 逐字符位移比对
// (input[i] + i*i) % 128 == Expected[i]
// 同时隐含约束: input.Length == 8
// 反推: input[i] = (Expected[i] - i*i) mod 128
//--------------------------------------------------------------------------
private static bool Stage1_CharShift(string input)
{
if (input.Length != Expected.Length)
return false;
for (int i = 0; i < input.Length; i++)
{
byte v = (byte)(((byte)input[i] + i * i) % 128);
if (v != Expected[i])
return false;
}
return true;
}
//--------------------------------------------------------------------------
// 阶段 2 - 多项式求和 (BigInteger)
// sum( 7*c^3 + 5*c^2 + 3*c + 1 ) == 47148428
//--------------------------------------------------------------------------
private static bool Stage2_Polynomial(string input)
{
BigInteger sum = 0;
for (int i = 0; i < input.Length; i++)
{
int c = input[i];
sum += 7 * c * c * c + 5 * c * c + 3 * c + 1;
}
return sum == 47148428;
}
//--------------------------------------------------------------------------
// 阶段 3 - 8 位 CRC (多项式 0x8D = 141, 初值 0, 无反射)
// 每字节: crc ^= c; 然后 8 次: 若最高位为 1 则 (crc<<1)^poly 否则 crc<<1
// 终值 == 135
//--------------------------------------------------------------------------
private static bool Stage3_CRC(string input)
{
byte crc = 0;
const byte poly = 141; // 0x8D
for (int i = 0; i < input.Length; i++)
{
crc = (byte)(crc ^ (byte)input[i]);
for (int j = 0; j < 8; j++)
{
if ((crc & 0x80) > 0)
crc = (byte)((crc << 1) ^ poly);
else
crc = (byte)(crc << 1);
}
}
return crc == 135;
}
//--------------------------------------------------------------------------
// 阶段 4 - 前后半段加权和 (二元一次方程组)
// front = sum(input[0 .. len/2-1]), back = sum(input[len/2 .. len-1])
// 3*front + 7*back == 3610
// 11*front + 5*back == 5466
// => front = 326, back = 376
//--------------------------------------------------------------------------
private static bool Stage4_Matrix(string input)
{
int half = input.Length / 2;
int frontSum = 0;
for (int i = 0; i < half; i++)
frontSum += input[i];
int backSum = 0;
for (int j = half; j < input.Length; j++)
backSum += input[j];
int a = 3 * frontSum + 7 * backSum;
int b = 11 * frontSum + 5 * backSum;
if (a != 3610)
return false;
return b == 5466;
}
//--------------------------------------------------------------------------
// 阶段 5 - 64 位 FNV-1a 变体 (额外增加循环左移 7 位与常量异或)
// h 初值 0xCBF29CE484222325 (FNV offset basis)
// 每字符: h ^= (byte)c; h *= 0x100000001B3; h = rotl(h,7); h ^= 0xABCDEF0123456789
// 终值 == 0x118E2281736DDE12
//--------------------------------------------------------------------------
private static bool Stage5_Hash(string input)
{
ulong h = 0xCBF29CE484222325UL;
for (int i = 0; i < input.Length; i++)
{
h ^= (byte)input[i];
h *= 0x100000001B3UL;
h = (h << 7) | (h >> 57); // 64 位循环左移 7
h ^= 0xABCDEF0123456789UL;
}
return h == 0x118E2281736DDE12UL;
}
}
|