2012-09-05 126 views
1

我不是一個develloper德爾福,但我必須改變這個Delphi代碼C#:翻譯Delphi代碼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; 

我發現在我的研究以下網站:

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

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

我不明白這一行:

FStringFormat:=4196; 

爲什麼有預定義的格式大小?用河豚(DECUtil)有另一種轉變嗎?

和模式:

Mode := TCipherMode(0); 
在密碼

(http://www.koders.com/delphi/fidE1F5EC890EF9FD7D5FFEB524898B00BC8403B799.aspx) 參數 '模式' 的Delphi源

有以下順序: 羧甲基殼聚糖,cmCBC,cmCFB,cmOFB,cmECB,cmCTSMAC,cmCBCMAC,cmCFBMAC

所以我想在delphi模式0是cmCTS ...但實際上我不知道。

結果的一個例子:用戶:ADMIN傳遞:ADMIN --->通:fAtP3sk =

回答

2

FStringFormat變量(4196)的值等於用在DECUtil單元中定義的常量fmtMIME64其像這樣

fmtMIME64  = $1064;  // MIME Base 64 

該值用於格式化字符串定義傳遞給CodeString方法。在這種情況下,線

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

返回在MIME基址64格式Clave變量的值。

現在約線

Mode:= TCipherMode(0);

您正在將Mode屬性設置爲枚舉的第一個值。

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

在這種情況下相當於寫入。

Mode := cmCTS; 
+0

感謝RRUZ!爲什麼你可以肯定$ 1064 == 4196 ?? –

+2

@Gilles:'$'表示Delphi中的十六進制,所以它在C#中是'0x1064',它是4196十進制。 –

0
FStringFormat:=4196; 

這純粹只是將值分配給一個變量,FStringFormat是被宣佈一個整數值和現在你給它一個價值。

+0

行權,S o它只是int val = 4196 :) –

+0

但爲什麼使用這個值?我不能除以8 .... –

1
var 
    FStringFormat: Integer; 
begin 
    FStringFormat := 4196; 

相同

Int32 FStringFormat; 
FStringFormat = 4196; 
在C#

FMode := TCipherMode(0);'是整數到枚舉值的類型。 Delphi枚舉與C#中的枚舉幾乎相同;默認情況下,他們開始在0,所以

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

枚舉將意味着cmCTS0數值,cmCBC 1,依此類推。

的代碼應該已經被適當書面

FMode := cmCTS; 

這不僅鍵入字符少,但對於那些讀它在未來(比如你)更清晰。:-)

+0

好吧,我現在確定模式是CTS!謝謝 ! –

2

查看Delphi對象TCipher_Blowfish的源代碼,有一些常量聲明爲字符串格式。

fmtMIME64  = $1064;  // MIME Base 64 

「$」在delphi中定義了一個十六進制數,所以在示例代碼中使用$ 1064 = 4196。

TCipherMode(0);

TCipherMode是以下枚舉類型的引用:

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

所以TCipherMode(0)=羧甲基殼聚糖

的代碼是一個比較容易理解,如果你讓那些替換:

Function EncodeClave(InputString:String):String; 
var 
    BlowfishObj: TCipher_Blowfish; 
begin 

    BlowfishObj := TCipher_Blowfish.Create('CLAVE', nil); 
    try 

    BlowfishObj.Mode := cmCTS; // (Cipher Text Stealing) 
    Result := BlowfishObj.CodeString(InputString, paEncode, fmtMIME64); 

    finally 
    BlowfishObj.Free; 
    end; 
end; 
0

首先感謝所有答案!

我不知道如果我可以在這裏發佈的解決方案,但如果也許我可以幫助任何人......

finaly我已經改變了Delphi代碼DLL是這樣的:

library crypto; 

    uses 
    Cipher in '\Source\Cipher.pas', 
    DECUtil in '\Source\DECUtil.pas', 
    Hash in '\Source\Hash.pas', 
    SysUtils; 

{$R *.res} 

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; 

function MsgEncode(pIn: PWideChar; out pOut: PWideChar): LongBool; stdcall; 
var 
    sOut: string; 
    BuffSize: Integer; 
begin 
    sOut := EncodeClave(pIn); 
    BuffSize := SizeOf(Char)*(Length(sOut)+1); 
    GetMem(pOut, BuffSize); 
    FillChar(pOut^, BuffSize, 0); 
    Result := Length(sOut)>0; 
    if Result then 
    Move(PChar(sOut)^, pOut^, BuffSize); 
end; 

procedure BlockFree(p: Pointer); stdcall; 
begin 
    FreeMem(p); 
end; 

exports 
    MsgEncode, 
    BlockFree; 

begin 
end. 

並使用此DLL在C#這樣的:

class Program 
    { 
     [DllImport("crypto.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     public static extern bool MsgEncode(string pIn, out IntPtr pOut); 

     [DllImport("crypto.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] 
     public static extern void BlockFree(IntPtr p); 

     static void Main(string[] args) 
     { 
      IntPtr pOut; 

      string encode = "admin"; 
      string encoded = ""; 


      if (MsgEncode(encode, out pOut)) 
       encoded = Marshal.PtrToStringAnsi(pOut); 
      BlockFree(pOut); 

      Console.WriteLine("String Encoded '" + encode + "' : " + encoded); 
     } 

它不是超級乾淨,但做的工作......