Recent

Author Topic: (SOLVED) problem with cryptography, incompatibility between libraries  (Read 5055 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Hi guys, I'm writing a program that needs some components that conflict but only on windows (on mac they work). I isolated the problem in an example I am attaching. If you run the program you will see that the decryption is not successful, it returns a different result from the input. While if from project1.lpr try to comment on the unit uinit everything works. I think there is something that conflicts with the DecodeBase64 function. But I don't understand how to solve. Who helps me? thanks a lot
« Last Edit: May 08, 2019, 11:05:55 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 19402
  • Glad to be alive.
Re: problem with cryptography, incompatibility between libraries
« Reply #1 on: May 04, 2019, 03:46:24 pm »
Never use any string type while using AES. Use a raw byte type. AES en/decryption operates on bytes, not strings.

Also note a possible cause is interpreting a C char * as a Pascal string: that's wrong. a C char * is a pointer to an array of byte, not a Pascal char.... C doesn't know byte....
« Last Edit: May 04, 2019, 03:51:23 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #2 on: May 04, 2019, 08:31:52 pm »
can you please change my example so i understand how to do it.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Xor-el

  • Sr. Member
  • ****
  • Posts: 414
Re: problem with cryptography, incompatibility between libraries
« Reply #3 on: May 05, 2019, 03:03:31 am »
Hi guys, I'm writing a program that needs some components that conflict but only on windows (on mac they work). I isolated the problem in an example I am attaching. If you run the program you will see that the decryption is not successful, it returns a different result from the input. While if from project1.lpr try to comment on the unit uinit everything works. I think there is something that conflicts with the DecodeBase64 function. But I don't understand how to solve. Who helps me? thanks a lot

Hi, first of all I must say you are doing quite a number of unsafe and wrong things with respect to Cryptography in your code.

1. As Thaddy said, Cryptographic operations is done on Byte Arrays not Strings. performing them on strings will lead to loss of information in the long run.

2. Never use a password as your AES Key directly, rather use a Key stretching algorithm like PBKDF2 or Argon2 to generate a suitable key to use for your operation.

3. Your IV is meant to be a random sequence of bytes that (all things being equal ) and should not be used to encrypt more than one message. where things are done properly, an IV is regenerated for every new encryption operation that is to be done and prefixed to the encrypted message so it can be extracted when needed.

4. AES uses an IV of 16 bytes not 32.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #4 on: May 05, 2019, 08:55:47 pm »
Hi guys, I'm writing a program that needs some components that conflict but only on windows (on mac they work). I isolated the problem in an example I am attaching. If you run the program you will see that the decryption is not successful, it returns a different result from the input. While if from project1.lpr try to comment on the unit uinit everything works. I think there is something that conflicts with the DecodeBase64 function. But I don't understand how to solve. Who helps me? thanks a lot

Hi, first of all I must say you are doing quite a number of unsafe and wrong things with respect to Cryptography in your code.

1. As Thaddy said, Cryptographic operations is done on Byte Arrays not Strings. performing them on strings will lead to loss of information in the long run.

2. Never use a password as your AES Key directly, rather use a Key stretching algorithm like PBKDF2 or Argon2 to generate a suitable key to use for your operation.

3. Your IV is meant to be a random sequence of bytes that (all things being equal ) and should not be used to encrypt more than one message. where things are done properly, an IV is regenerated for every new encryption operation that is to be done and prefixed to the encrypted message so it can be extracted when needed.

4. AES uses an IV of 16 bytes not 32.

Thanks for the much appreciated directions. I just wanted to clarify that I used a fixed password because it is an example, in the program that I want to create the password is randomly generated by a server that will communicate it to the client through an asymmetric key encryption. For the rest you said you could change my example and repost it so I learn and understand better what you mean. thanks a lot
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #5 on: May 06, 2019, 11:01:15 am »
Ok, I tried to fix the string to byte array conversion. Look at the attached example. Is that what you meant?

I also wanted to ask for information on 16/32 bit blocks. I knew that if 16-bit blocks were used it was aes 128, while for 256-bit blocks it was necessary to use 32-bit blocks. It's wrong?

Moreover all these suggestions are welcome but I would also like to understand the problem of my post.

Quote
Hi guys, I'm writing a program that needs some components that conflict but only on windows (on mac they work). I isolated the problem in an example I am attaching. If you run the program you will see that the decryption is not successful, it returns a different result from the input. While if from project1.lpr try to comment on the unit uinit everything works. I think there is something that conflicts with the DecodeBase64 function. But I don't understand how to solve. Who helps me? thanks a lot
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #6 on: May 06, 2019, 12:15:20 pm »
Now I have an absurd thing. Starting from the second attachment I modified the test to be both visual (project1) and console (project2). The two projects share the sources for encryption. From console it works while from visual the decryption is wrong. Why? What am I doing wrong? See Attachment
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #7 on: May 07, 2019, 09:20:36 am »
No suggestion?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Xor-el

  • Sr. Member
  • ****
  • Posts: 414
Re: problem with cryptography, incompatibility between libraries
« Reply #8 on: May 07, 2019, 11:18:48 am »
Now I have an absurd thing. Starting from the second attachment I modified the test to be both visual (project1) and console (project2). The two projects share the sources for encryption. From console it works while from visual the decryption is wrong. Why? What am I doing wrong? See Attachment

as we all know, LCL projects treats strings as UTF8 but console projects treats strings using the ANSI Encoding on Windows so this might be where your problem sprouts from.
where does the decryption fail to be exact, in Base64Decoding or AES Decryption?

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #9 on: May 07, 2019, 11:30:57 am »
Now I have an absurd thing. Starting from the second attachment I modified the test to be both visual (project1) and console (project2). The two projects share the sources for encryption. From console it works while from visual the decryption is wrong. Why? What am I doing wrong? See Attachment

as we all know, LCL projects treats strings as UTF8 but console projects treats strings using the ANSI Encoding on Windows so this might be where your problem sprouts from.
where does the decryption fail to be exact, in Base64Decoding or AES Decryption?

AES Decryption
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 19402
  • Glad to be alive.
Re: problem with cryptography, incompatibility between libraries
« Reply #10 on: May 07, 2019, 11:48:08 am »
AES Decryption
{$macro on} {$define listen:=read}
You do not listen. AES is byte based... As is most modern encryption.
I am not the only one who gets grumpy if you do not listen.....

Maybe Xor-el has a kinder way to respond... :-[

(and actually: most are bit based)
« Last Edit: May 07, 2019, 11:53:40 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #11 on: May 07, 2019, 12:14:29 pm »
AES Decryption
{$macro on} {$define listen:=read}
You do not listen. AES is byte based... As is most modern encryption.
I am not the only one who gets grumpy if you do not listen.....

Maybe Xor-el has a kinder way to respond... :-[

(and actually: most are bit based)

Thaddy I read and in fact if you look at my posts I asked if anyone could edit my sources so that I could learn. No one answered and I tried to do it by myself, if you look at the subsequent sources I convert the strings to byte arrays and then perform encryption and decryption. So that means you're wrong. I'm really happy to learn, please change my source so that I can understand where I'm wrong and what is the solution to adopt. I will be forever grateful
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 19402
  • Glad to be alive.
Re: problem with cryptography, incompatibility between libraries
« Reply #12 on: May 07, 2019, 02:28:30 pm »
1. If there is a conflict between the implementations of the same algorithm, then one of the two has a wrong implementation.... Choose the one that is commonly accepted.
2. Have eyes and ears.
3. Program correctly, or at least try. (To program correctly is not something most people master, including me...)

I believe this is answered in the correct way and you really should pay attention.... Xor-el and me know a lot about the matter, both academically and professionally.
The immediate advantage of the latter statement is that is tends to work.... (e.g. Xor-el writes crypto libraries in full Pascal  that can be verified against for instance OpenSsl, are you aware of that?)
« Last Edit: May 07, 2019, 02:41:10 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #13 on: May 07, 2019, 02:42:52 pm »
1. If there is a conflict between the implementations of the same algorithm, then one of the two has a wrong implementation.... Choose the one that is commonly accepted.
2. Have eyes and ears.
3. Program correctly, or at least try.

I believe this is answered in the correct way and you really should pay attention.... Xor-el and me know a lot about the matter, both academically and professionally.
The immediate advantage of the latter statement is that is tends to work.... (e.g. Xor-el writes crypto libraries in full Pascal  that can be verified against for instance OpenSsl, are you aware of that?)

I don't doubt your skills. In fact I ask for help because I do not understand where I am wrong. Let's do this, since there is no one who wants to modify my example to correct it, you know where I can find an example of how to encrypt a text with aes256 bit, where you see as text text an array byte, then digit the array and turns it back into a string to send it over the network with synapse. Obviously then I also need an example for decrypting rate. Obviously it is a multi-platform example.

You are aware that xor-el write cryptographic libraries. In fact, I use his to manage encryption with asymmetric keys. Only I didn't find an example in its packages to encrypt and decrypt with 256-bit AES
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: problem with cryptography, incompatibility between libraries
« Reply #14 on: May 07, 2019, 02:55:00 pm »
For example of xor-el I found this example.

https://forum.lazarus.freepascal.org/index.php/topic,44746.msg314966.html#msg314966

But there is no decrypt.
« Last Edit: May 07, 2019, 03:09:47 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018