我試圖使用DEC 3.0庫(Delphi Encryption Compedium Part I)在Delphi 7中的數據進行加密,並通過POST,在那裏我與mcrypt的解密它(發送到PHP腳本RIJNDAEL_256,ECB模式)。德爾福DEC庫(Rijndael算法)加密
德爾福部分:
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;
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));
}
的問題是,從PHP解密無法正常工作,輸出爲亂碼,從實際的數據不同。
當然,德爾福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;
方問題:
我知道不建議ECB模式,我得到歐洲央行的工作我會盡快使用CBC。問題是,我是否必須將Delphi中生成的IV傳遞給PHP腳本?或者知道關鍵是足夠的,就像歐洲央行?
這可能是一個非常愚蠢的問題。但是使用delphi可以解密你的加密數據嗎?哦,並且這個問題的答案是否有幫助:http://stackoverflow.com/q/8313992/41338 – RobS 2012-02-10 10:57:43
您可以調用mcrypt_create_iv()。 Delphi中使用的IV是什麼? – 2012-02-10 11:14:50
@ldsandon:talereader正在使用ECB模式。沒有IV。 – 2012-02-10 11:26:00