-1

我使用了这个加密纯文本的简单代码。然后我尝试使用相同的加密方法对其进行解密,但在加密部分进行了反转。有一个乘法过程,我不知道如何在解密代码中反转它。

这是代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  s: String;
  count, ilength: Integer;
begin
  s := edit1.Text;
  ilength := Length(s);
  FOR count := 1 to ilength do
  begin
    s[count] := chr(ord(s[count]) * 4 + 1); // Encoding
  end;
  Label1.caption := s;
  // Display encoded text
  // Decoding section
  // This will probably be placed in another procedure.
  FOR count := 1 to ilength do
  begin
    s[count] := chr(ord((s[count]) / 4) - 1);
    // Here I Get An Error ! Please Help Guys, Thanks
  end;
end;
4

2 回答 2

10

您正在尝试执行整数除法。在 Delphi 中,您可以使用div. /运算符用于浮点除法。查看代码,您正在尝试反转此计算:

ord(s[count]) * 4 + 1

你像这样反转:

(ord(s[count]) - 1) div 4

但是,您的算法将不起作用。考虑一下加密 64 和 128 时会发生什么。乘以 4 分别得到 256 和 512。然后加一得到 257 和 513。然后存储回 8 位数据类型并丢失高位字节。所以这两个字符都被编码为值 1。

我假设您使用的是 8 位文本。但是,如果您使用 16 位文本,您的算法仍然会以完全相同的方式失败。您提出的算法是不可逆的。

我敦促您找到现成的加密算法,而不是尝试编写自己的加密算法。加密很难正确。

于 2012-12-26T15:57:21.173 回答
5

/用来做除法。这将返回一个浮点数。改为DIV返回一个整数。

于 2012-12-26T15:57:09.383 回答