#1 Re: mORMot 2 » How to use ObjectLoadJson to deserialize json not generated by mORMot? » 2024-01-10 16:22:09

tbo wrote:
liam1983 wrote:

My problem is that with 500 child objects in the list, the whole process lasts 10 seconds for deserialisation. Is there a way to speed it up?

Something else is wrong. In this article you will find some benchmark values for comparison. The time for 500 objects should only be a few milliseconds.

With best regards
Thomas

I believe this also, but how can I search for the reason? Because its not a very complex thing here.

#2 Re: mORMot 2 » How to use ObjectLoadJson to deserialize json not generated by mORMot? » 2024-01-10 15:47:32

Thanks! My problem is that with 500 child objects in the list, the whole process lasts 10 seconds for deserialisation. Is there a way to speed it up?

#3 mORMot 2 » How to use ObjectLoadJson to deserialize json not generated by mORMot? » 2024-01-10 13:31:11

liam1983
Replies: 5

Hello,

I try to deserialize JSON from a rest service

type
  TecRmiData = class
  private
    FTypeId: string;
    FTcdTypeId: string;
    FHerstSchluessel: string;
  public
    property TypeId: string read FTypeId write FTypeId;
    property TcdTypeId: string read FTcdTypeId write FTcdTypeId;
    property HerstSchluessel: string read FHerstSchluessel
      write FHerstSchluessel;

  end;

type
  TKbaDataResult = class(TObject)
  private
    FSearchKey: string;
    FTooMuchResults: Boolean;
    FNumberOfResults: Integer;
    FNumberOfPossibleResults: Integer;
    FFoundData: TArray<TecRmiData>;
  public
    constructor Create;
    destructor Destroy; override;

    property SearchKey: string read FSearchKey write FSearchKey;
    property TooMuchResults: Boolean read FTooMuchResults write FTooMuchResults;
    property NumberOfResults: Integer read FNumberOfResults
      write FNumberOfResults;
    property NumberOfPossibleResults: Integer read FNumberOfPossibleResults
      write FNumberOfPossibleResults;
    property FoundData: TArray<TecRmiData> read FFoundData;
  end;

Like this:

  
 t := TKbaDataResult.Create;
 valid := ObjectLoadJson(t, StringToUtf8(restRequest.Response.JSONText));

Valid is true but the object isnt populated with any data.

#4 Re: Other components » Try to decrypt a string from C# » 2022-05-30 14:16:48

Hi,

so I changed my C# code to

var rngCryptoServiceProvider = new RNGCryptoServiceProvider();

            // iv
            var iv = new byte[16];
            rngCryptoServiceProvider.GetNonZeroBytes(iv);

            var key64 = "TW4YnFfPEhLeehtwdEAZyJYg/zYxeeaDjlhKIifwGKI=";
                           
            using (var aesManaged = new AesManaged())
            {
                var keyBytes = Convert.FromBase64String(key64);

                aesManaged.Mode = CipherMode.CBC;
                aesManaged.Padding = PaddingMode.PKCS7;

                byte[] encrypted;
                using (var encryptor = aesManaged.CreateEncryptor(keyBytes, iv))
                using (var toMemoryStream = new MemoryStream())
                using (var writer = new CryptoStream(toMemoryStream, encryptor, CryptoStreamMode.Write))
                {
                    var valueBytes = Encoding.UTF8.GetBytes(value);

                    writer.Write(valueBytes, 0, valueBytes.Length);
                    writer.FlushFinalBlock();
                    encrypted = new byte[16 + toMemoryStream.Length];
                    iv.CopyTo(encrypted, 0);
                    toMemoryStream.ToArray().CopyTo(encrypted, 16);
                }

                aesManaged.Clear();
                return Convert.ToBase64String(encrypted);
            }

And give the same key and the result (WKaCNKYZpF9tb/CLp8fW/mX4r95OfjztavXhycsgDpA=) to Delphi

procedure TTest.DecryptEigenschaften(cryptedString: string);
var
  AES: TAESCBC;
  sha: TSHA256Digest;
  InBytes, OutBytes, Key: TBytes;
  s: string;
begin
  InBytes := TNetEncoding.Base64.DecodeStringToBytes(cryptedString);
//  sha := TNetEncoding.Base64.DecodeStringToBytes(cryptedString);
  Key := TNetEncoding.Base64.DecodeStringToBytes('TW4YnFfPEhLeehtwdEAZyJYg/zYxeeaDjlhKIifwGKI=');
  AES := TAESCBC.Create(key, 256);
  try

    OutBytes := AES.DecryptPKCS7(InBytes, True);
    s := TNetEncoding.Base64.EncodeBytesToString(OutBytes);
  finally
    FreeAndNil(AES);
  end;
end;
But still get the same error.

#5 Re: Other components » Try to decrypt a string from C# » 2022-05-30 13:32:07

rvk wrote:

You also use Rfc2898DeriveBytes to get a hash of the password.
In Delphi you use the same password as exact key (which is not the same as the passwordDeriveBytes password hash).
https://9to5answer.com/why-do-i-need-to … -key-or-iv

Rfc2898DeriveBytes is an implementation of PBKDF2. What it does is repeatedly hash the user password along with the salt.

You could look at the function PBKDF2_HMAC_SHA256 after you translated the C# to SHA256 (but I'm not sure if they give the exact same result).

You could also just try to use a exact 32 bytes key in your C# code which is the same as in Delphi (without the Rfc2898DeriveBytes step).

Thanks! I will try it.

#6 Re: Other components » Try to decrypt a string from C# » 2022-05-30 06:13:48

This is the C# code.

 public string Encrypt(string value, string password)
        {
            var rngCryptoServiceProvider = new RNGCryptoServiceProvider();

            // password
            var passwordBytes = Encoding.UTF8.GetBytes(password);

            // iv
            var iv = new byte[16];
            rngCryptoServiceProvider.GetNonZeroBytes(iv);

            // salt erstellen
            var salt = Encoding.UTF8.GetBytes(password);
            var sha1 = HashAlgorithm.Create("SHA1");
            salt = Enumerable.Range(0, Iterations).Aggregate(salt, (current, i) => sha1.ComputeHash(current));


            var passwordDeriveBytes = new Rfc2898DeriveBytes(passwordBytes, salt, Iterations);
            using (var aesManaged = new AesManaged())
            {
                var keyBytes = passwordDeriveBytes.GetBytes(KeySize / 8);

                aesManaged.Mode = CipherMode.CBC;
                aesManaged.Padding = PaddingMode.PKCS7;

                byte[] encrypted;
                using (var encryptor = aesManaged.CreateEncryptor(keyBytes, iv))
                using (var toMemoryStream = new MemoryStream())
                using (var writer = new CryptoStream(toMemoryStream, encryptor, CryptoStreamMode.Write))
                {
                    var valueBytes = Encoding.UTF8.GetBytes(value);

                    writer.Write(valueBytes, 0, valueBytes.Length);
                    writer.FlushFinalBlock();
                    encrypted = new byte[16 + toMemoryStream.Length];
                    iv.CopyTo(encrypted, 0);
                    toMemoryStream.ToArray().CopyTo(encrypted, 16);
                }

                aesManaged.Clear();
                return Convert.ToBase64String(encrypted);
            }
        }

#7 Other components » Try to decrypt a string from C# » 2022-05-25 14:57:58

liam1983
Replies: 9

Hi,

I created the following Base64 string with C#
4XoaSLUowvAbRZl76s053aRzoWrMpxqayzBOk/syjdQ=
With the following settings:
Key Size: 256 bits / 32 bytes
Cipher Mode: CBC
Padding: PKCS#7
Initialization Vector: 16 byte
And the IV is at the beginning of the data.

The content is Hallo and the password was also Hallo.
So I try to do this in Delphi:

procedure TTest.DecryptEigenschaften(cryptedString: string);
var
  AES: TAESCBC;
  sha: TSHA256Digest;
  InBytes, OutBytes: TBytes;
  s: string;
begin
  InBytes := TNetEncoding.Base64.DecodeStringToBytes(cryptedString);
  sha := SHA256Digest(UTF8Encode('Hallo'));
  AES := TAESCBC.Create(sha, 256);
  try

    OutBytes := AES.DecryptPKCS7(InBytes, True);
    s := TNetEncoding.Base64.EncodeBytesToString(OutBytes);
  finally
    FreeAndNil(AES);
  end;
end;

But I got, invalid data. What to do?

#9 Re: PDF Engine » PDF as Background Image » 2021-01-05 10:17:29

Sorry here is a better description of my problem.

I export a Quickreport file to PDF.
          Pdf := TPdfDocument.Create();
          try
            Pdf.DefaultPaperSize := psA4;
            Pdf.UseUniscribe := False;
            Pdf.EmbeddedTTF := true;
            Pdf.EmbeddedWholeTTF := False;
            Pdf.UseOptionalContent := True;
            Pdf.StandardFontsReplace := False;
            Pdf.CompressionMethod := cmFlateDecode;
//            pic := TPicture.Create;
//            pic.LoadFromFile('C:\tmp\img_snow.jpg');
//            background := TPdfImage.Create(PDF, pic.Graphic, true);
//            PDF.AddXObject('BackgroundImage',background);
            TDesignQuickReport(qrd.ParentControl).Prepare;
            for i := 1 to TDesignQuickReport(qrd.ParentControl).QRPrinter.PageCount do begin
              aMeta := TDesignQuickReport(qrd.ParentControl).QRPrinter.GetPage(i);
              try
                Pdf.DefaultPageWidth := MulDiv(aMeta.Width,72,Pdf.ScreenLogPixels);
                Pdf.DefaultPageHeight := MulDiv(aMeta.Height,72,Pdf.ScreenLogPixels);
                page := Pdf.AddPage;
//              PDF.Canvas.DrawXObject(0,0,page.PageWidth,page.PageHeight,'BackgroundImage');

                Pdf.Canvas.RenderMetaFile(aMeta, 1,0, 0,0, tpExactTextCharacterPositining, 0, 0, tcNeverClip);
              finally
                aMeta.Free;
              end;
            end;
            Pdf.SaveToFile(SaveFileName);

it works with images, but I need to use it with a pdf file

#10 PDF Engine » PDF as Background Image » 2021-01-01 13:40:07

liam1983
Replies: 4

Hi,

I found a way to make Pictures as a Background or Watermark Image, but how to do this with a PDF?

Board footer

Powered by FluxBB