#1 2008-05-25 18:31

airjrdn
Member
Registered: 2007-07-14
Posts: 22

Pascal script to XOR encrypt filenames?

I have a couple of questions...

I've been using ReNamer a long time and love it, but I'm not sure how to accomplish this one.

Before backing files up to an online backup service, I wish to encrypt the filenames.  Since I don't have the space to make copies of the files, here's my thought.

Encrypt the filenames
Run the backup
Decrypt the filenames

The encryption needs to be able to easily flip the filenames back and forth between encrypted and unencrypted, hence my thought on using XOR.  I'm no Pascal programmer though, so I'm not sure how to do that using ReNamers powerful PascalScript rule.

My second question is, whether or not it's possible to call ReNamer from a command line, passing it a Preset to use, and a directory (and parm for subdirectories).  So, to automate my scenario above, I could just call it, run the backup, then call it again to rename the files back to normal again.

For safety reasons, I could do hardlinks I guess, but that could be version 2.

Offline

#2 2008-05-25 18:35

airjrdn
Member
Registered: 2007-07-14
Posts: 22

Re: Pascal script to XOR encrypt filenames?

I just found out that ReNamer does support command lines via this thread.

"C:\Program Files\ReNamer\ReNamer.exe" /preset "MyRulesPreset" "D:\Pictures\*.jpg"

Offline

#3 2008-05-25 20:17

airjrdn
Member
Registered: 2007-07-14
Posts: 22

Re: Pascal script to XOR encrypt filenames?

Here's a lame attempt at accomplishing my goal, but using Translit

encode
======
a=$01$
b=$02$
c=$03$
d=$04$
e=$05$
f=$06$
g=$07$
h=$08$
i=$09$
j=$10$
k=$11$
l=$12$
m=$13$
n=$14$
o=$15$
p=$16$
q=$17$
r=$18$
s=$19$
t=$20$
u=$21$
v=$22$
w=$23$
x=$24$
y=$25$
z=$26$
0=9
1=8
2=7
3=6
4=5
5=4
6=3
7=2
8=1
9=0


decode
======
$01$=a
$02$=b
$03$=c
$04$=d
$05$=e
$06$=f
$07$=g
$08$=h
$09$=i
$10$=j
$11$=k
$12$=l
$13$=m
$14$=n
$15$=o
$16$=p
$17$=q
$18$=r
$19$=s
$20$=t
$21$=u
$22$=v
$23$=w
$24$=x
$25$=y
$26$=z
0=9
1=8
2=7
3=6
4=5
5=4
6=3
7=2
8=1
9=0

However, decoded letters come back as all caps.

Offline

#4 2008-05-26 16:07

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Pascal script to XOR encrypt filenames?

Hi,
I think you can use xor in PascalScript.
Below I've written scripts for encoding/decoding using CASE statement (and only for first twelve CAPITAL letters, the rest is up to you).
I think you will find out what is what and if you would prefer to use xor, you'll do that smile

//encoding
var  
  character, TempName, BaseName, Extention: WideString;
  i: Integer;
  
begin
  
  BaseName:=WideExtractBaseName(FileName);
  Extention:=WideExtractFileExt(FileName);
  TempName:='';
  character:='';
//  BaseName:=BaseName+Extention     //uncomment that line if you would like to apply encoding also to extention
  
  for i:=1 to Length(BaseName) do
  begin
    character:=WideCopy(BaseName, i, 1);
    case character of
      'A': character := '$01$';        //here you can enclose your encoding routine
      'B': character := '$02$';
      'C': character := '$03$';
      'D': character := '$04$';
      'E': character := '$05$';
      'F': character := '$06$';
      'G': character := '$07$';
      'H': character := '$08$';
      'I': character := '$09$';
      'J': character := '$10$';
      'K': character := '$11$';
      'L': character := '$12$';
    else ;
    end;
    TempName:=TempName+character;
  end;
  
  FileName:=TempName+Extention;

  
end.

decoding

//decoding
var  
  character, temp, TempName, BaseName, Extention: WideString;
  i: Integer;
  capturing:  boolean;
  
begin
  capturing:=false;
  BaseName:=WideExtractBaseName(FileName);
  Extention:=WideExtractFileExt(FileName);
  TempName:='';
//  BaseName:=BaseName+Extention     //uncomment that line if you would like to apply decoding also to extention
  
  
  for i:=1 to Length(BaseName) do
  begin
    temp:=WideCopy(BaseName, i, 1);
 
    if capturing = false then character:='';         //
    if temp = '$' then                               //treat anything between two $'s as one character
    begin                                            
      if capturing=false then 
          capturing:=true
      else
          capturing:=false;
    end;
      
    
    character:=character+temp;
 
    if(capturing = false) then
    begin
        case character of
          '$01$': character := 'A';        //here you can enclose your decoding routine
          '$02$': character := 'B';
          '$03$': character := 'C';
          '$04$': character := 'D';
          '$05$': character := 'E';
          '$06$': character := 'F';
          '$07$': character := 'G'; 
          '$08$': character := 'H';
          '$09$': character := 'I';
          '$10$': character := 'J';
          '$11$': character := 'K';
          '$12$': character := 'L';
        else ;
        end;
    
        TempName:=TempName+character;
    end;
      
  end;

    
  
  FileName:=TempName+Extention;
  
end.

Let us know how it works.

Cheers,
Konrad

Last edited by krtek (2008-05-27 16:02)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#5 2008-05-26 16:08

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Pascal script to XOR encrypt filenames?

And I suggest using new feature: "Save filter with presets" when you're preparing presets for command line usage...


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#6 2008-05-26 18:01

airjrdn
Member
Registered: 2007-07-14
Posts: 22

Re: Pascal script to XOR encrypt filenames?

Thanks, I'll give this a shot.

Offline

#7 2008-05-26 21:16

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,376

Re: Pascal script to XOR encrypt filenames?

Here is a XOR encryption of the filename, against an integer key:

const
  KEY = 12345;
var
  C: Char;
  I: Integer;
begin
  for I:=1 to WideLength(FileName) do
  begin
    C := WideToAnsi(FileName[i])[1];
    FileName[i] := Chr(Ord(C) xor KEY);
  end;
end.

WARNING: This encryption will do you no good, because most of the characters will come out as "unprintable", "forbidden", or even "control" characters!! Also, this simple implementation discards the Unicode characters, so it will work properly only for English/ANSI characters!

I would suggest using a map, like you have tried. Or you can use XOR encryption, but output characters in a HEX format.

Tell me if you need more help with it..

Offline

#8 2008-05-26 23:07

airjrdn
Member
Registered: 2007-07-14
Posts: 22

Re: Pascal script to XOR encrypt filenames?

I really appreciate the help.  I'll give it a try tonight after the kids are in bed.

The main issue I'd have w/my first solution is that all of them come back as all caps.

Offline

#9 2008-05-26 23:24

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Pascal script to XOR encrypt filenames?

I guess the problem with translit rule is that when you substitute a letter with something that's not a letter it stops producing pairs of lowercase-uppercase letters (as it can no longer do it by simply add/substract 32 to an ascii code).
I don't know what's the purpose of the encoding filenames, so that might not be helpful, but if you substituted letters with letters (and not $dd$) then the translit rule should work as well.


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#10 2008-05-27 14:58

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,376

Re: Pascal script to XOR encrypt filenames?

I thought about it, and decided that it might become useful for a large group of users.

So I decided to make a proper script for encrypting filenames. It uses a XOR-BASE64 method: XOR encryption against a given string KEY, with modified BASE64 encoding (not a standard MIME BASE64) to make valid filenames. For the script to work you will need to use the latest development version: ReNamerBeta.zip (27 May 2008)

Anyway, all you have to worry about are the 2 constants at the top of the script: KEY and DECODE. KEY should any text - which is your secret key. DECODE should be a "True" if you are decoding filenames, and "False" if you are encoding filenames. Script is included in the latest development version, but for the reference it is also included in this post (below).

NOTE: Make sure you test it first, before encrypting your filenames for good!

{ XOR-BASE64 Encrypt Filename against a String Key }

const
  KEY = 'My Secret Key'; // Set your key!
  DECODE = False; // "True" or "False"

function XorEnDeCrypt(const S: String): String;
var
  I, IK, Code: Integer;
begin
  IK := 1;
  Result := '';
  for I := 1 to Length(S) do
  begin
    if IK > Length(KEY) then IK := 1;
    Code := (Ord(S[i]) xor Ord(KEY[IK]));
    Result := Result + Chr(Code);
    IK := IK + 1;
  end;
end;

procedure ReplaceChar(var S: String; Find, Replace: Char);
var
  I: Integer;
begin
  for I:=1 to Length(S) do
    if S[i] = Find then
      S[i] := Replace;
end;

function Encrypt(const S: WideString): String;
begin
  Result := UTF8Encode(S);
  Result := XorEnDeCrypt(Result);
  Result := Base64Encode(Result);
  ReplaceChar(Result, '/', '=');
end;

function Decrypt(const S: String): WideString;
var
  Temp: String;
begin
  Temp := S;
  ReplaceChar(Temp, '=', '/');
  Temp := Base64Decode(Temp);
  Temp := XorEnDeCrypt(Temp);
  Result := UTF8Decode(Temp);
end;
  
begin
  if DECODE then
    FileName := Decrypt(FileName)
  else
    FileName := Encrypt(FileName);
end.

Last edited by den4b (2008-05-27 15:30)

Offline

Board footer

Powered by FluxBB