Skip to content

Instantly share code, notes, and snippets.

@jimmckeeth
Last active August 15, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimmckeeth/44ccb19032e41f4dfc7963f00c706461 to your computer and use it in GitHub Desktop.
Save jimmckeeth/44ccb19032e41f4dfc7963f00c706461 to your computer and use it in GitHub Desktop.
Implements the Rot47 text obfuscation and de-obfuscation routine for Delphi 10.4 Sydney (Similar to Rot13)
uses Math;
function ROT47(UnRot: string): string;
// More information https://en.wikipedia.org/wiki/ROT13#Variants
begin
Result := UnRot;
for var I := 1 to Length(Result) do
begin
var o := ord(Result[i]);
case o of
33..79: Result[i] := Chr(Ord(Result[i]) + 47);
80..126: Result[i] := Chr(Ord(Result[i]) - 47);
end;
end;
end;
// bonus
function ROT13(UnRot: string): string;
// Based on code by Andreas Rejbrand
// https://stackoverflow.com/a/6800389/255
const
OrdBigA = Ord('A');
OrdBigZ = Ord('Z');
OrdSmlA = Ord('a');
OrdSmlZ = Ord('z');
begin
Result := UnRot;
for var i := 1 to length(Result) do
begin
var o := Ord(Result[i]);
if InRange(o, OrdBigA, OrdBigZ) then
Result[i] := Chr(OrdBigA + (o - OrdBigA + 13) mod 26)
else if InRange(o, OrdSmlA, OrdSmlZ) then
Result[i] := Chr(OrdSmlA + (o - OrdSmlA + 13) mod 26);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment