I’ve used this simple code that encrypts plain text. Then I tried to decrypt it using the same encrypting method but reversed in encryption section. There’s a multiplication process that I don’t know how to reverse it in the decryption code.
Here is the code:
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;
You are trying to perform integer division. In Delphi you do that with
div. The/operator is for floating point division. Looking at the code, you are trying to reverse this calculation:You reverse that like this:
However, your algorithm will not work. Consider what happens when you encrypt 64 and 128. You multiply by 4 to get 256 and 512 respectively. Then add one to get 257 and 513. Then you store back to an 8 bit data type and lose the higher order bytes. And so both characters are encoded to the value 1.
I’m assuming that you are using 8 bit text. But if you are using 16 bit text, your algorithm still fails in an exactly analogous fashion. Your proposed algorithm is not reversible.
I urge you to find an off-the-shelf encryption algorithm rather than trying to write your own. Encryption is hard to get right.