【问题标题】:Aes|Rijndael-128 Hashed Text Decode in Delphi XE2Aes|Rijndael-128 Delphi XE2 中的散列文本解码
【发布时间】:2014-08-04 21:47:40
【问题描述】:

我们有php代码:

   define('myaesKey', 'znwoq8fq0jf2qjve8laper9f');  // 192 bits and 25 ch.  
   function encode($CodeTo) { 
      $Type = 'rijndael-128';
      $Mode = 'ecb';
      $IV = "1234567890123450";
      $Object = mcrypt_module_open($Type, '', $Mode, '');
      mcrypt_generic_init($Object , myaesKey, $IV);
      $Enc2Code = mcrypt_generic($Object , $CodeTo);
      mcrypt_generic_deinit($Object);
      mcrypt_module_close($Object);
      return bin2hex($secEncCode);
    }

$CodeTo 的长度是 5,CodeTo 是英文字母的可读符号,函数发送类似这样的东西 1e49651ba23801907e1d67c5a7c18e06 aefdc02bbcb8ed8e8209a935aa62be53

我尝试通过 diff 解码。方法,其中之一:

  const

  KeySize = 24; // 32 bytes = 256 bits     24 - 192
  BlockSize = 16; // 16 bytes = 128 bits

function Decrypt(AText:AnsiString):String;
var
  Cipher : TDCP_rijndael;   i:Integer;
  Data, Key, IV,NewStr : ansistring;
begin
  // Pad Key and IV with zeros as appropriate
  Key := PadWithZeros(ansistring('znwoq8fq0jf2qjve8laper9f'),KeySize);
  IV := PadWithZeros(ansistring('1234567890123450'),BlockSize);
  // Decode the Base64 encoded string


  NewStr:='';
  for i:=1 to (Length(AText) div 2) do
  NewStr:=NewStr+chr(byte(StrToInt('$'+Copy(AText,(i-1)*2+1,2))));
  Data := NewStr;

  // Create the cipher and initialise according to the key length
  Cipher := TDCP_rijndael.Create(nil);
  if Length(ansistring('znwoq8fq0jf2qjve8laper9f')) <= 16 then
    Cipher.Init(Key[1],128,@IV[1])
  else if Length(ansistring('znwoq8fq0jf2qjve8laper9f')) <= 24 then
    Cipher.Init(Key[1],192,@IV[1])
  else
    Cipher.Init(Key[1],256,@IV[1]);
  // Decrypt the data
 // Cipher.DecryptCBC(Data[1],Data[1],Length(Data));
  Cipher.DecryptECB(Data[1],Data[1]);
  // Free the cipher and clear sensitive information
  Cipher.Free;
  FillChar(Key[1],Length(Key),0);
  // Display the result
  result:= Data;
end;

但是解码出来的文字是错误的

6d309aab9887deed8da964cca8818eb4 µ€ц‰ъиTDHQ ЮB№еП

为什么? 有人可以帮忙吗? http://www.tools4noobs.com/online_tools/decrypt/ withot IV 很容易解码...

【问题讨论】:

标签: php delphi encryption delphi-xe2 rijndael


【解决方案1】:

尝试使用这个

function AESDecrypt(AData, AKey: String): string;
var
    KeyByte,Data,Dest:TBytes;
    KeyBlock:integer;
    Cipher:TDCP_rijndael;

begin
  KeyByte:=TEncoding.UTF8.GetBytes(AKey);
  while (Length(KeyByte) mod 16 <> 0) do begin
    SetLength(KeyByte,Length(KeyByte)+1);
    KeyByte[Length(KeyByte)-1]:=0;
  end;

  SetLength(Data,Length(AData) div 2);
  SetLEngth(Dest,Length(AData) div 2);

  Data:=GetBytesFromHex(AData);

  Cipher:= TDCP_rijndael.Create(nil);

  KeyBlock:=192; //by PHP code comment

  Cipher.Init(KeyByte[0],KeyBlock,nil); //for ECB method IV is optional

  try
    for i := 1 to (Length(AData) div 16) do
    begin
      Cipher.DecryptECB(Data[(i-1)*16],Dest[(i-1)*16]);
    end;
  finally
    Cipher.Burn;
  end;
  AData:=TEncoding.UTF8.GetString(Dest);
  Result:=AData;
end;

【讨论】:

    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多