Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Delphi代码转换为C#(河豚)_C#_Delphi_Encryption_Blowfish_Encryption Symmetric - Fatal编程技术网

将Delphi代码转换为C#(河豚)

将Delphi代码转换为C#(河豚),c#,delphi,encryption,blowfish,encryption-symmetric,C#,Delphi,Encryption,Blowfish,Encryption Symmetric,我不是Developer Delphi,但我必须将此Delphi代码转换为C#: 我在研究中发现了以下网站: 及 我不懂台词: FStringFormat:=4196; 为什么格式有预定义的大小?河豚(DECUtil)是否有另一种转变 以及模式: Mode := TCipherMode(0); delphi中的密码源 (http://www.koders.com/delphi/fidE1F5EC890EF9FD7D5FFEB524898B00BC8403B799.aspx) 参数“mod

我不是Developer Delphi,但我必须将此Delphi代码转换为C#:

我在研究中发现了以下网站:

我不懂台词:

FStringFormat:=4196;
为什么格式有预定义的大小?河豚(DECUtil)是否有另一种转变

以及模式:

Mode := TCipherMode(0);
delphi中的密码源

(http://www.koders.com/delphi/fidE1F5EC890EF9FD7D5FFEB524898B00BC8403B799.aspx) 参数“mode”的顺序如下: cmCTS、cmCBC、cmCFB、cmOFB、cmECB、cmCTSMAC、cmCBCMAC、cmCFBMAC

所以我假设在delphi中,模式0是cmCTS。。。但实际上我不知道

结果示例:user:ADMIN pass:ADMIN-->pass:fAtP3sk=

FStringFormat:=4196;
这纯粹是给变量赋值,
FStringFormat
是一个声明的整数值,现在您给它一个值

var 
  FStringFormat: Integer; 
begin 
  FStringFormat := 4196; 
这纯粹是给变量赋值,
FStringFormat
是一个声明的整数值,现在您给它一个值

var 
  FStringFormat: Integer; 
begin 
  FStringFormat := 4196; 

Int32 FStringFormat; 
FStringFormat = 4196; 
在C#中

FMode:=TCipherMode(0)”是枚举值的整数的typecase。Delphi枚举与C#中的枚举几乎相同;默认情况下,它们从
0
开始,因此

type
  TCipherMode = ( cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
这意味着
cmCTS
的数值为
0
cmCBC
1,依此类推

代码应该写得很好

FMode := cmCTS;
这不仅减少了要输入的字符,而且对于将来阅读它的人(像你一样)来说更清晰了

Int32 FStringFormat; 
FStringFormat = 4196; 
在C#中

FMode:=TCipherMode(0)”是枚举值的整数的typecase。Delphi枚举与C#中的枚举几乎相同;默认情况下,它们从
0
开始,因此

type
  TCipherMode = ( cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
这意味着
cmCTS
的数值为
0
cmCBC
1,依此类推

代码应该写得很好

FMode := cmCTS;

这不仅减少了要输入的字符,而且对于将来阅读它的人(像你一样)来说更清晰了

变量(4196)的
FStringFormat
值等于使用在如下定义的单位中定义的
fmtMIME64
常量

 fmtMIME64      = $1064;     // MIME Base 64
此值用于格式化传递给CodeString方法的字符串。在这种情况下,该行

 R := CodeString(Clave, paEncode, FStringFormat);
返回MIME Base 64格式的
Clave
变量的值

现在关于线路

模式:=TCipherMode(0)

您正在将Mode属性设置为枚举的第一个值

 TCipherMode = (cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
在这种情况下相当于写

Mode := cmCTS;

FStringFormat
变量(4196)的值等于使用单元中定义的
fmtMIME64
常量,该单位定义如下

 fmtMIME64      = $1064;     // MIME Base 64
此值用于格式化传递给CodeString方法的字符串。在这种情况下,该行

 R := CodeString(Clave, paEncode, FStringFormat);
返回MIME Base 64格式的
Clave
变量的值

现在关于线路

模式:=TCipherMode(0)

您正在将Mode属性设置为枚举的第一个值

 TCipherMode = (cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
在这种情况下相当于写

Mode := cmCTS;

查看Delphi对象的源代码,有一些为字符串格式声明的常量

fmtMIME64      = $1064;     // MIME Base 64
“$”在delphi中定义了一个十六进制数,因此示例代码中使用了$1064=4196

TCipherMode(0)

TCipherMode是对以下枚举类型的引用:

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

如果进行以下替换,代码会更容易理解:

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;

查看Delphi对象的源代码,有一些为字符串格式声明的常量

fmtMIME64      = $1064;     // MIME Base 64
“$”在delphi中定义了一个十六进制数,因此示例代码中使用了$1064=4196

TCipherMode(0)

TCipherMode是对以下枚举类型的引用:

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

如果进行以下替换,代码会更容易理解:

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;

首先感谢所有的答案

我不知道我是否可以在这里发布解决方案,但如果我可以帮助任何人

最后,我将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.
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);
        }
并在C#中使用此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.
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);
        }

它不是超级干净,但要做好工作…

首先感谢所有的答案

我不知道我是否可以在这里发布解决方案,但如果我可以帮助任何人

最后,我将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.
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);
        }
并在C#中使用此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.
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);
        }

它不是超干净的,但可以完成任务…

@Gilles:
$
在Delphi中表示十六进制,所以在C#中是
0x1064
,是4196十进制。@Gilles:
$
在Delphi中表示十六进制,所以在C#中是
0x1064
,是4196十进制。