UTF-8 проблема шифрования / дешифрования файлов, Delphi Community Edition - PullRequest
0 голосов
/ 28 апреля 2020

Delphi 10.3.2 Community edition. Я хочу открыть файл UTF-8, зашифровать его и сохранить в зашифрованном виде. И, конечно, наоборот: я хочу открыть и расшифровать зашифрованный файл и сохранить его в формате UTF-8. Проблема в том, что я не могу правильно настроить мониторинг в заметках. Как решить эту проблему?

Тест MD5 и сравнение сохраненных файлов myUTF8textfile.txt (исходный файл) и myUTF8textfileDecrypted.txt показывают, что шифрование / дешифрование UTF8-файлов прошло правильно. Проблема заключается в том, как настроить мониторинг. Записки показывают правильный текст UTF8.

Вот пример кода:

//The Function for the most simple Encryption/Decryption of string

// https://edn.embarcadero.com/article/28325
// It's the same function for the encryption and decryption
function TForm1.EnDeCrypt(const Value : String) : String;
var
  CharIndex : integer;
begin
  Result := Value;
  for CharIndex := 1 to Length(Value) do
    Result[CharIndex] := chr(not(ord(Value[CharIndex])));
end;

----------------------

var myfile, mycryptedfile : file;
var tempstring, EnCryptedString, DeCryptedString  : string;
var c : char;
i : integer;

begin

//ENCONDING THE FILE

//Reading bytes from the file (could be any sort of file..)

AssignFile(myfile, 'myUTF8textfile.txt'); 
reset(myfile,1);
tempstring := '';
Try 
  while not (eof(myfile)) do
  begin
    BlockRead(myfile, c,1);  
    tempstring := tempstring + c;
  end;
  Finally
  CloseFile(myfile);
end;

//Monitoring the content:
//The problem is here: the temp string contains all information of UTF-8 string file 
//but....
Memo1.text := tempstring;  //Why does Memo1 not show the the text right, it looks like corrupted ANSI or something?

//----------------------------
//Encrypting the string:
//For this I'll make something more effective in future
  EnCryptedString := EnDeCrypt(tempstring);
//----------------------------

//Saving encrypted stuff to the file:

AssignFile(mycryptedfile, 'myUTF8textfile.cry');
 rewrite(mycryptedfile,1);
for i := 1 to (length(EnCryptedString)) do
            begin
             BlockWrite(mycryptedfile, EnCryptedString[i],1);
            end;
 CloseFile(mycryptedfile);


//DECODING OF THE FILE

//Reading bytes from the encoded file

AssignFile(myfile, 'myUTF8textfile.cry'); 
reset(myfile,1);
tempstring := '';
Try 
  while not (eof(myfile)) do
  begin
    BlockRead(myfile, c,1);  
    tempstring := tempstring + c;
  end;
  Finally
  CloseFile(myfile);
end;

//----------------------------
//Decrypting the string
  DeCryptedString := EnDeCrypt(tempstring);
//----------------------------

//Monitoring the content:
//The problem is here again: the DeCryptedString should be exactly the
//as the original tempstring before encryption but
Memo2.text := DeCryptedString;  //Memo2 do not  show the the text right.

//Saving Decrypted file:

AssignFile(mycryptedfile, 'myUTF8textfileDecrypted.txt');
 rewrite(mycryptedfile,1);
for i := 1 to (length(DeCryptedString)) do
            begin
             BlockWrite(mycryptedfile, DeCryptedString[i],1);
            end;
 CloseFile(mycryptedfile);

end;
...