【问题标题】:Pascal code to C# code conversion (ord and chr)Pascal 代码到 C# 代码的转换(ord 和 chr)
【发布时间】:2021-02-04 13:51:29
【问题描述】:

我实际上是在尝试将 pascal 代码转换为 c# 代码(我们正在重写旧应用程序)。

帕斯卡码:

    function DecryptStr(Source: PChar): string;
var
  st: string;
  i, k, mask: byte;
begin
  Result := '';
  try
    SetString(st, Source, 32);
    if st[1] <> #0 then
    begin
      mask := ord(st[1]);
      k := ord(st[2]) xor mask;
      SetLength(Result, k);
      for i := 1 to k do
      begin
        inc(mask);
        k := ord(st[i + 2]) xor mask;
        Result[i] := chr(k);
//        Result := Result + chr(k);
      end;
    end;
  except
  end;
end;     

还有我的 C# 代码:

public static string decrypt(string hash)
        {   string buffer;
            byte i, k, mask;
            string result = "";
            buffer = hash.Substring(0, 32);
            mask = (byte)(buffer[0]);
            k = (byte)((byte)(buffer[1])^mask);
            for (i = 0; i<k-1; i++)
            {
                mask += 1;
                k = (byte)((byte)(buffer[i+2])^mask);
                result+=(char)(k);
            }
           // string decoded = System.
            return (result);

        }

请告诉我,是不是很相似,还是帕斯卡有一些隐藏的东西?

示例: 在c#中输入:

акРЖђЏГ€€њљђНѓGН q6™&і—'n1•\›ЛH[

c# 输出:

\u0011$\f\v&\t\b

但它看起来不像真正的密码。 请告诉我出了什么问题。

【问题讨论】:

  • 什么是真正的密码/帕斯卡码的输出是什么?
  • 术语hash 不正确。您无法解密哈希。
  • 旧代码是用什么版本的delphi编写的?知道字符串是 ANSI 还是 UTF-8 可能很重要...
  • Pascal 的哪个版本?似乎原始字符串是Unicode。在这种情况下,您不能简单地将其转换为 (byte)。你可以看看Encoding.GetBytes()
  • 谢谢,我正在尝试!

标签: c# delphi pascal lazarus


【解决方案1】:

问题在于两件事:

  1. 输入字符串的 PChar 格式 - 表示它在末尾存储“0”。
  2. 愚蠢的加密机制 - 实际上,写下所代表代码的人只是选择了字节掩码作为 rand(200) + 32 将其存储在加密通道的第一个字节中,然后在循环中他将掩码递增所有通过长度的每一步。 所以解密的逻辑如下:
  3. 在这种情况下抓取掩码 - 缓冲区 [0]
  4. 使用此掩码对每个其他字符执行 XOR,每一步将其增加一个。 3.???
  5. 利润! 感谢所有参与此主题的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2013-08-24
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多