#1 2018-12-21 09:30:11

ertank
Member
Registered: 2016-03-16
Posts: 168

Decrypt OpenSSL library crypt data

Hello,

I have to decrypt data which is crypt using OpenSSL library AES-CBC on a Linux system. Decryption will be on Windows. Plain text encrypt on Linux is: "The quick brown fox jumps over the lazy dog"

Below is my code for decryption:

function DecryptItAESCBC(const s: string; AKey, IV: Array of Byte; out Value: string): Boolean;
var
  Aes: TAESCBC;
  LocalIV: THash128;
  Utf8String: RawByteString;
  MyBytes: TBytes;
  I: Integer;
begin
  if s = EmptyStr then Exit(False);
  for I := Low(LocalIV) to High(LocalIV) do LocalIV[I] := IV[I];
  Aes := TAESCBC.Create(AKey, 256);
  try
    Aes.IV := LocalIV;
    SetLength(Utf8String, 1024);
    try
      MyBytes := TNetEncoding.Base64.DecodeStringToBytes(s);  // I confirm bytes are identical to crypt data on other end
      Aes.Decrypt(Pointer(MyBytes), Pointer(Utf8String), 1024);
    except
      Value  := EmptyStr;
      Exit(False);
    end;
    Value := UTF8ToString(Utf8String);
  finally
    Aes.Free();
  end;
  Result := True;
end;

For test purpose simple Key and IV used. My code to call above function is:

procedure TForm1.Button1Click(Sender: TObject);
var
  Key: TBytes;
  IV: TBytes;
  I: Integer;
  Plain: string;
begin
  SetLength(Key, 32);
  SetLength(IV, 16);

  for I := Low(Key) to High(Key) do Key[I] := I;  // same Key used to crypt on other end
  for I := Low(IV) to High(IV) do IV[I] := I;  // same IV used to crypt on other end
  if not DecryptItAESCBC(TFile.ReadAllText('Z:\b64crypt'), Key, IV, Plain) then
  begin
    Memo1.Lines.Add('Cannot decrypt!');
    Exit();
  end;
  Memo1.Lines.Add('Plain text: ' + AnsiQuotedStr(Plain, '"'));
end;

What I read in memo is: "The quick brown fox jumps over the lazy dog#5#5#5#5#5'n'#$14#$E'9'" (without double quotes)

I could not find my mistake. I tested decryption using OpenSSL on Linux and it works just fine. On Windows, I read above 9 more characters and most of them are invisible.

Any help is appreciated.

Offline

#2 2018-12-21 10:06:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Decrypt OpenSSL library crypt data

You have a padding issue I guess.
See how padding works - in a nutshell, AES works on 16 bytes blocks, so you need to do something with your last bytes.

In your Aes.Decrypt() code, you uncypher 1000 bytes... this is not the way to do it!

Use a Aes method using padding algorithm, e.g. PKCS7.

Offline

#3 2018-12-21 10:14:40

ertank
Member
Registered: 2016-03-16
Posts: 168

Re: Decrypt OpenSSL library crypt data

ab wrote:

You have a padding issue I guess.
See how padding works - in a nutshell, AES works on 16 bytes blocks, so you need to do something with your last bytes.

In your Aes.Decrypt() code, you uncypher 1000 bytes... this is not the way to do it!

Use a Aes method using padding algorithm, e.g. PKCS7.

Exactly, changing one line and I can read same text on Windows.

Utf8String := Aes.DecryptPKCS7Buffer(Pointer(MyBytes), Length(MyBytes), False);

Thank you.

Offline

Board footer

Powered by FluxBB