Recent

Author Topic: Randomally generated code  (Read 1006 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 616
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Randomally generated code
« Reply #15 on: April 28, 2025, 07:57:26 pm »
Quote
BUT, when the code exchanged to other machine (via TCP/IP)
then seconde machine must generate code using the same function and do comparison.

  [machine1] => code1 ---->(tcp/ip)----->[machine2] => code2
                      ==> (code1 = code2) ??
How can you ever do that if code1 is random?

Random implies random, so you can't regenerate it.
So you can never regenerate code2 to be the same as code1.
UNLESS code1 isn't really random.

I think there is misunderstanding ( I did not express the needs exactly ) What I mean by random is: writing a function that generate a number, string or code that can be send using TCP/IP protocol then when the server app catch data, It uses the same function to calculate (generate) a number, string or code, then compared to one sent by client side.
   I want just control that the communication come from my own client.
PS: this function must generate different codes every time I call it (I think the best way is to be based on datetime).
That's all.

Hope @rvk understand  :)
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

rvk

  • Hero Member
  • *****
  • Posts: 6715
Re: Randomally generated code
« Reply #16 on: April 28, 2025, 08:07:59 pm »
   I want just control that the communication come from my own client.
PS: this function must generate different codes every time I call it.
Yes, but the question is how secure does it need to be.

Like I said before... You can just use a datetime string (rounded to 10 seconds) with something (for example 'aa' prefixed). So aa20250428200210.123 and your client knows this comes from a client. Is that enough? Or do you have more requirements. You can also just use the Unix timestamp (rounded to 10 seconds).

It's very easy to 'hack'. As I, as a rogue computer, can just forge such messages.
If that's not a problem... Just use any format of datetime you like. (Int64 with unixtime or just a ASCII string with mentioned time code).

Knowing if a certain message comes from your own program is just a matter of creating your own format/protocol. If it starts with abcd{etc},datetimecode,your message it is a valid message. You see, you can just create your own 'protocol' to recognize your own messages. (The same as you look in a zip file it always has PK\x03\x04 etc magic number in front of it.)



« Last Edit: April 28, 2025, 08:10:35 pm by rvk »

BSaidus

  • Hero Member
  • *****
  • Posts: 616
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Randomally generated code
« Reply #17 on: April 28, 2025, 10:55:34 pm »
   I want just control that the communication come from my own client.
PS: this function must generate different codes every time I call it.
Yes, but the question is how secure does it need to be.

Like I said before... You can just use a datetime string (rounded to 10 seconds) with something (for example 'aa' prefixed). So aa20250428200210.123 and your client knows this comes from a client. Is that enough? Or do you have more requirements. You can also just use the Unix timestamp (rounded to 10 seconds).

It's very easy to 'hack'. As I, as a rogue computer, can just forge such messages.
If that's not a problem... Just use any format of datetime you like. (Int64 with unixtime or just a ASCII string with mentioned time code).

Knowing if a certain message comes from your own program is just a matter of creating your own format/protocol. If it starts with abcd{etc},datetimecode,your message it is a valid message. You see, you can just create your own 'protocol' to recognize your own messages. (The same as you look in a zip file it always has PK\x03\x04 etc magic number in front of it.)

All what you sayed is good, but, if one intercept the message data and try to resend some data formated as previous intercepted message, the code I want is one shot (generated using datetime) and transformed (hashed, crypted) with some mecanism to be unreadable by the interceptor
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

rvk

  • Hero Member
  • *****
  • Posts: 6715
Re: Randomally generated code
« Reply #18 on: April 28, 2025, 11:06:58 pm »
That's why I was asking about security. So you want encryption.
And don't leak the encryption method (and key) so others can use it to fake your messages.

BTW. The timestamp is just one part of it. But the encryption is more important because a timestamp in an unencrypted message can always be faked.

So you can just encrypt the message with a key (with your preferred encryption) and you need to make sure the key is only available for the client.

Thaddy

  • Hero Member
  • *****
  • Posts: 16928
  • Ceterum censeo Trump esse delendam
Re: Randomally generated code
« Reply #19 on: April 29, 2025, 01:48:29 pm »
Maybe this will help.
The key is a number, any number that fits in 32 bits.
Cycles is the number of cycles to repeat the encryption.
You can send the key and optionally the number of cycles per sms.
The other side needs a small executable that does the decryption
or you can build it in the application:
Code: Pascal  [Select][+][-]
  1.   function Crypt(const Data:TBytes;Key:Cardinal;Cycles:Cardinal=1):TBytes;inline;
  2.   var
  3.     i,j:integer;
  4.    begin
  5.     Result := [];
  6.     Setlength(Result, Length(Data));
  7.     For i:= 0 to Pred(Cycles) do
  8.       for j:= 0 to high(Data) do
  9.       begin
  10.         { this is an lcg type prng, actually Delphi's }
  11.         Key := Key * 134775813  + 1;
  12.         Result[j] := Data[j] xor (Key * 256 shr 32);
  13.       end;
  14.   end;
  15.   // more secure
  16.   function CryptXos(const Data:TBytes;Key:Cardinal;Cycles:Cardinal=1):TBytes;inline;
  17.   var
  18.     i,j:integer;
  19.    begin
  20.     Result := [];
  21.     Setlength(Result, Length(Data));
  22.     For i:= 0 to Pred(Cycles) do
  23.       for j:= 0 to high(Data) do
  24.       begin
  25.         key := key xor(key shl 13);
  26.         key := key shr 17;
  27.         key := key xor (key shl 5);
  28.         result[j] := key;
  29.       end;
  30.   end;

Just put your code in a TBytes.
Then send the key and optionally the number of cycles per sms and the encrypted data over internet.
The other side will be able to decrypt by calling the same function with the key and optionally the cycles from the sms.
Code: [Select]
trunk
FPC 3.3.1-17789-g5952c5452b [2025/04/13] for x86_64 - Win64
tkUString
Enter your pincode:
12345
Encrypt: This is thè téxt to êncrypt/decrypt Ä.
Equivalent in bytes:
   54  124  203    3   40  144   78  162  163  174  216  148   90  136    0  183  251  243  162  137  182  240  141   68  210  224   25  153  247  166  119  222  221  127  169  180  109  253
Decrypt: 䨶赼埋ᐃ劐쥎涢첣ෘ澔띚䢈က₷䟻훳ಢ쮉ຶ㗰튍迒쓠ꮙ૷䆦ꭷߞዝ챿ꖩ▴ㇽ
Result:  This is thè téxt to êncrypt/decrypt Ä.
If required I can easily adapt it to use an even more advanced (RSA) encryption but the XOS version is already very secure.
In effect the key is the random seed.
I also have a stream version.
This encryption is symmetric, so the same function encrypts and decrypts.
For normal people this is impossible to decrypt if you don't know the key and the cycles, it is way more advanced than simple XOR.
Let me know if this fits your requirements.



« Last Edit: April 29, 2025, 02:20:09 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 16928
  • Ceterum censeo Trump esse delendam
Re: Randomally generated code
« Reply #20 on: April 29, 2025, 06:41:17 pm »
I have noticed a small bug in CryptXos regarding the string types, but the TBytes encrypt/Decrypt OK and is the best way to use anyway, I added the string types just for display purpose. Use the TBytes version directly.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018