Jump to content
Shrinavat

Simple string obfuscation/deobfuscation

Recommended Posts

How can I implement functions for basic string obfuscation and deobfuscation in Delphi, where the input string can contain Latin and Cyrillic letters, digits, and special characters !@#$%^&*()_+-=[]{}\|;':",./<>?

Requirements:

  • The obfuscated string must contain only Latin letters and digits.
  • The obfuscated string must have the same length as the original string.
  • Strong cryptographic security is not required.

I'm looking for a simple solution that can handle a variety of characters and maintain the original string length. Any suggestions or code examples would be greatly appreciated.

 

 

Edited by Shrinavat

Share this post


Link to post
13 minutes ago, Shrinavat said:
  • The obfuscated string must contain only Latin letters and digits.
  • The obfuscated string must have the same length as the original string.

These two do make things very hard and even impossible to put in an algorithm, i mean going form wider range (arbitrary strings with special characters) of data to narrower output range (only Latin and digits) and while keeping length.

 

You have to drop one of them or accept some degree of length increase !

I can't think of an alternative approach or existing algorithm, unless the length of your strings are long enough (must be big and long, or may be with repeated words and parts) to add compression layer that decrease the input strings to a sufficient length to do the presentation with Latin and digits.

  • Thanks 1

Share this post


Link to post

Thank you for your insights. I understand the challenges involved in meeting both requirements simultaneously.
I'm willing to remove the constraint that "The obfuscated string must contain only Latin letters and digits."
Revised requirements:

  • The obfuscated string can contain any printable characters.
  • The obfuscated string must have the same length as the original string.
  • Strong cryptographic security is not required.
  • The input strings will generally not exceed 30 characters in length.

With this revised requirement, are there any suitable algorithms or approaches you could suggest for implementing the obfuscation and deobfuscation functions?

Share this post


Link to post

Well see ROT13, Caesar and their family, these doesn't need key but easy to add one in case you want to, by shifting (adding) by the key, these it might fit your need

 

Start here

https://stackoverflow.com/questions/6800326/how-to-crypt-or-hide-a-string-in-delphi-exe

A nice playground for Cesar 

https://www.dcode.fr/caesar-cipher

And in general 

https://www.dcode.fr/en

 

Also https://www.dcode.fr/vigenere-cipher looks nice and does have a key

https://en.wikipedia.org/wiki/Vigenère_cipher

 

Yet from quick look at it, i think it might fit your need more than Cesar or ROT13, might be wrong though,

 

In all cases i hope this will give you at least start point for your search, and of course i believe many here will give you insight too.

 

  • Thanks 1

Share this post


Link to post
2 hours ago, Kas Ob. said:

Well see ROT13, Caesar and their family, these doesn't need key but easy to add one in case you want to, by shifting (adding) by the key, these it might fit your need

Thanks for the feedback! I ended up implementing a modified ROT13 algorithm with a custom alphabet, as shown in the code below. Initial tests indicate that it meets my requirements effectively.

const
  ALL_CHARACTERS_CONSTANT = '!#$%&()*+,-./:;<=>?@[\]^_`{|}~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя';

function ROT13Encrypt(const Input: string): string;
var
  idx, newIdx: Integer;
  halfLength: Integer;
begin
  Result := '';
  halfLength := Length(ALL_CHARACTERS_CONSTANT) div 2;

  for var i := 1 to Length(Input) do
  begin
    idx := Pos(Input[i], ALL_CHARACTERS_CONSTANT);
    if idx > 0 then
    begin
      newIdx := ((idx - 1 + halfLength) mod Length(ALL_CHARACTERS_CONSTANT)) + 1;
      Result := Result + ALL_CHARACTERS_CONSTANT[newIdx];
    end
    else
      Result := Result + Input[i];
  end;
end;

function ROT13Decrypt(const Input: string): string;
begin
  // ROT13 decryption is the same as encryption in this case
  Result := ROT13Encrypt(Input);
end;

 

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×