Question

We have php code:

   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);
    }

Length of $CodeTo is 5, CodeTo is readable symbols of English Alphabet, function send somethind like this 1e49651ba23801907e1d67c5a7c18e06 aefdc02bbcb8ed8e8209a935aa62be53

I tried to decode by diff. ways, one of this :

  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;

but the decoded text is wrong

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

Why? Can someone help? Easy to decode it by http://www.tools4noobs.com/online_tools/decrypt/ withot IV ...

Was it helpful?

Solution

Try to use this

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top