Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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