bin*_*nar 7 php delphi delphi-7 mcrypt rijndael
我正在尝试使用DEC 3.0库(Delphi Encryption Compedium Part I)来加密Delphi 7中的数据并通过POST将其发送到PHP脚本,我用mcrypt(RIJNDAEL_256,ECB模式)对其进行解密.
德尔福部分:
uses Windows, DECUtil, Cipher, Cipher1;
function EncryptMsgData(MsgData, Key: string): string;
var RCipher: TCipher_Rijndael;
begin
RCipher:= TCipher_Rijndael.Create(KeyStr, nil);
RCipher.Mode:= cmECB;
Result:= RCipher.CodeString(MsgData, paEncode, fmtMIME64);
RCipher.Free;
end;
Run Code Online (Sandbox Code Playgroud)
PHP部分:
function decryptMsgContent($msgContent, $sKey) {
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sKey, base64_decode($msgContent), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
}
Run Code Online (Sandbox Code Playgroud)
问题是PHP的解密不起作用,输出是乱码,与实际数据不同.
当然,Delphi Key和PHP $Key是相同的24个字符串.
现在我知道DEC 3.0已经老了,已经过时了,我不是加密专家,无法判断实际上是不是实际上是Rijndael 256.也许有人可以告诉我这个实现与PHP的mcrypt w/RIJNDAEL_256有什么不同.也许密钥大小不同,或块大小,但无法从代码中分辨出来.这是Cipher1.pas的摘录:
const
{ don’t change this }
Rijndael_Blocks = 4;
Rijndael_Rounds = 14;
class procedure TCipher_Rijndael.GetContext(var ABufSize, AKeySize, AUserSize: Integer);
begin
ABufSize := Rijndael_Blocks * 4;
AKeySize := 32;
AUserSize := (Rijndael_Rounds + 1) * Rijndael_Blocks * SizeOf(Integer) * 2;
end;
Run Code Online (Sandbox Code Playgroud)
问题:
我知道ECB模式不推荐,一旦我让ECB工作,我就会使用CBC.问题是,我是否必须将Delphi中生成的IV传输到PHP脚本中?或者知道钥匙就足够了,就像欧洲央行一样?
您正在调用TCipher.Create(const Password:String; AProtection:TProtection); 构造函数,它将在将密码传递给Init方法之前计算密码的哈希值,该方法执行所实现算法的标准密钥调度.要覆盖此密钥派生,请使用:
function EncryptMsgData(MsgData, Key: string): string;
var RCipher: TCipher_Rijndael;
begin
RCipher:= TCipher_Rijndael.Create('', nil);
RCipher.Init(Pointer(Key)^,Length(Key),nil);
RCipher.Mode:= cmECB;
Result:= RCipher.CodeString(MsgData, paEncode, fmtMIME64);
RCipher.Free;
Run Code Online (Sandbox Code Playgroud)
结束;