简体   繁体   English

PHP中的Delphi DEC Twofish加密和服务器端解密

[英]Delphi DEC Twofish Encryption and server-side decryption in PHP

I have to encrypt string in Delphi with Twofish/CBC algorithm, send it to server and decrypt there. 我必须使用Twofish / CBC算法在Delphi中加密字符串,将其发送到服务器并在那里解密。 I've tested the code below, and B64 encoding/decoding process works, however I'm stuck at cipher encryption/decryption. 我已经测试了下面的代码,并且B64编码/解码过程可以正常工作,但是我仍然坚持使用密码加密/解密。

I am using DEC 5.2 for Delphi. 我正在使用DEC 5.2 for Delphi。

Here is the Delphi code that does the encryption: 这是进行加密的Delphi代码:

class function TEncryption.EncryptStream(const AInStream: TStream; const AOutStream: TStream; const APassword: String): Boolean;
var
  ASalt: Binary;
  AData: Binary;
  APass: Binary;
begin
  with ValidCipher(TCipher_Twofish).Create, Context do
  try
    ASalt := RandomBinary(16);
    APass := ValidHash(THash_SHA1).KDFx(Binary(APassword), ASalt, KeySize);
    Mode := cmCBCx;
    Init(APass);

    EncodeStream(AInStream, AOutStream, AInStream.Size);
    result := TRUE;
  finally
    Free;
    ProtectBinary(ASalt);
    ProtectBinary(AData);
    ProtectBinary(APass);
  end;
end;

class function TEncryption.EncryptString(const AString, APassword: String): String;
var
  instream, outstream: TStringStream;
begin
  result := '';
  instream := TStringStream.Create(AString);
  try
    outstream := TStringStream.Create;
    try
      if EncryptStream(instream, outstream, APassword) then
        result := outstream.DataString;
    finally
      outstream.Free;
    end;
  finally
    instream.Free;
  end;
end;

And PHP function that is supposed to decrypt sent data: 还有应该解密发送数据的PHP函数:

function decrypt($input, $key) {
    $td = mcrypt_module_open('twofish', '', 'cbc', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);

    $decrypted_data = mdecrypt_generic($td, base64_decode_urlsafe($input));

    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return $decrypted_data;
}

I beleive I have to fiddle a bit more with salt and initialization vectors, however I have no idea how. 我相信我必须在盐和初始化向量上花更多的力气,但是我不知道该怎么做。 From what I understand, KDFx() function makes SHA1 hashed password out from user password and salt, but I'm pretty much stuck at that point. 据我了解,KDFx()函数将SHA1哈希密码从用户密码和盐中剔除出来,但是到那时我还是很困惑。

KDFx is an proprietary incompatible key derivation method, from the source: "class function TDECHash.KDFx // DEC's own KDF, even stronger". KDFx是一种专有的不兼容密钥派生方法,其来源为:“类函数TDECHash.KDFx // DEC自己的KDF,甚至更强大”。 You will have problens using this from DEC and PHP and should use other libraries. 您将在DEC和PHP中使用此问题,并且应使用其他库。 The problem is discussed eg in http://www.delphipraxis.net/171047-dec-5-2-mit-vector-deccipher-umbau-von-dot-net-auf-delphi.html 例如,在http://www.delphipraxis.net/171047-dec-5-2-mit-vector-deccipher-umbau-von-dot-net-auf-delphi.html中讨论了该问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM