Search code examples
c#delphiencryptionblowfishencryption-symmetric

Translating Delphi code to C# (Blowfish)


I'm not a develloper Delphi, but i must transform this delphi code to C#:

Function EncodeClave(Clave:String):String;
 var
   R: String;
   FStringFormat:Integer;
 begin
   FStringFormat:=4196;
 with TCipher_Blowfish.Create('CLAVE', nil) do
  try
    Mode := TCipherMode(0);
    R := CodeString(Clave, paEncode, FStringFormat);
    Result := R;
  finally
    Free;
  end;
 end;

I have found the following sites in my research:

http://www.bouncycastle.org/csharp/

and

http://www.schneier.com/code/blowfish.cs

I don't understand the line :

FStringFormat:=4196;

Why is there a predefine size of the format? Is there another transformation with blowfish (DECUtil) ?

and the mode :

Mode := TCipherMode(0);

in the delphi source of Cipher

(http://www.koders.com/delphi/fidE1F5EC890EF9FD7D5FFEB524898B00BC8403B799.aspx) the parameter 'mode' have the following order : cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC

So I suppose in delphi the mode 0 is cmCTS ... but in reality I don't know.

an example of result : user : ADMIN pass : ADMIN ---> pass : fAtP3sk=


Solution

  • The value of the FStringFormat variable (4196) is equal to use the fmtMIME64 const defined in the DECUtil unit which is defined like so

     fmtMIME64      = $1064;     // MIME Base 64
    

    This value is used to format the string passed to the CodeString method. in this case the line

     R := CodeString(Clave, paEncode, FStringFormat);
    

    returns the value of the Clave variable in the MIME Base 64 format.

    Now about the line

    Mode := TCipherMode(0);

    you are setting the the Mode property to the first value of the enumeration.

     TCipherMode = (cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
    

    in this case is equivalent to write.

    Mode := cmCTS;