How can I apply a simple XOR encryption/decryption mechanism in Delphi with no third party packages?

Gege GMF

How can I apply a file encryption/decryption mechanism correctly in Delphi?

I found a very old source that supposedly teaches how to do just that, but using strings. After my previous question here, that doesn't appear to be the correct approach. Rather I should perform byte operations. This is what my old source proposes:

1-) Read the file using the old binded C function:

function ReadFile(path: String): AnsiString;
var
  file: File;
  type: Byte;
begin
  type:= FileMode;
  try
    FileMode := 0;
    AssignFile(file, path);
{$I-}
    Reset(file, 1);
{$I+}
    if IOResult = 0 then
      try
        SetLength(Result, FileSize(file));

        if Length(Result) > 0 then

        begin
{$I-}
          BlockRead(file, Result[1], Length(Result));
{$I+}
          if IOResult <> 0 then
            Result := '';
        end;
      finally
        CloseFile(file);
      end;
  finally
    FileMode := type;
  end;
end;

2-) Apply the XOR operations character by character on this huge AnsiString, like so:

 function Xor(Buffer: String; Key: integer): String;
  var
    i, c, x: integer;
  begin
    for i := 1 to Length(Buffer) do
    begin
      c := integer(Buffer[i]);
      x := c xor Key;
      Result := Result + Char(x);
    end;
  end;

But the problem afterwards, and is what my source does NOT show, is how to convert this transformed string back to bytes so I can have a functional resource to write back to a new decrypted file.

Apparently this is not the correct approach to encrypt and decrypt a file. Can someone please explain to me the correct approach to properly:

Read a file, apply a basic XOR encryption mechanism and then be able to read this file again, decrypt it and use this VALID binary (DLL, EXE) data to write back into a new decrypted file?

rook

A "simple xor encryption" will not improve security of an application. Make sure to add a comment informing any reader that this performs no security benefit, as it might confuse another developer into thinking that it raises the bar against an attacker. If this was a security system then you would need to use AES for encryption or SHA-256/SHA3 for hashing, or PBKDF2/Scrypt for password storage.

That being said, GitHub search is powerful, here is some delphi code that obfuscates bits: https://github.com/EonaCat/NightBitsEncryptor/blob/master/NightBitsEncryptor.pas

The above code is (in)security though obscurity and must never be used as a security system.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I full parsing HTML without third party library?

From Dev

How can I use a third party Class Object as Hashmap Key?

From Dev

How can I contribute to a third-party Symfony bundle?

From Dev

How can I use a third party library: SWXMLHash in the playground ?

From Dev

How do I get the list of third-party python packages that I have installed with pip?

From Dev

How to use third party npm packages with ember cli app

From Dev

How do I cancel third party task?

From Dev

AngularJS, Leaflet. How can I get lat and lon coordinates dynamically from a third-party service?

From Dev

How can I structure a mocha unit test that involves promises and third party NPM modules?

From Dev

How can I include third-party javascript to run before my GWTTestCase?

From Dev

In Rust how can I define or import a C struct from a third party library?

From Dev

How can I extract data from third party apps from an iPad?

From Dev

How can I add an external, third-party dependency to a perl6 project?

From Dev

Can I / How to Implement IEnumerable on third party object to Sort...just use LINQ?

From Dev

How can I use a third party api in the vendors directory of symfony from my controller

From Dev

How can I use third party dependencies in custom task under buildSrc in Gradle

From Dev

How can I feed third-party RSS reader with Firebase data?

From Dev

How can I remap my OSX keyboard without using third-party programs?

From Dev

How to 'import' a third party Javascript file into IntelliJ so that I can refer to it in a Typescript file

From Dev

In Rust how can I define or import a C struct from a third party library?

From Dev

Instead of using Maven to pull in a third party dependency, how can I implement the source of that dependency directly into my project?

From Dev

How to compile a third party Java library into a jar file I can use?

From Dev

How can I use a third party api in the vendors directory of symfony from my controller

From Dev

How can I obtain IP information without third party tools in Linux?

From Dev

How can I make my application find a third-party DLL using Visual Studio?

From Dev

How can i get the min value and max value from the rc slider third party component

From Dev

TYPO3: How can I allow third party query string parameters?

From Dev

Can I change a dll reference within a third-party dll?

From Dev

Can I load a third-party .dll in R?

Related Related

  1. 1

    How can I full parsing HTML without third party library?

  2. 2

    How can I use a third party Class Object as Hashmap Key?

  3. 3

    How can I contribute to a third-party Symfony bundle?

  4. 4

    How can I use a third party library: SWXMLHash in the playground ?

  5. 5

    How do I get the list of third-party python packages that I have installed with pip?

  6. 6

    How to use third party npm packages with ember cli app

  7. 7

    How do I cancel third party task?

  8. 8

    AngularJS, Leaflet. How can I get lat and lon coordinates dynamically from a third-party service?

  9. 9

    How can I structure a mocha unit test that involves promises and third party NPM modules?

  10. 10

    How can I include third-party javascript to run before my GWTTestCase?

  11. 11

    In Rust how can I define or import a C struct from a third party library?

  12. 12

    How can I extract data from third party apps from an iPad?

  13. 13

    How can I add an external, third-party dependency to a perl6 project?

  14. 14

    Can I / How to Implement IEnumerable on third party object to Sort...just use LINQ?

  15. 15

    How can I use a third party api in the vendors directory of symfony from my controller

  16. 16

    How can I use third party dependencies in custom task under buildSrc in Gradle

  17. 17

    How can I feed third-party RSS reader with Firebase data?

  18. 18

    How can I remap my OSX keyboard without using third-party programs?

  19. 19

    How to 'import' a third party Javascript file into IntelliJ so that I can refer to it in a Typescript file

  20. 20

    In Rust how can I define or import a C struct from a third party library?

  21. 21

    Instead of using Maven to pull in a third party dependency, how can I implement the source of that dependency directly into my project?

  22. 22

    How to compile a third party Java library into a jar file I can use?

  23. 23

    How can I use a third party api in the vendors directory of symfony from my controller

  24. 24

    How can I obtain IP information without third party tools in Linux?

  25. 25

    How can I make my application find a third-party DLL using Visual Studio?

  26. 26

    How can i get the min value and max value from the rc slider third party component

  27. 27

    TYPO3: How can I allow third party query string parameters?

  28. 28

    Can I change a dll reference within a third-party dll?

  29. 29

    Can I load a third-party .dll in R?

HotTag

Archive