MHumm / DelphiEncryptionCompendium

Cryptographic library for Embarcadero Delphi and potentially for FPC as well
Apache License 2.0
257 stars 67 forks source link

DEC 6.0 - Shark, 3DES, 2DES some time fail to decrypt data in all modes except OFB #4

Closed piker00 closed 4 years ago

piker00 commented 6 years ago

Shark, 3DES, 2DES some time fail to decrypt data in all modes except OFB

MHumm commented 5 years ago

Sorry for only finding time to follow up on this now. Do you have any sample code demoing this? That would really help investigating and fixing this.

MHumm commented 4 years ago

I did a test now (will publish the code used later). 10.000 iterations each for 2DES encrypting/decrypting the same string and comparing the result. Works flawlessly for 2DES and 3DES but seems to have a general failure for Shark, as there it produced 10.000 failures as the decrypted string never matched the plain text fed into the encryption method.

Mode used was CFB each time and cipher key etc. were all the same each time. For me this suggests, that 2DES and 3DES do work but Shark has a general problem, despite having a unit test for it with data derived from the old 5.2/5.3 test program which does not report any error.

MHumm commented 4 years ago

As promised here the code of the test application used. Does anybody spot any flaws in my test?

program Shark_3DES_2DES_Bugtest;

{$APPTYPE CONSOLE}

{$R *.res}

uses System.SysUtils, DECCipherModes in '..\Source\DECCipherModes.pas', DECCiphers in '..\Source\DECCiphers.pas', DECBaseClass in '..\Source\DECBaseClass.pas', DECCipherBase in '..\Source\DECCipherBase.pas', DECCipherFormats in '..\Source\DECCipherFormats.pas', DECCRC in '..\Source\DECCRC.pas', DECFormat in '..\Source\DECFormat.pas', DECFormatBase in '..\Source\DECFormatBase.pas', DECUtil in '..\Source\DECUtil.pas', DECTypes in '..\Source\DECTypes.pas', DECUtilRawByteStringHelper in '..\Source\DECUtilRawByteStringHelper.pas', DECData in '..\Source\DECData.pas', DECCipherInterface in '..\Source\DECCipherInterface.pas';

var Cipher : TCipher_Shark; // We use raw byte string here since Unicode handling of Windows console // is not given SourceText : RawByteString; CipherText : string; // Key for the initialization of our encryption run CipherKey : RawByteString; IV : RawByteString; Input, Output : TBytes; i, n, ErrC : Integer;

const cPlainText = 'Beispielklartext'; begin Cipher := TCipher_Shark.Create; // number of decryption failures ErrC := 0;

try try for n := 1 to 10000 do begin // Init our encryption CipherKey := 'Passwort'; IV := #0#0#0#0#0#0#0#0; Cipher.Init(CipherKey, IV, 0); Cipher.Mode := cmCBCx;

    SourceText := cPlainText; //'Beispielklartext';
    WriteLn(n:5, ' Source text: ' + SourceText);

    Input := System.SysUtils.BytesOf(SourceText);

    Write(n:5, ' Source text in hex:    ');
    for i := 0 to high(Input) do
      Write(IntToHex(Input[i], 2), ' ');
    WriteLn;

    // Encrypt
    Output := Cipher.EncodeBytes(Input);

    Write(n:5, ' Encrypted data in hex: ');
    for i := 0 to high(Output) do
      Write(IntToHex(Output[i], 2), ' ');

    WriteLn;

    // Decrypt
    Cipher.Init(CipherKey, IV, 0);
    Output := Cipher.DecodeBytes(Output);

    SourceText := System.SysUtils.StringOf(Output);

    WriteLn(n:5, ' Decrypted data: ' + SourceText);

    if SourceText <> cPlainText then
    begin
      inc(ErrC);
      WriteLn('Decryption error!');
    end;

    // Show that using a different key results in a different output
    WriteLn;

    CipherKey := 'Password';
    Cipher.Init(CipherKey, IV, 0);
    Output := Cipher.DecodeBytes(Output);

    SourceText := System.SysUtils.StringOf(Output);

    WriteLn('Decrypted with different key: ' + SourceText);
  end;

  WriteLn;
  WriteLn('Number of decription errors ', ErrC);
  ReadLn;
except
  on E: Exception do
    Writeln(E.ClassName, ': ', E.Message);
end;

finally Cipher.Free; end; end.

MHumm commented 4 years ago

Did another test with Shark cipher now. When changing the cipher key from 'Passwort' to 'TCipher_Shark', which is longer, it properly encrypts and decrypts the encrypted text. This has been run 10.000 times in a loop as well so I cannot find any failure. At least not with the simplistic description of @piker00. So I close this one now.